是否可以在 Makefile 中设置环境变量 - 之后使用
Is it possible to set Environment variables in Makefile - to be used after
我正在尝试在 Makefile 中设置一个环境变量,因此它可以在另一个程序 运行ning 中使用 shell 作为 make
,但是在 make
有 运行.
更新:根据接受的评论答案,这是不可能的。
步骤:
- 运行
make test
设置环境:export TEST_ENV_ONE=OneString
- 运行 另一个程序,可以读取
TEST_ENV_ONE
试过这个:
不工作:
test:
export TEST_ENV_ONE=OneString
$(shell export TEST_ENV_TWO=TwoString)
之后这是空的:
echo $TEST_ENV_ONE
echo $TEST_ENV_TWO
你上面的export TEST_ENV_ONE=OneString
是运行宁在专用的shell。在其他 shell 实例中的后续命令 运行。因此,他们不继承环境变量TEST_ENV_ONE
.
您可以在 makefile 中使用顶级(即不在目标的配方中)export
directive:
export env_var := MyEnvVariable
.PHONY: all
all:
echo env_var: $$env_var
这样,变量 env_var
被导出到将执行配方的 shell。
如果您 运行 make
使用上面的 makefile:
$ make
echo env_var: $env_var
env_var: MyEnvVariable
正如您从输出中看到的那样,运行 echo env_var: $env_var
的 shell 在其环境中具有变量 env_var
。
如果你想将环境变量导出到你调用make的shell,事情有点困难,因为正如ネロク所解释的,你不能直接从子进程导出环境变量(make
) 到其父进程(您从中调用 make
的 shell)。但是,如果您接受这样调用 make:
eval "$(make)"
那么这确实是可能的:只需在您的食谱中回显 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
注意 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 本身复杂计算的结果,否则获得相同结果的更自然的方法是在父 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
我正在尝试在 Makefile 中设置一个环境变量,因此它可以在另一个程序 运行ning 中使用 shell 作为 make
,但是在 make
有 运行.
更新:根据接受的评论答案,这是不可能的。
步骤:
- 运行
make test
设置环境:export TEST_ENV_ONE=OneString
- 运行 另一个程序,可以读取
TEST_ENV_ONE
试过这个:
不工作:
test:
export TEST_ENV_ONE=OneString
$(shell export TEST_ENV_TWO=TwoString)
之后这是空的:
echo $TEST_ENV_ONE
echo $TEST_ENV_TWO
你上面的export TEST_ENV_ONE=OneString
是运行宁在专用的shell。在其他 shell 实例中的后续命令 运行。因此,他们不继承环境变量TEST_ENV_ONE
.
您可以在 makefile 中使用顶级(即不在目标的配方中)export
directive:
export env_var := MyEnvVariable
.PHONY: all
all:
echo env_var: $$env_var
这样,变量 env_var
被导出到将执行配方的 shell。
如果您 运行 make
使用上面的 makefile:
$ make
echo env_var: $env_var
env_var: MyEnvVariable
正如您从输出中看到的那样,运行 echo env_var: $env_var
的 shell 在其环境中具有变量 env_var
。
如果你想将环境变量导出到你调用make的shell,事情有点困难,因为正如ネロク所解释的,你不能直接从子进程导出环境变量(make
) 到其父进程(您从中调用 make
的 shell)。但是,如果您接受这样调用 make:
eval "$(make)"
那么这确实是可能的:只需在您的食谱中回显 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
注意 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 本身复杂计算的结果,否则获得相同结果的更自然的方法是在父 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