Original article: automatically handles the dependency between header files
This article explains in detail how makefile automatically handles header file dependencies. The MAKEFILE file is as follows:
all: mainmain: main.o stack.o maze.ogcc $^ -o $@clean:-rm main *.o.PHONY: cleansources = main.c stack.c maze.cinclude $(sources:.c=.d)%.d: %.cset -e; rm -f $@; \$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \rm -f $@.$$$$
The explanation of the original article is clear, and I will not analyze it in detail. What I don't understand is in this sentence:
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
Can it be changed:
sed 's,\($*\)\.o[ :]*,\1.o : ,g' < $@.$$$$ > $@; \
I think $ @ is redundant. After careful consideration, I went into the test and found that $ @ is definitely indispensable and I checked my testing program.
My dependencies are as follows:
Main. O: Main. c Foo. h
Now I Add the following sentence to foo. h:
#include “barrrrrr.h”
The barrrrrrr. h file does not exist.
When $ @ is not added to the MAKEFILE file, compile the program and output the result:
make: Nothing to be done for 'all'
This is obviously not as expected. Main. c relies on barrrrr. H through Foo. H, and barrrrrr. h does not exist. This should be true only when an error is reported.
View the main. d file:
main.o: main.c foo.h
The original main. d file simply adds the dependency on barrrrr. H!
I found the counterexample and looked back at the role of $ @, so I can understand it very well.
Main. d also needs to have the same dependency as main. O.