本文介绍了Makefile变量的初始化和导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
somevar := apple
export somevar
update := $(shell echo "v=$$somevar")
all:
@echo $(update)
我希望将Apple作为命令输出,但是它为空,这使我认为导出和:=
变量扩展发生在不同的阶段.如何克服这个问题?
I was hoping to apple as output of command, however it's empty, which makes me think export and :=
variable expansion taking place on different phases. how to overcome this?
推荐答案
问题是export
将变量导出到命令所使用的子shell中.在其他作业中无法扩展.因此,不要试图从规则之外的环境中获取它.
The problem is that export
exports the variable to the subshells used by the commands; it is not available for expansion in other assignments. So don't try to get it from the environment outside a rule.
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"'
这篇关于Makefile变量的初始化和导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!