Function 在Makefile函数中设置变量值时遇到问题

Function 在Makefile函数中设置变量值时遇到问题,function,makefile,wildcard,gnu,Function,Makefile,Wildcard,Gnu,目录结构(用于运行此示例)如下所示: example |-- Makefile |-- foo | |-- foo.h (empty file) |-- bar | |-- bar.h (empty file) TARGETS=foo bar define GEN_HEADER_RULE $(1)_INCS = $(wildcard $(1)/*.h) $(info $$($(1)_INCS) is [$($(1)_INCS)]) $(info $$(wildcard

目录结构(用于运行此示例)如下所示:

example
 |-- Makefile
 |-- foo
 |    |-- foo.h (empty file)
 |-- bar
 |    |-- bar.h (empty file)
TARGETS=foo bar

define GEN_HEADER_RULE
$(1)_INCS = $(wildcard $(1)/*.h)
$(info $$($(1)_INCS) is [$($(1)_INCS)])
$(info $$(wildcard $$(1)/*) is: [$(wildcard $(1)/*)])
endef

$(foreach t,$(TARGETS),$(eval $(call GEN_HEADER_RULE,$t)))

all: 
    @echo "DONE"
Makefile如下所示:

example
 |-- Makefile
 |-- foo
 |    |-- foo.h (empty file)
 |-- bar
 |    |-- bar.h (empty file)
TARGETS=foo bar

define GEN_HEADER_RULE
$(1)_INCS = $(wildcard $(1)/*.h)
$(info $$($(1)_INCS) is [$($(1)_INCS)])
$(info $$(wildcard $$(1)/*) is: [$(wildcard $(1)/*)])
endef

$(foreach t,$(TARGETS),$(eval $(call GEN_HEADER_RULE,$t)))

all: 
    @echo "DONE"
实际产出为:

$(foo_INCS) is []
$(wildcard $(1)/*) is: [foo/foo.h]

$(bar_INCS) is []
$(wildcard $(1)/*) is: [bar/bar.h]

DONE
$(foo_INCS) is [foo/foo.h]
$(bar_INCS) is [bar/bar.h]
预期产出为:

$(foo_INCS) is []
$(wildcard $(1)/*) is: [foo/foo.h]

$(bar_INCS) is []
$(wildcard $(1)/*) is: [bar/bar.h]

DONE
$(foo_INCS) is [foo/foo.h]
$(bar_INCS) is [bar/bar.h]
我无法理解为什么变量$(1)\u INCS的值没有被设置为通配符函数的输出?通配符函数显然可以正常工作,正如$info命令直接从中转储输出所示


我构建了这个简单的Makefile来演示我在更大的构建系统中遇到的相同问题。

您忘记了保护变量和函数不受双重扩展的影响。将所有
$
翻一番,除了
$(1)
,您应该可以看到几乎您想要的内容(除了第二个
$(信息…
,它将产生比您预期更多的输出)

使用
foreach eval call
时,请记住,当make将结果解析为make语法时,它会通过
eval call
进行第一次扩展,并通过make进行第二次扩展。您经常需要转义第一个扩展(
$$
),有时甚至转义第二个扩展(
$$$$
)。有关详细说明,请参阅