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

标签 linux makefile

在手册中:

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/

相关文章:

c++ - 如何在 g++ 中链接同一库的不同版本?

c - 如何为 3 个不同的 c 文件创建一个 makefile

python - AWS Boto 连接仅适用于 Windows Python shell

c - 如果计数值大于它可以容纳的值,CPU 计时器如何工作?

C 生成文件,在 CCS 中, "The system cannot find the file specified"

检查和编译跨多个目录的更改依赖项?

c - Wine 可以与 Simulink Real-Time C API 一起使用吗?

linux - 使用 init.d 脚本启动命令和服务启动之间的区别

linux - 从目录读取文件的单个命令

gcc - Phusion 乘客 Ubuntu 11.10