I am reading Managing Projects with GNU Make Third Edition and in the section on auto dependency generation, I saw the following sed script
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;
Here is the entire rule with the include
include $(subst .c,.d,$(SOURCES))
%.d: %.c
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
After studying this I was left wondering why the g command is used. So I copied everything to a new directory and removed all the target files and removed the g command from the sed script and executed make again. All was built without error. I also diffed the two directories, the one with the makefile using the g command in the sed script and the one without, and only that one difference popped out.
So I am wondering if there is some corner case where this g command comes into play.
What am I missing?
Thanks, Steven
A friend just pointed out that the g in 's,($).o[ :],\1.o $@ : ,g' is not a sed command. Sed commands are separated by the semicolon. No wonder this made no sense in the context of sed commands.
The /g is a part of the regex and will cause it to look for all matches not just the first occurrence.
Thanks for pointing this out.