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

在手册中:

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:
$(info $(call func,1.c))

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

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

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

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...