开发者

how to write makefile to take care of changes in the header file

Actually i have a library 'cryptopp' and what i want is that when i make any change to a file and issue the make command it should take care of the changes made in any file in the source directory. well, the GNUMakefile of cryptoopp takes care of the changes 'if' made in the '.cpp' files but not for the changes开发者_JAVA百科 made in a '.h' file.

So what changes can i make in the 'GNUMakefile' of cryptopp so that it looks at all the modified header files and recompiles all the files dependent on the 'modified' header file.


If you are building with g++ you can let g++ generate dependancy makefiles. You can include these in your main makefile.

Use the -M and -M* arguments to use this feature. (see http://gcc.gnu.org/onlinedocs/gcc-4.6.1/gcc/Preprocessor-Options.html#Preprocessor-Options)


You have to add all the dependencies to your Makefile:

mycode.o: mycode.cpp mycode.h somelib.h resources.h
        $(CXX) -c -o $@ $< $(CXXFLAGS) $(INCLUDES)

If you already have a generic pattern matching command line, you don't have to say the command again, you can just list the dependencies:

%o: %.cpp
        $(CXX) -c -o $@ $< $(CXXFLAGS) $(INCLUDES)

mycode.o: mycode.cpp mycode.h somelib.h resources.h

yourcode.o: yourcode.cpp yourcode.h mycode.h somethingelse.h

# ...

In general, this is a terrible and unscalable mess. You'll almost definitely want a higher-level build system to generate the Makefile for you. Even for very small projects keeping the header dependencies up to date in the Makefile is such a pain that it is simply not worth it.

There are several popular portable build environments. I personally like cmake a lot, which includes discovery if you changed the build settings (say from Debug to Release) and will always build all the necessary files (for example, if you change the cmake master file and type "make" it'll automatically run cmake again for you first).

For a Unix-only solution you could try makedepend, or the infamous autotools, though that's a whole other headache...


You might try 'makedepend' if it's installed on your system. The easiest way is to add a target to your makefile. Something like:

depend:
    makedepend *.cc

You might have to replace the '*.cc' with a list of your source files. Then you can regenerate all the dependencies with 'make depend' command. You might want to redirect error messages to /dev/null since it always seems to generate a lot of noise.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜