"global" makefile 变量是否与特定于目标的变量不同?

Are "global" makefile variables distinct from target-specific variables?

来自docs:

All variables that appear within the VARIABLE-ASSIGNMENT are evaluated within the context of the target: thus, any previously-defined target-specific variable values will be in effect. Note that this variable is actually distinct from any "global" value: the two variables do not have to have the same flavor (recursive vs. simple).


所以,给定一个 makefile:

% : foo += 1

all : x;

x ::
    @echo '$(foo)'

foo := 2


和运行,我得到:

2 1 1


仔细看看上面的引述:

All variables that appear within the VARIABLE-ASSIGNMENT are evaluated within the context of the target.

无法解释 目标特定 定义如 % : foo += 1 将如何结束 在其值中包含,"global"(即非目标或模式特定)值:2.

更难证明这一点,当我们稍后看时,在上面的引用中:

Note that this variable is actually distinct from any "global" value: the two variables do not have to have the same flavor (recursive vs. simple).

如果,它们是 "distinct",为什么 Make 最终会针对目标 all 进行扩展,该值是从 [=62= 中获取的值的 "salad" ],以及来自特定目标的定义?

以及使用什么方法来实现价值 2 1 1,因为很明显,价值 1 产生 价值 2在上面的 makefile 中。

我相信您只是误解了 "distinct" 的意思。 并不表示它们是两个不同的变量。以下片段 "the two variables do not have to have the same flavor" 阐明了含义。这才是重点。全局变量 foo 和特定于目标的变量 foo 是两个不同的(但在扩展时合并)变量。

考虑到这一点,您得到的输出正是您所期望的。

分配给 foo 第一个 是全局分配的值 2

只有 之后,在目标处理期间,才会处理 foo += 1 赋值(两次,一次用于 all,一次用于 x).

这让你 2 1 1 完全如图所示。