bash 是否可以在 Makefile 中设置环境变量 - 之后使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52114730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set Environment variables in Makefile - to be used after
提问by Chris G.
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.
我正在尝试在 Makefile 中设置一个环境变量,因此它可以在 sam shell 中运行的另一个程序中使用make
,但是在make
运行之后。
Update: This is not possible according to accepted answer with comments.
更新:根据已接受的带有评论的答案,这是不可能的。
Steps:
脚步:
- run
make test
setting env:export TEST_ENV_ONE=OneString
- run another program, that can read
TEST_ENV_ONE
- 运行
make test
设置环境:export TEST_ENV_ONE=OneString
- 运行另一个程序,可以读取
TEST_ENV_ONE
Tried this:
试过这个:
Not working:
不工作:
test:
export TEST_ENV_ONE=OneString
$(shell export TEST_ENV_TWO=TwoString)
Afterwards this is empty:
之后这是空的:
echo $TEST_ENV_ONE
echo $TEST_ENV_TWO
采纳答案by Renaud Pacalet
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:
如果您希望将环境变量导出到您调用的 shell 中,事情会有点困难,因为正如 ネロク 所解释的,您不能直接将环境变量从子进程 ( make
)导出到其父进程(从中调用的 shell)你调用make
)。但是,如果您接受这样调用 make:
eval "$(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:
那么确实有可能:只需export VARIABLE1=VALUE1; export VARIABLE2=VALUE2; ...
在您的食谱中回声即可。警告:您还必须保证 make 在标准输入上不会回显任何其他内容。但是您可以改用标准错误。例子:
$ 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
Note that make more or less considers environment variables as make variables. From GNU make manual:
请注意, make 或多或少将环境变量视为 make 变量。来自 GNU make 手册:
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 中的变量可以来自 make 运行的环境。make 启动时看到的每个环境变量都被转换为具有相同名称和值的 make 变量。但是,makefile 中的显式赋值或带有命令参数的赋值会覆盖环境。(如果指定了“-e”标志,则来自环境的值将覆盖 makefile 中的分配。请参阅选项摘要。但这不是推荐的做法。)
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:
因此,除非变量的值是 make 自身复杂计算的结果,否则获得相同结果的更自然的方法是在父 shell 中定义环境变量并在 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
回答by u8012646
Your export TEST_ENV_ONE=OneString
above is running in a dedicated shell. The subsequent commands run in other shell instances. Therefore, they don't inherit the environment variable TEST_ENV_ONE
.
您的export TEST_ENV_ONE=OneString
上述内容在专用外壳中运行。后续命令在其他 shell 实例中运行。因此,它们不继承环境变量TEST_ENV_ONE
。
You could use a top-level (i.e., not in a target's recipe) export
directivein the makefile:
您可以在 makefile 中使用顶级(即,不在目标的配方中)export
指令:
export env_var := MyEnvVariable
.PHONY: all
all:
echo env_var: $$env_var
This way, the variable env_var
is exported to the shells that will execute the recipes.
通过这种方式,变量env_var
被导出到将执行配方的 shell。
If you run make
with the makefile above:
如果您make
使用上面的 makefile运行:
$ make
echo env_var: $env_var
env_var: MyEnvVariable
As you can see from the output, the shell that run echo env_var: $env_var
had the variable env_var
in its environment.
从输出中可以看出,运行的 shell在其环境中echo env_var: $env_var
具有该变量env_var
。