gpt4 book ai didi

makefile - 如何在makefile配方中设置环境变量?

转载 作者:行者123 更新时间:2023-12-04 13:53:20 25 4
gpt4 key购买 nike

这是一个简化的Makefile:

all:
@for (( i = 0; i < 5; ++i )); do \
var="$$var $$i"; \
echo $$var; \
done
@echo $$var

我假设“var”的值为“0 1 2 3 4”,但输出为:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
<--- NOTHING!!!

如您所见,最后一个回声是“NOTHING”。怎么了?

最佳答案

here:

When it is time to execute recipes to update a target, they are executed by invoking a new subshell for each line of the recipe...

Please note: this implies that setting shell variables and invoking shell commands such as cd that set a context local to each process will not affect the following lines in the recipe. If you want to use cd to affect the next statement, put both statements in a single recipe line. Then make will invoke one shell to run the entire line, and the shell will execute the statements in sequence.


请尝试以下方法:
all:
@for (( i = 0; i < 5; ++i )); do \
var="$$var $$i"; \
echo $$var; \
done; \
echo $$var

关于makefile - 如何在makefile配方中设置环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10161915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com