回显中的Makefile变量分配错误 [英] Makefile variable assignment error in echo

查看:57
本文介绍了回显中的Makefile变量分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文件目标

  foo:我= 1@回声$ {i} 

当我像这样运行make文件时:

  $ make foo 

i = 1
make:i:找不到命令
Makefile:2:目标'foo'的配方失败
make:[foo]错误127

但是,如果我不给作业分配空格(即)

  i = 1 

然后没有错误但没有输出,不会打印i的值

解决方案

首次尝试使用参数 = 2 i >.外壳程序中的适当赋值在等号的两边没有没有空格.

您的第二个问题是,在两条物理线上的配方将运行两个不相关的Shell实例.第一个将变量设置为值,然后退出并丢失该变量.第二个无关的实例不知道第一个实例是做什么的,当然也没有任何变量赋值的痕迹.解决此问题的方法是将这两条逻辑上合并为一行(只要它们之间有分号,您仍然可以将这些行拆分为几条物理行):

  foo:i = 1;\回声"$$ {i}" 

还请注意,为了防止 make 解释它们,我们需要如何将美元符号加倍.和在外壳中的字符串周围正确使用引号.(在这种特殊情况下,我们知道该字符串不包含任何shell元字符;但是许多初学者也偶然发现了此字符.)

GNU Make也允许您指定 .ONESHELL 强制在单个shell实例中全部评估配方中的命令;

.ONESHELL:foo:i = 1回声"$$ {i}" 

I have make file target like this

foo: 
    i = 1
    @echo $(i)

when I run the make file like this:

$ make foo 

i = 1
make: i: Command not found
Makefile:2: recipe for target 'foo' failed
make: [foo] Error 127

But if I do not give spaces in assignment (i.e)

i=1

Then no errors but no output, value of i is not printed

解决方案

The first attempt runs the command i with the parameters = and 2. A proper assignment in the shell has no spaces on either side of the equals sign.

Your second problem is that a recipe on two physical lines will run two unrelated shell instances. The first sets a variable to a value, then exits and loses the variable. The second, unrelated instance has no idea what the first did, and of course has no trace of the variable assignment. The fix for that is to merge the two into a single line logically (you can still split the lines over several physical lines as long as you have a semicolon between them):

foo: 
    i=1; \
    echo "$${i}"

Notice also how we need to double the dollar signs in order to prevent make from interpreting them; and the proper use of quotes around strings in the shell. (In this particular case we know the string doesn't contain any shell metacharacters; but many beginners stumble over this as well.)

GNU Make alternatively allows you to specify .ONESHELL which forces the commands in the recipe to be evaluated all in a single shell instance;

.ONESHELL:
foo: 
    i=1
    echo "$${i}"

这篇关于回显中的Makefile变量分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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