gpt4 book ai didi

variables - 立即变量赋值

转载 作者:行者123 更新时间:2023-12-04 02:20:37 24 4
gpt4 key购买 nike

我在 make 文件中使用 := 与 += 分配的变量相结合时立即分配变量时遇到问题。例如:

VAR := a
VARS += $(VAR)

rule1:
echo "$(VARS)"

VAR := b
VARS += $(VAR)

rule2:
echo "$(VARS)"

当我运行 make 时,无论规则如何,它都会打印“b b”。我感到困惑的是,如果我改用以下 make 文件:

VARS += a

rule1:
echo "$(VARS)"

VARS += b

rule2:
echo "$(VARS)"

无论规则如何,它都会打印“a b”,这正是我所期望的。根据制作手册https://www.gnu.org/software/make/manual/html_node/Reading-Makefiles.html :

We say that expansion is immediate if it happens during the firstphase: in this case make will expand any variables or functions inthat section of a construct as the makefile is parsed.

immediate := immediate

immediate += deferred or immediate

For the append operator, ‘+=’, the right-hand side is consideredimmediate if the variable was previously set as a simple variable(‘:=’ or ‘::=’), and deferred otherwise.

如果 := 实际上是立即变量,并且立即变量在解析时被扩展,正如 make 手册所说的那样,那么我希望这些 make 文件具有相同的行为。为什么那不是真的?我认为这要么是 make 文档中的错误,要么是 make 中的错误。

最佳答案

你缺少的一点是配方行(规则的主体)在解析时没有扩展(最多它们可以部分扩展,想想像 $@$^ 例如),而是在规则执行时展开。

这在您上面引用的手册部分的最后一节中介绍了 How make Reads a Makefile :

Rule Definition

A rule is always expanded the same way, regardless of the form:

immediate : immediate ; deferred
deferred

That is, the target and prerequisite sections are expanded immediately, and the recipe used to construct the target is always deferred. This general rule is true for explicit rules, pattern rules, suffix rules, static pattern rules, and simple prerequisite definitions.

因此,虽然您认为分配是立即的是正确的,但您错过了配方上下文中的扩展不是。所以食谱总是在每个顶层任务完成后展开。

此外,对于第一个示例,您从未将 VARS 设置为简单扩展,因此它默认为递归扩展,因此当您编写时:

VAR := a
VARS += $(VAR)

并认为你正在做:

VAR := a
VARS += a

你不是。

你实际上是在做字面上写的事情,并将 $(VAR) 存储在 $(VARS) 的值中,这样当 make 完成时 的值$(VARS)$(VAR) $(VAR),然后自然地扩展为 b b

如果您将 VARS := 添加到第一个 makefile 示例的顶部,您将获得预期的 a b(从那时起 VARS 是在 += 分配时间简单地扩展和扩展 $(VAR)

关于variables - 立即变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30195873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com