Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

makefile - GNU Make. Why this complex syntax to generate dependencies?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually even the rule itself is not necessary. There is a great overview of different approaches of generating Make-style dependencies in Advanced Auto-Dependency Generation article written by Paul D. Smith.

After all, the following rule should be enough (in case of using GCC):

%.o: %.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -o $@ -c $<

-include $(SOURCES:.c=.d)

UPD.

I have also answered a similar question a bit earlier. It contains an explanation (quotation of GCC manual) of -MMD -MP options.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...