I have main.cc and test.h files,
main.cc includes test.h
Here is the Makefile, but it doesn't work. any clue?
Code:main : main.o g++ -g main.o -o main main.o : main.cc test.h g++ -o main.o -c -g test.h main.cc
I have main.cc and test.h files,
main.cc includes test.h
Here is the Makefile, but it doesn't work. any clue?
Code:main : main.o g++ -g main.o -o main main.o : main.cc test.h g++ -o main.o -c -g test.h main.cc
Last edited by meili100; 04-29-2008 at 03:19 PM.
Thanks. It works.
Is that to say, as long as a head file is #included in a correct .cc file, the Makefile never needs to contain the head file? But why I often see a Makefile contains a head file?
No. As long as the header file is #included correctly, the compiler doesn't need to be supplied with it on the command line; it will access it when it compiles the appropriate .cc file.Originally Posted by meili100
Make will want to know about it however as a dependency so it can detect when to recompile certain parts.
This tells make that main.o depends on main.cc and test.h, and you want it to know this. If make detects that main.cc or test.h have been updated, then it will execute the command(s) you supply in order to create main.o, in this case the next line:Code:main.o : main.cc test.h
This calls the compiler and gives it test.h and main.cc as the source files, just as if you had typed it yourself on the command line. However, test.h is not a source file, it is a header, so you don't want to include it as one of the arguments to g++.Code:g++ -o main.o -c -g test.h main.cc
Notice that the first line is a "dependancy" while the second line is a "command". Dependancies are what tells make what depends on what. "main.o depends on main.cc and test.h, so if any of these are updated then execute the following command to re-build main.o"
Then:
tells make that "main depends on main.o, so if main.o has been updated, execute the following command(s) to rebuild main".Code:main : main.o
Is the command make uses (the same as you would type yourself on the command line) to build the file main.Code:g++ -g main.o -o main
So if you modify test.h, then the next time you make, it will see that main.o depends on test.h which was updated so it will execute the command to build main.o again, and then it will see that main depends on main.o which was updated, and so it will execute the command to build main.
Make can get really tricky, and I am still figuring it out myself, but it is a great tool. Google around for makefile tutorials.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "