Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 560
A. K. Avatar asked Jan 18 '23 20:01

A. K.


2 Answers

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)

like image 103
Ingmar Blonk Avatar answered Jan 31 '23 04:01

Ingmar Blonk


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...

like image 38
Kerrek SB Avatar answered Jan 31 '23 03:01

Kerrek SB