In the makefile of a program, one has to write rules that define the dependencies of each object file. Consider the object file fileA.o
. It is obvious that this object file depends on the source file fileA.c
. But it will also depend on all the header files that this source file includes. So the following rule should be added to the makefile:
# This rule states that fileA.o depends on fileA.c (obviously), but also
# on the header files fileA.h, fileB.h and fileC.h
fileA.o: fileA.c fileA.h fileB.h fileC.h
Note that the rule has no recipe. One could add a recipe to it, but it is strictly speaking not necessary, because GNU make can rely on an implicit rule (with recipe) to compile a *.c
file into a *.o
file.
Anyway, writing such rules manually is a hellish task. Just imagine the work to keep the makefile rules in sync with the #include statements from the source code.
The GNU make manual describes in chapter 4.14 "Generating Prerequisites Automatically" a methodology to automate this procedure. The procedure starts with the generation of a *.d
file for each source file. I quote:
For each source file name.c there is a makefile name.d which lists what files the object file name.o depends on.
The manual proceeds:
Here is the pattern rule to generate a file of prerequisites (i.e, a makefile) called name.d from a C source file called name.c :
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
Sadly, the manual does not explain in detail how this rule actually works. Yes, it gives the desired name.d file, but why? The rule is very obfuscated..
When looking at this rule, I get the feeling that its recipe will only run smoothly on Linux. Am I right? Is there a way to make this recipe run correctly on Windows as well?
Any help is greatly appreciated :-)