1

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.

4

1 回答 1

1

The g option in a sed script performs the search for multiple times within a line. (In your case, the regular expression "\($*\)\.o[ :]*".) Without g, only the first match in the line is used.

Hence if there was only going to be at most one match anyway, the g makes no difference.

于 2013-09-26T18:20:44.773 回答