是否可以在Makefile中设置环境变量-在之后使用 [英] Is it possible to set Environment variables in Makefile - to be used after

查看:210
本文介绍了是否可以在Makefile中设置环境变量-在之后使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Makefile中设置环境变量,因此可以在运行于sam shell中的另一个程序中以make的形式使用它,但是要在make运行之后使用.

I am trying to set an Environment variable in a Makefile, so it can be used in another program running in the sam shell as make, but after make has run.

更新:根据接受的带有答案的答案,这是不可能的.

Update: This is not possible according to accepted answer with comments.

步骤:

  1. 运行make test设置环境:export TEST_ENV_ONE=OneString
  2. 运行另一个程序,该程序可以读取TEST_ENV_ONE
  1. run make test setting env: export TEST_ENV_ONE=OneString
  2. run another program, that can read TEST_ENV_ONE

尝试过:

不起作用:

test:
    export TEST_ENV_ONE=OneString
    $(shell export TEST_ENV_TWO=TwoString)

此后为空:

echo $TEST_ENV_ONE
echo $TEST_ENV_TWO

推荐答案

如果您希望将环境变量导出到您从中调用的shell,这会使操作变得有些困难,因为正如ネロク所解释的那样,您不能直接导出从子进程(make)到其父进程(您从中调用make的外壳)的环境变量.但是,如果您接受调用make这样的话:

If you want the environment variables to be exported to the shell from which you invoked make things are a bit difficult because, as explained by ネロク, you cannot directly export environment variables from a child process (make) to its parent process (the shell from which you invoke make). But if you accept to invoke make like this:

eval "$(make)"

那么确实有可能:只需在您的配方中回显export VARIABLE1=VALUE1; export VARIABLE2=VALUE2; ....警告:您还必须保证标准输入上的make不会回显其他任何内容.但是您可以改用标准错误.示例:

then it is indeed possible: just echo export VARIABLE1=VALUE1; export VARIABLE2=VALUE2; ... in your recipe. Warning: you will also have to guarantee that nothing else gets echoed by make on the standard input. But you can use the standard error, instead. Example:

$ cat Makefile
export TEST_ENV_ONE := OneString

all:
    @printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)" 1>&2
    @printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$$TEST_ENV_ONE" 1>&2
    @printf 'export TEST_ENV_ONE="%s"\n' '$(TEST_ENV_ONE)'

$ unset TEST_ENV_ONE
$ printenv TEST_ENV_ONE
$ eval "$(make)"
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString

请注意,make或多或少将环境变量视为make变量.从GNU制作手册:

Note that make more or less considers environment variables as make variables. From GNU make manual:

make中的变量可以来自运行make的环境. make在启动时看到的每个环境变量都是 转换为具有相同名称和值的make变量. 但是,在makefile中或使用命令进行显式分配 参数,覆盖环境. (如果指定了-e标志, 然后来自环境的值将覆盖makefile中的分配. 请参阅选项摘要.但这不是推荐的做法.)

Variables in make can come from the environment in which make is run. Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (If the ‘-e’ flag is specified, then values from the environment override assignments in the makefile. See Summary of Options. But this is not recommended practice.)

因此,除非变量的值是make自身进行复杂计算的结果,否则获得相同结果的更自然的方法是在父shell中定义环境变量并按原样使用它.生成文件:

So, unless the value of your variable is the result of complex computations by make itself, a much more natural way to obtain the same result would be to define the environment variable in the parent shell and to use it as is in the Makefile:

$ cat Makefile
all:
    @printf 'make variable TEST_ENV_ONE = %s\n' "$(TEST_ENV_ONE)"
    @printf 'in-recipe shell variable TEST_ENV_ONE = %s\n' "$$TEST_ENV_ONE"

$ export TEST_ENV_ONE=OneString
$ make
make variable TEST_ENV_ONE = OneString
in-recipe shell variable TEST_ENV_ONE = OneString
$ printenv TEST_ENV_ONE
OneString

这篇关于是否可以在Makefile中设置环境变量-在之后使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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