我如何在目标之间传递ENV变量 [英] How can i pass ENV variables between make targets

查看:66
本文介绍了我如何在目标之间传递ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在makefile中有这样的

target1:
       export var1=test
       $(MAKE) target2

target2:
       echo $(var1)

这是空的

我还有其他依赖关系,所以我想在第一个目标中设置变量,然后所有子依赖项都应该能够访问

.ONESHELL:

target1:
        export var1=test
        echo $(var1)

输出

make target1
export var1=test
echo

解决方案

默认情况下,make为每个配方调用一个新的Shell环境,第一行的export ed变量不在第二行的范围内.

您可以通过多种方式解决此问题:

使用make的export指令导出变量

target1: export var1 := test
target1:
    $(MAKE) target2


使用make的命令行变量分配

target1:
    $(MAKE) target2 var1=test


使用shell命令变量分配

target1:
    var1=test $(MAKE) target2


在一个配方中合并两个命令

target1:
    export var1=test; $(MAKE) target2


强制使所有配方传递到相同的shell实例

.ONESHELL:

target1:
    export var1=test
    $(make) target2

I have like this in makefile

target1:
       export var1=test
       $(MAKE) target2

target2:
       echo $(var1)

This is coming as empty

I have other depencies so i want to set variable in first target and then all children dependencies should be able to access that

EDIT:

.ONESHELL:

target1:
        export var1=test
        echo $(var1)

output

make target1
export var1=test
echo

解决方案

By default make invokes a new shell environment for each recipe, the exported variable on the first line isn't in scope for the second.

You can fix this in multiple ways:

Export the variable with make's export directive

target1: export var1 := test
target1:
    $(MAKE) target2


Use make's command line variable assignment

target1:
    $(MAKE) target2 var1=test


Use shell command variable assignment

target1:
    var1=test $(MAKE) target2


Combine the two commands in a single recipe

target1:
    export var1=test; $(MAKE) target2


Force make to pass all recipes to the same shell instance

.ONESHELL:

target1:
    export var1=test
    $(make) target2

这篇关于我如何在目标之间传递ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆