I'm reading Managing Projects with GNU Make, and found this example in Chapter 2.7 - Automatic Dependency Generation. The Author says their from the GNU manual:
%.d: %c
$(CC) -M $(CPPFLAGS $< > $@.$$$$; \
sed s',\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
However, I was able to do the same thing with this (note the sed
):
-include $(subst .c,.d,$(SOURCES))
%.d: %.c
@$(CC) -M $(CPPFLAGS) $< | sed 's|:| $*.d : |' > $@;
All these lines do is generate the dependencies, then add in the *.d
name. They had to change the first line from:
foo.o: bar.h foo.h fubar.h
to foo.o foo.d : bar.h foo.h fubar.h
Mine is simpler and seems to work quite well, but I assume that the GNU folks had a reason for their sed
command. Also:
- Why do a redirect of the file into
sed
? Why not simply take it as a commond line parameter - Why not skip the intermediary file completely?
I know the guys at GNU could have thought of these too, but for some reason, went with the more complex setup. I just want to understand their reasoning, so I can do these on the fly.