如何在makefile配方中设置环境变量? [英] How to set environment variables in makefile recipe?

查看:92
本文介绍了如何在makefile配方中设置环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简化的Makefile:

Here is a simplified Makefile:

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

我假设"var"的值为"0 1 2 3 4",但输出为:

I suppose the value of "var" is "0 1 2 3 4", but the output is:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
               <--- NOTHING!!!

您可以看到最后一个回声是"NOTHING".怎么了?

As you can see the last echo is "NOTHING". What is wrong?

推荐答案

来自这里:

当需要执行配方来更新目标时,可以通过为配方的每一行调用一个新的子外壳来执行它们...

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...

请注意::这意味着设置外壳程序变量和调用诸如cd之类的设置每个进程本地上下文的外壳程序命令不会影响配方中的以下几行.如果要使用cd影响下一条语句,请将两个语句放在单个配方行中.然后make将调用一个shell来运行整行,然后shell将按顺序执行语句.

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配方中设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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