英文:
Run a Make pattern rule on all matching files
问题
我有一个Makefile,一旦简化,看起来像这样:
%.pdf: %.md
pandoc $< -o $@
如何创建一个规则,使其对文件夹中的所有*.md文件运行此规则?
例如,我希望能够添加新的Markdown文件x.md、y.md和z.md,然后运行
make pdf
...以生成x.pdf、y.pdf和z.pdf,而无需更改Makefile或手动运行make x.pdf y.pdf z.pdf。
目前,我的解决方法是touch x.pdf y.pdf z.pdf,touch *.md,然后make *.pdf。
英文:
I have a Makefile which, once simplified, looks like this:
%.pdf: %.md
pandoc $< -o $@
How can I create a rule which runs this rule for all the *.md file present in the folder?
For instance I would like to be able to add new markdown files x.md, y.md and z.md, and then run
make pdf
... to produce x.pdf, y.pdf and z.pdf, without having change the Makefile or run make x.pdf y.pdf z.pdf manually.
At the moment, my workaround is to touch x.pdf y.pdf z.pdf, touch *.md, and make *.pdf.
答案1
得分: 2
MDS := $(wildcard *.md)
PDFS := $(patsubst %.md,%.pdf,$(MDS))
.PHONY: pdf clean
pdf: $(PDFS)
%.pdf: %.md
pandoc $< -o $@
clean:
rm -f $(PDFS)
"pdf" 和 "clean" 必须声明为 phony 目标,因为它们不是真实的文件。这是通过将它们添加为 .PHONY 特殊目标的先决条件来完成的。
英文:
MDS := $(wildcard *.md)
PDFS := $(patsubst %.md,%.pdf,$(MDS))
.PHONY: pdf clean
pdf: $(PDFS)
%.pdf: %.md
pandoc $< -o $@
clean:
rm -f $(PDFS)
The clean phony target is a bonus.
pdf and clean must be declared as phony because they are not real files. This is done by adding them as prerequisites of the .PHONY special target.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论