gpt4 book ai didi

linux - 如何在 makefile 中使用 eval 函数?

转载 作者:IT王子 更新时间:2023-10-29 00:10:02 25 4
gpt4 key购买 nike

在手册中:

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.

“扩大了两倍”让我很困惑。

例如,我创建了一个 makefile :

define func
tmp = $(OBJPATH)/$(strip $1)
objs += $$(tmp)
$$(tmp) : $2
gcc $$^ -o $$@
endef

all : foo

$(eval $(call func, foo, 1.c))

eval 函数将如何扩展?

最佳答案

最简单的理解方式就是把eval换成info:

$(info $(call func, foo, 1.c))

这会将第一次扩展的结果显示为输出,因此您可以看到 make 实际解析的内容。您没有提供 OBJPATH 变量的值,但如果它是 obj,那么在您的情况下,(调用函数的)第一次扩展会导致:

tmp = obj/foo
objs += $(tmp)
$(tmp) : 1.c
gcc $^ -o $@

然后 make 解析器将对其进行评估,并在此过程中再次扩展它,因此扩展了 $(tmp) 之类的内容。

关于linux - 如何在 makefile 中使用 eval 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435490/

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