gpt4 book ai didi

variables - Makefile 变量初始化和导出

转载 作者:行者123 更新时间:2023-12-03 08:54:08 26 4
gpt4 key购买 nike

somevar := apple
export somevar
update := $(shell echo "v=$$somevar")

all:
@echo $(update)

我希望苹果作为命令的输出,但是它是空的,这让我想到了 export 和 :=发生在不同阶段的可变膨胀。如何克服这个?

最佳答案

问题是export将变量导出到命令使用的子shell;它不可用于其他作业中的扩展。所以不要试图从规则之外的环境中获取它。

somevar := apple
export somevar

update1 := $(shell perl -e 'print "method 1 $$ENV{somevar}\n"')
# Make runs the shell command, the shell does not know somevar, so update1 is "method 1 ".

update2 := perl -e 'print "method 2 $$ENV{somevar}\n"'
# Now update2 is perl -e 'print "method 2 $$ENV{somevar}\n"'

# Lest we forget:
update3 := method 3 $(somevar)

all:
echo $(update1)
$(update2)
echo $(update3)
perl -e 'print method 4 "$$ENV{somevar}\n"'

关于variables - Makefile 变量初始化和导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2838715/

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