Linux 如何在makefile中使用eval函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10435490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How eval function can be used in makefile?
提问by Y.L.
In the manual:
在手册中:
The eval function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax.
It's important to realize that the eval argument is expanded twice; first by the eval function, then the results of that expansion are expanded again when they are parsed as makefile syntax. This means you may need to provide extra levels of escaping for “$” characters when using eval.
eval 函数非常特殊:它允许您定义新的非常量 makefile 结构;这是评估其他变量和函数的结果。eval 函数的参数被扩展,然后扩展的结果被解析为 makefile 语法。
重要的是要意识到 eval 参数被扩展了两次;首先通过 eval 函数,然后在解析为 makefile 语法时再次扩展该扩展的结果。这意味着在使用 eval 时,您可能需要为“$”字符提供额外的转义级别。
the "expanded twice" confuses me.
“扩大两次”让我感到困惑。
for example, i create a makefile :
例如,我创建了一个 makefile:
define func
tmp = $(OBJPATH)/$(strip )
objs += $$(tmp)
$$(tmp) :
gcc $$^ -o $$@
endef
all : foo
$(eval $(call func, foo, 1.c))
how will the eval function be expanded ?
eval 函数将如何扩展?
采纳答案by MadScientist
The easiest way to understand it is to replace the eval with info:
理解它的最简单方法是将 eval 替换为 info:
$(info $(call func, foo, 1.c))
That will display as output the result of the first expansion, so you can see what make will actually be parsing. You didn't provide the values for the OBJPATH variable, but if it was obj
for example then in your case the first expansion (of the call function) results in:
这将显示第一次扩展的结果作为输出,因此您可以看到 make 实际将解析的内容。您没有提供 OBJPATH 变量的值,但如果是obj
例如,那么在您的情况下,(调用函数的)第一次扩展会导致:
tmp = obj/foo
objs += $(tmp)
$(tmp) : 1.c
gcc $^ -o $@
Then the make parser will evaluate this, and in the process it will expand it again, so things like $(tmp)
are expanded.
然后 make 解析器会评估它,在这个过程中它会再次扩展它,所以像这样的东西$(tmp)
被扩展了。