GNU Make.为什么用这种复杂的语法生成依赖关系? [英] GNU Make. Why this complex syntax to generate dependencies?

查看:75
本文介绍了GNU Make.为什么用这种复杂的语法生成依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读使用GNU Make管理项目,并在第2.7章-自动依赖项生成中找到了此示例.作者从GNU手册中说:

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 $@.$$$$

但是,我能够做到这一点(请注意sed):

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 : |'  > $@;

所有这些行所做的就是生成依赖项,然后添加*.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

到 foo.o foo.d:bar.h foo.h fubar.h

to foo.o foo.d : bar.h foo.h fubar.h

我的更简单,并且看起来工作得很好,但是我认为GNU人员有理由使用他们的sed命令.另外:

Mine is simpler and seems to work quite well, but I assume that the GNU folks had a reason for their sed command. Also:

  • 为什么将文件重定向到sed?为什么不简单地将其作为公用线参数
  • 为什么不完全跳过中间文件?
  • 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?

我知道GNU的人也可能想到过这些,但是出于某种原因,使用了更复杂的设置.我只想了解他们的理由,所以我可以即时进行.

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.

推荐答案

解决问题:Why do a redirect of the file into sed?如果您这样做:

Addressing the question: Why do a redirect of the file into sed? If you do:

@$(CC) -M $(CPPFLAGS) $<  | sed 's|:| $*.d : |'  > $@;

并且编译失败(错误并不生成任何输出),您将创建一个空的目标文件.再次运行make时,它将看到新创建的空文件而不重新生成它,从而导致生成错误.使用中间文件是一种常见的防御策略,可以避免意外创建空目标.

and the compilation fails (errors out and generates no output), you will create an empty target file. When make is run again, it will see the newly created empty file and not regenerate it, leading to build errors. Using intermediate files is a common defensive strategy to avoid accidentally created an empty target.

这篇关于GNU Make.为什么用这种复杂的语法生成依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆