make-在同一个eval调用中定义多个变量
我想使用make
的eval
函数在foreach
内定义几个(动态命名的)变量,但是我无法获得eval
来完成这项工作.
I would like to use make
's eval
function to define several (dynamically-named) variables inside a foreach
, but I can't get eval
to do this job.
我尝试过这样的事情:
$(eval \
var1 = val1 \
var2 = val2 \
)
它不起作用:var1
被定义为val1 var2 = val2
,而未定义var2
.这是有道理的,因为我在第二行的末尾放置了\
.但是,如果我删除它,则eval
调用将永远不会终止.
It doesn't work: var1
gets defined as val1 var2 = val2
and var2
is not defined. It makes sense, because I put \
at the end of the second line. But if I remove it, the eval
call will never be terminated.
我尝试了不同的方法来使此\
只被eval
看到,但是没有任何窍门.因此,问题是:是否可以在同一eval
调用中定义多个变量?
I tried different things to have this \
only seen by eval
, but nothing did the trick. Hence the question: is it possible to define multiple variables in the same eval
call ?
我当然可以打eval
两次...这很好奇.
Of course I could call eval
twice... it's rather curiosity.
分隔每个变量定义的是换行符,您要用反斜杠将其转义.由于您不能将其直接放在eval
函数中,因此必须定义它并将其用于eval
,如下所示:
What separates each variable definition is the newline character, which you are escaping with the backslash. Since you cannot put it directly in the eval
function, you have to define it and to use it into eval
like this :
define newline
endef
然后,如果将以下行放置在目标中:
Then if you place the following lines inside a target :
$(eval FOO=abc$(newline)BAR=def)
@echo FOO : $(FOO) BAR : $(BAR)
您将得到以下结果:
FOO:abc BAR:def
FOO : abc BAR : def
请注意,如果要在第二个变量的定义中使用变量,则必须像这样转义$字符:
Note that if you want to use a variable in the definition of the second one you have to escape the $ character like this :
$(eval FOO=abc$(newline)BAR=def$$(FOO))
@echo FOO : $(FOO) BAR : $(BAR)
结果最终将是:
FOO:abc BAR:defabc
FOO : abc BAR : defabc
这是由于在eval
实际上开始工作之前(在此处添加了换行符)之前存在第一级解释,但是我们希望仅在实数eval
工作,当定义了变量时,这就是为什么我们需要一个双$符号的原因.
This is due to the fact that there is a first level of interpretation before that eval
actually begins to do its work (the newline is added here) but we want the interpretation of the variable to occur only during the real eval
work, when the variable is defined, which is why we need a double $ symbol.