变量"$?"的错误扩展在makefile中 [英] Wrong expansion for the variable "$?" in makefile

查看:97
本文介绍了变量"$?"的错误扩展在makefile中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自文档:

$?

比目标更新的所有先决条件的名称, 它们之间有空格.

$?

The names of all the prerequisites that are newer than the target, with spaces between them.





现在,有了一个生成文件:

Now, given a makefile:

# Create target 'all', that is created later than 'old', which was created after 'older'.
$(shell touch older)
$(shell sleep 1)
$(shell touch old)
$(shell sleep 1)
$(shell touch all)

all : old phony;
    @echo '"$$?" is: "$?"'

# A command to turn the file 'old' even "older", by acquiring the modification-time of 'older'.
old ::
    cp -p 'older' 'old'


.PHONY: phony





正在运行,我得到:

Running, I get:

$ make
cp -p 'older' 'old'
"$?" is: "old phony"

# Trying again, but this time, with a *parallel-execution*.
$ make -j
cp -p 'older' 'old'
"$?" is: "phony"



*您能看到,对于并行执行(-j),Make扩展$?的值与对 non-parallel-execution执行扩展不同. em>?*

让我们首先分析第一种情况,即非并行执行.



*Can you see, that Make expand the value of $? differently for a parallel execution (-j), than it does for a non-parallel-execution?*

Let's first analyze the first case, i.e. for a non-parallel execution.

allold的修改时间为:

  • 在构建之前 old比"all"旧".这是不言而喻的.对吧?
  • 在Make完成前提条件 之后,即在Make"builds" old之后,old通过获取"而变得更老".名为older的文件的修改时间.这基本上是shell命令cp -p older old的净结果.现在,不难发现olderall强" .
  • Before the build, old was "older" than all. This is self-evident. Right?
  • After Make finished building the prerequisite, i.e. after Make "builds" old, old got "older", so to speak, by "acquiring" the modification time of a file named: older. This is basically a net-result of the shell command: cp -p older old. Now, it is not hard to see, that older was way "older" than all.

现在,由于 other 的先决条件,即phony.,Make最终构建了目标all.

Now, Make ended up building the target all, because of the other prerequisite, namely: phony.

但是,根据文档,$?应该仅扩展为比目标更新的所有先决条件的名称".这是确切报价.

But, according the documentation, $? should expands only to "the names of all the prerequisites that are newer than the target". This is an exact quote.

我们是否可以同意,old从未从未 更新.而且,如果不是phony的话,Make甚至不会重新构建all.

Could we agree, that old was never newer than all. And, if fact, had it not been for phony, Make would not even re-build all.

那么,对于Make来说,将$?扩展到old phony是多么的错误.

So, how wrong is it then, for Make, to expand $? to old phony.

phony我明白了.但是old吗?真的吗?!

phony I get. But old? Really?!



但是,现在让我们将注意力转移到并行执行上,其中Make将$?扩展为phony.



But, let's now turn our attention to parallel-execution, where Make expands $? as phony.

这里:

  1. 可以肯定地说,这种扩展是正确的(与 non-parallel 执行中的扩展相比). 原因是,只有phony 是触发all重建的文件.没有它,Make根本不会去重新构建all.

  1. Certainly, one can say, that this expansion is right (compared to the expansion in a non-parallel execution). The reason is, that only phony is the file that triggered the re-build of all. Without it, Make would not bother to re-build all, at-all.

一个人仍然纳闷,对于这两种执行方式,如何才能使扩展变量有所不同. 我什至无法想到,这与$?的扩展有什么关系.

总而言之,看起来随机"更改(即从 parallel 更改为 non-parallel 的执行模式修改)影响了看似无关的实体,即自动变量的扩展.为什么?

Still, one wonders, how can make expand the variable differently for a these two modes of execution. I can not even think, how this could be any relevant to the expansion of $?.

In summary, it looks like that a "random" change (i.e. a modification in the mode of execution, from a parallel to a non-parallel) influences a seemingly unrelated entity, that is the expansion of automatic-variables. Why?

推荐答案

使用规则构建了old,现在Make认为它是依赖关系图中的最新文件. 制作后不检查old .

Having built old using the rules, Make now considers that to be the newest file in the dependency graph. It does not inspect old after making it.

这显然是对伪造目标的正确选择,通常也是对文件目标的正确选择.

This is obviously the right thing to do for phony targets, and it usually is The Right Thing for file targets, too.

这篇关于变量"$?"的错误扩展在makefile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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