我正在使用 GNU make 3.81。这是一个演示该问题的测试 makefile:
define BOZO
a$(1): b c
touch a$(1)
endef
$(foreach i,1 2 3,$(call BOZO,$(i)))
这里的想法是使用宏模板(BOZO)来生成遵循可预测模式的规则。
问题:当我在此 makefile 上运行 make 时,出现错误:
Makefile.fake:10: *** multiple target patterns. Stop.
(其中第 10 行是包含 foreach 的行)。
现在,我知道该错误通常表示什么。让我们看看该行通过使用扩展成什么info
将扩展发送到标准输出的函数。我将第 10 行更改为:
$(info $(foreach i,1 2 3,$(call BOZO,$(i))))
我跑:
$ make -n
a1: b c
touch a1
a2: b c
touch a2
a3: b c
touch a3
make: *** No targets. Stop.
请注意,“无目标”消息是预期的,因为 $(info ...) 函数计算结果为空,但会导致 makeprint生成的规则。
那么让我们运行这些规则吧?
$make -n > out.txt
make: *** No targets. Stop.
$make -f out.txt a1 a2 a3
touch a1
touch a2
touch a3
$
啊啊!规则运作良好。那么...是 make 中的错误,还是我的理解中的错误?
最后一条线索可能有助于诊断:如果我将 foreach 行更改为:
$(foreach i,1,$(call BOZO,$(i)))
(这样 foreach 只有一次迭代)
然后做
$make a1
我收到一个不同的错误:
make: *** No rule to make target `a1'. Stop.
我不知道有什么方法可以“看到”扩展$(foreach )
使看到除了$(info )
,而且它的输出是合法的,所以我很困惑。