问题描述
我正在使用GNU make 3.81.这是一个演示该问题的测试文件:
I am using GNU make 3.81. Here is a test makefile that demonstrates the problem:
define BOZO
a$(1): b c
touch a$(1)
endef
$(foreach i,1 2 3,$(call BOZO,$(i)))
这里的想法是使用宏模板(BOZO)生成遵循可预测模式的规则.
The idea here is to use a macro template (BOZO) to generate rules that follow a predictable pattern.
问题:当我在此makefile上运行make时,出现错误消息:
Problem: when I run make on this makefile I get an error saying:
Makefile.fake:10: *** multiple target patterns. Stop.
(其中第10行是带有foreach的行).
(where line 10 is the line with the foreach).
现在,我知道该错误通常表示什么.让我们看看通过使用info
函数将扩展发送到标准输出行会扩展到什么.我将第10行更改为:
Now, I know what that error normally indicates. Let's see what that line expands to by using the info
function to send the expansion to standard out. I change line 10 to be:
$(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 ...)函数的值为空,但会导致make对所生成的规则进行 print 打印.
Note that the "no targets" message is expected, since the $(info ...) function evaluates to empty but causes make to print the generated rules.
那我们去执行那些规则吧?
Let's run those rules then shall we?
$make -n > out.txt
make: *** No targets. Stop.
$make -f out.txt a1 a2 a3
touch a1
touch a2
touch a3
$
AAARGH!规则工作正常.那么...是制造还是我的理解中的错误?
AAARGH! The rules work fine. So... is the bug in make, or in my understanding?
最后一条可能有助于诊断的线索:如果我将foreach行更改为:
One final clue that might help diagnose: if I change the foreach line to:
$(foreach i,1,$(call BOZO,$(i)))
(这样foreach只有一个迭代)
(so that foreach has only one iteration)
然后做
$make a1
我收到另一个错误:
make: *** No rule to make target `a1'. Stop.
我不知道有什么办法可以看到" $(foreach )
的扩展,除了$(info )
以外,sees的输出是合法的,所以我很困惑.
I don't know of any way to "see" the expansion of $(foreach )
that make sees except for $(info )
, and its output is legal, so I'm quite stumped.
推荐答案
$(foreach i,1 2 3,$(eval $(call BOZO,$(i))))
告诉Make将结构解析为makefile语法, 颁布"他们.我不确定为什么Make会以这种特殊的方式反对不合规则的规则,但这是一种学术性的方法.
The eval function tells Make to parse the structures as makefile syntax, to "enact" them. I'm not sure why Make objected to the un-eval'd rules this particular way, but that's kind of academic.
这篇关于如何使用宏在foreach中生成多个Makefile目标/规则?神秘行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!