PDA

View Full Version : Help with making a Makefile



Eavan Hyde
05-03-2004, 03:11 AM
I would lik to create a program in C/C++ running in Linux with multiple files, with 1 main file calling all my subroutines. There will also be a header file containing all my global variables that will be shared to used in each of the individual subroutines(So that i can access the same data anywhere).
Each of the subroutines will be in contained in an individual file.

eg. main.cpp, funct1.cpp, funct2.cpp, calculate.cpp, variables.h

main.cpp will call funct1.cpp & funct2.cpp. In funct1.cpp & funct2.cpp, both will call on funct3.cpp.

Prob:
funct3 contains multiple small functions within. In which file should i declare these sub functions? should this funct3 be saved as a .cpp or a .h?

Since all the cpp files require e shared use of the same variables, do i have to #include "variables.h" in every of the files? If not, in which file should i having this statement? I am facing the problem of redeclaration of variables when linking the files together with my makefile. However, if i did not include this in each of the files, i am unable to create my object files.

I am unable to link the files together. How should i go about creating the makefile as well as rest of the files?

My makefile goes like this:



test: main.o funct1.o funct2.o funct3.o variables.h
cc -o test main.o funct1.o funct2.o funct3.o variables.h
main.o: main.cpp variables.h
cc -o main.cpp
funct1.o: funct1.cpp funct3.cpp variables.h
cc -o funct1.cpp
funct2.o: funct2.cpp funct3.cpp variables.h
cc -o funct1.cpp
funct3.o: funct3.cpp variables.h
cc -o funct1.cpp

gandalf_bar
05-03-2004, 03:36 AM
I would lik to create a program in C/C++ running in Linux with multiple files, with 1 main file calling all my subroutines. There will also be a header file containing all my global variables that will be shared to used in each of the individual subroutines(So that i can access the same data anywhere).
Each of the subroutines will be in contained in an individual file.

eg. main.cpp, funct1.cpp, funct2.cpp, calculate.cpp, variables.h

main.cpp will call funct1.cpp & funct2.cpp. In funct1.cpp & funct2.cpp, both will call on funct3.cpp.

Prob:
funct3 contains multiple small functions within. In which file should i declare these sub functions? should this funct3 be saved as a .cpp or a .h?

Since all the cpp files require e shared use of the same variables, do i have to #include "variables.h" in every of the files? If not, in which file should i having this statement? I am facing the problem of redeclaration of variables when linking the files together with my makefile. However, if i did not include this in each of the files, i am unable to create my object files.

I am unable to link the files together. How should i go about creating the makefile as well as rest of the files?

My makefile goes like this:



test: main.o funct1.o funct2.o funct3.o variables.h
cc -o test main.o funct1.o funct2.o funct3.o variables.h
main.o: main.cpp variables.h
cc -o main.cpp
funct1.o: funct1.cpp funct3.cpp variables.h
cc -o funct1.cpp
funct2.o: funct2.cpp funct3.cpp variables.h
cc -o funct1.cpp
funct3.o: funct3.cpp variables.h
cc -o funct1.cpp




It should be like this:


main.o: main.cpp variables.h
cc -c main.cpp
......................

Salem
05-03-2004, 03:36 AM
A makefile should contain your compilation dependencies, not your functional dependencies.

So
funct1.o: funct1.cpp funct3.cpp variables.h
You don't need to depend on funct3.cpp in order to compile func1.cpp. The point where funct.o depends on funct3.o is when you link all the files together.

All you need is the name of the source file, and the header file(s) which it includes.

For the sake of brevity, standard includes like iostream are not included in dependencies, since they can be regarded as never changing.

Also, your compiler command lines need to produce the target (funct1.o) from the dependencies



test: main.o funct1.o funct2.o funct3.o
cc -o test main.o funct1.o funct2.o funct3.o
main.o: main.cpp variables.h
cc -c main.cpp
funct1.o: funct1.cpp variables.h
cc -c funct1.cpp
funct2.o: funct2.cpp variables.h
cc -c funct2.cpp
funct3.o: funct3.cpp variables.h
cc -c funct3.cpp

Eavan Hyde
05-04-2004, 02:21 AM
Thks for pointing out the mistake.

I tried it however my program still does not work. I am getting problems getting my object file. Is it because of the way i do my #include in my cpp files.

I am getting the error of not finding the variables and when i include "variables.h" in all my cpp files, i get the error of redeclaration of variables. Why is this so?

basically my program requires a funct1.cpp to read in input values to be stored in the global variables in <variables.h> after which the rest of my functions will manipulate these values.

can anyone help?

chris78
05-04-2004, 02:50 AM
You should not declare variables in you header file.
If you need global variables (better try to avoid them if possible) then you need to state in each C-file in which you want to use them that they are extern.
Like this:


extern int global_number;


However you want to chose one file in which you leave the extern. In that file the memory for that variable will be allocated.

However i think you should read a little bit more basics documentation. They explain all this much better than i can do here :)

Chris

Eavan Hyde
05-04-2004, 03:05 AM
I was considering declaring variables in my header files because i have many variables in this program because it is a very large program consisting of many files(of which most of the files are functions).

It is hard for me to pass so many parameters into the functions and since i require the same variables and data, i was thinking of allocatting memory location so that i can access the data in each of the variables in the header files, thus the use of global variables.

Does it mean that i have to redeclare all e global variables in each of the files that requires them? If not how do i go about sharing the data n accessing them?

My data type varies and some of the functions call other functions which require the same data.

any suggesstions?