gpt4 book ai didi

c - GNU 使 : Exporting a Simply Expanded Variable

转载 作者:太空宇宙 更新时间:2023-11-04 02:36:54 26 4
gpt4 key购买 nike

以下是我遇到的问题的简化版本:

主.mk:

export lst :=

tmp := a
lst += $(tmp)
$(warning lst is $(lst))

tmp := b
lst += $(tmp)
$(warning lst is $(lst))

all:
$(MAKE) -f sub.mk

子.mk:

tmp := c
lst += $(tmp)
$(warning lst is $(lst))

tmp := d
lst += $(tmp)
$(warning lst is $(lst))

运行 make -f main.mk 打印以下内容:

main.mk:7: lst is  a
main.mk:11: lst is a b
make -f sub.mk
sub.mk:4: lst is a b c
sub.mk:8: lst is a b d d

我在main.mk中将lst定义为简单扩展变量,为什么make在submake中将其转换为递归扩展变量?

我阅读了以下内容:

https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_6.html#SEC59 https://www.gnu.org/software/make/manual/html_node/Variables_002fRecursion.html

两者都没有指出变量在导出到 submake 时会失去其“ flavor ”。

我可以通过将其添加到 sub.mk 中轻松解决此问题:

lst := $(lst)

虽然看起来像黑客攻击。我做错了什么吗?

我在 Cygwin 中使用 GNU Make v4.1,我也在使用 GNU Make v3.81 的 Linux 中确认了这种行为。

最佳答案

我相信这是手册中这一点的结果

To pass down, or export, a variable, make adds the variable and its value to the environment for running each line of the recipe. The sub-make, in turn, uses the environment to initialize its table of variable values. See Variables from the Environment.

Communicating Variables to a Sub-make 中您在帖子中链接到的部分。

具体来说,没有一种方法(通过环境)来指示哪些变量是递归扩展的,哪些是简单扩展的。 (并非没有其他标记机制。)

对比一下

make automatically passes down variable values that were defined on the command line, by putting them in the MAKEFLAGS variable.

以及该变量(或 MAKEOVERRIDES-*-command-variables-*- 变量视情况而定)实际上可以包含 =/:= 表示 flavor 。

关于c - GNU 使 : Exporting a Simply Expanded Variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158577/

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