How to manage C header file dependencies?

I have many C files, some have a header (.h), some do not.

Here is my makefile:

.SUFFIXES: SRC := $(wildard ./src/*.c) OBJ := $(SRC:%.c=%.o) all: $(OBJ) %.o: %.c $(MyNotGCCCompiler) "@../$(*F).cmd" 

It works fine, except that if I modify the header file, the target does not recompile because it is not included in the dependency.

How can I manage this case?

thanks

+1
source share
2 answers

The standard approach is to automatically create compilation header dependencies.

For the first compilation, no dependencies are needed, since each source file must be compiled. Subsequent recompilations of the load dependencies generated by the previous compilation to determine what needs to be recompiled.

Your $(MyNotGCCCompiler) will probably have a command line option to create a dependency file.

When using gcc it works as follows:

 .SUFFIXES: SRC := $(wildard ./src/*.c) OBJ := $(SRC:%.c=%.o) DEP := $(OBJ:%.o=%.d) all: $(OBJ) # when compiling produce a .d file as well %.o: %.c gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) -MD -MP -MF ${@:.o=.d} $< # don't fail on missing .d files # there won't be any on the first run -include $(DEP) 
+2
source

(I'm not sure how this does not mean evidence, but anyway :)

Add rules to explicitly list these dependencies, file by file. Preferably in a separate makefile that you include from the main.

There are tools (like gcc ) that can generate them for you; if you cannot use or create such a tool, you will need to maintain these rules yourself.

0
source

Source: https://habr.com/ru/post/1411593/


All Articles