我有这样的工作:
target1.PREREQUISITES = file11 file12 file13
target2.PREREQUISITES = file21 file22 file23
$(myDir)target1.sfx : $(target1.PREREQUISITES)
<same recipe here>
$(myDir)target2.sfx : $(target2.PREREQUISITES)
<same recipe here>这就是我想要做的,但它行不通:
target1.PREREQUISITES = file11 file12 file13
target2.PREREQUISITES = file21 file22 file23
$(myDir)%.sfx : $(%.PREREQUISITES)
<commun recipe here>它总是说没有什么可做的,因为目标是最新的。
我觉得问题可能是在每个制作阶段做了什么,我的意思是,首先做的是%或$。它能正常工作吗?
发布于 2017-07-18 14:28:53
谢谢user657267的回答,它确实有效,它给了我一个很好的线索。对于那些不太熟悉的人来说,请注意双美元的标志。我在这里包括我的答案,但是,因为它仍然使用%标志,这是我最初的问题的一部分,并且和你的问题一样有效。我正在使用以下解决方案。
.SECONDEXPANSION:
$(mydir)%.sfx: $$(%.PREREQUISITES)
echo $^我刚刚注意到,而且要注意的是,当使用辅助扩展make时并不会告诉您缺少的先决条件没有规则,相反,它显示了一条误导性消息,说明没有规则可以使目标。
发布于 2017-07-18 05:17:02
您需要像二次膨胀这样的东西才能工作,否则在模式替换之前就会对先决条件中的变量进行扩展,而且您还没有定义一个名为%.PREREQUISITES的变量。
.SECONDEXPANSION:
$(mydir)%.sfx: $$($$*.PREREQUISITES)
echo $^发布于 2017-07-18 06:15:15
user657267提出的第二个扩展非常有效。GNU还支持一种循环机制,您可以使用它以非常相似的形式实例化几个规则:
target1.PREREQUISITES = file11 file12 file13
target2.PREREQUISITES = file21 file22 file23
TARGETS = target1 target2
# $(1) is a parameter to substitute. The $$ will expand as $.
define MY_rule
$$(myDir)$(1).sfx : $$($(1).PREREQUISITES)
<same recipe here>
endef
$(foreach target,$(TARGETS),$(eval $(call MY_rule,$(target))))foreach遍历$(TARGETS)中的所有单词,并将当前单词分配给$(target)。call执行MY_rule的扩展,它用当前的$(target)值替换$(1),用$替换$$。eval作为规则实例化call展开的结果。例如,foreach的第一次迭代的结果是:
$(eval $(call MY_rule,target1))call将评估如下:
$(myDir)target1.sfx : $(target1.PREREQUISITES)
<same recipe here>eval将作为一项规则实例化它。重要的:不要忘记call执行第一个扩展。因此,如果您的<same recipe here>包含$符号,请不要忘记将它们加倍,除非它们通过call进行扩展是可以的。如果您的菜谱使用了shell变量,那么您甚至有可能最终得到类似于$$$$var的东西。
这种机制比第二次扩展更强大,更通用。它甚至可以使用多个参数来替代和嵌套循环:
target1.PREREQUISITES = file11 file12 file13
target2.PREREQUISITES = file21 file22 file23
TARGETS = target1 target2
DIRS = myDir
# $(1): target
# $(2): directory
define MY_rule
$(2)$(1).sfx : $$($(1).PREREQUISITES)
<same recipe here>
endef
$(foreach target,$(TARGETS),$(foreach dir,$(DIRS),$(eval $(call MY_rule,$(target),$(dir)))))甚至可以将foreach-eval-call嵌入到define-endef中。
target1.PREREQUISITES = file11 file12 file13
target2.PREREQUISITES = file21 file22 file23
TARGETS = target1 target2
DIRS = myDir
# $(1): target
# $(2): directory
define MY_rule_1
$(2)$(1).sfx : $$($(1).PREREQUISITES)
<same recipe here>
endef
# $(1): directory
define MY_rule_2
$$(foreach target,$$(TARGETS),$$(eval $$(call MY_rule_1,$$(target),$(1))))
endef
$(foreach dir,$(DIRS),$(eval $(call MY_rule_2,$(dir))))https://stackoverflow.com/questions/45156678
复制相似问题