Makefile:防止无限模式递归
Makefile: prevent infinite pattern recursion
我觉得这很简单
%.png: ../figs/%.png
convert $? -resize '40%' $@
也就是我想从"../figs/"中对应的图片生成这个目录下的图片.
但是,上面会导致无限的依赖链,因为 ../figs/foo.png
匹配 %.png
,因此 make 会尝试检查 ../figs/../figs/foo.png
,它匹配 %.png
,因此 make尝试 。 . .
最终,停止 "File name too long".
我一定是漏掉了什么。什么是干净的解决方案?
用空规则终止链
%.png: ../figs/%.png
convert $? -resize '40%' $@
../figs/%.png: ;
user657267的解决方案很完美。另一种选择是使用静态模式规则:
PNGS := $(patsubst ../figs/%.png,%.png,$(wildcard ../figs/*.png))
all: $(PNGS)
$(PNGS): %.png: ../figs/%.png
convert $< -resize '40%' $@
clean:
rm -f $(PNGS)
根据所有先决条件列表计算所有目标的列表有几个很好的副作用,例如添加 all
和 clean
目标的可能性。
上面的回答都很有意思。但是,我想提一下终端规则解决方案:
%.png:: ../figs/%.png
convert $? -resize '40%' $@
通过更改为双冒号::
,然后我们将先决条件标记为terminal:
One choice is to mark the match-anything rule as terminal by defining it with a double colon. When a rule is terminal, it does not apply unless its prerequisites actually exist. Prerequisites that could be made with other implicit rules are not good enough. In other words, no further chaining is allowed beyond a terminal rule.
注意:只适用于match-anything规则
- What are double-colon rules in a Makefile for?
- GNU make seems to ignore non-terminal match-anything rules for intermediate files
我觉得这很简单
%.png: ../figs/%.png
convert $? -resize '40%' $@
也就是我想从"../figs/"中对应的图片生成这个目录下的图片.
但是,上面会导致无限的依赖链,因为 ../figs/foo.png
匹配 %.png
,因此 make 会尝试检查 ../figs/../figs/foo.png
,它匹配 %.png
,因此 make尝试 。 . .
最终,停止 "File name too long".
我一定是漏掉了什么。什么是干净的解决方案?
用空规则终止链
%.png: ../figs/%.png
convert $? -resize '40%' $@
../figs/%.png: ;
user657267的解决方案很完美。另一种选择是使用静态模式规则:
PNGS := $(patsubst ../figs/%.png,%.png,$(wildcard ../figs/*.png))
all: $(PNGS)
$(PNGS): %.png: ../figs/%.png
convert $< -resize '40%' $@
clean:
rm -f $(PNGS)
根据所有先决条件列表计算所有目标的列表有几个很好的副作用,例如添加 all
和 clean
目标的可能性。
上面的回答都很有意思。但是,我想提一下终端规则解决方案:
%.png:: ../figs/%.png
convert $? -resize '40%' $@
通过更改为双冒号::
,然后我们将先决条件标记为terminal:
One choice is to mark the match-anything rule as terminal by defining it with a double colon. When a rule is terminal, it does not apply unless its prerequisites actually exist. Prerequisites that could be made with other implicit rules are not good enough. In other words, no further chaining is allowed beyond a terminal rule.
注意:只适用于match-anything规则
- What are double-colon rules in a Makefile for?
- GNU make seems to ignore non-terminal match-anything rules for intermediate files