Gnu make:从subst定位到宏

问题描述

我想生成一个等效目标的列表:

TARGETLIST = a b c d e f 

define RULE
$(subst X,$(1),file_X.txt) $(subst X,otherfile_X.txt) &: # some dependency
    # some commands
endef

$(foreach _t,$(TARGETLIST),$(eval $(call RULE,$(_t))))

这不能满足我的要求:例如,如果要构建file_b.txt,则找不到配方。我该如何解决

解决方法

您的问题来自调用foreach-eval-call时宏中的双重扩展。您需要将$中的$(subst...$$(subst...的两倍。但是,为什么不简单地让call替代呢?

TARGETLIST = a b c d e f 

define RULE
file_$(1).txt otherfile_$(1).txt &: # some dependency
    # some commands
endef
$(foreach _t,$(TARGETLIST),$(eval $(call RULE,$(_t))))

call自动用$(1)循环中的当前RULE替换$(_t)宏中的foreach项。这就是call的目的。