Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

makefile - How to manage C header file dependencies?

I've a lot of C files, some have a header (.h), some files not.

Here's 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 change a header file, the target isn't recompiled because not included in the dependencies.

How can I manage this case?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The standard approach is to generate header dependencies automatically while compiling.

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

Your $(MyNotGCCCompiler) is likely to have a command line option to generate a dependencies file.

When using gcc it works like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...