运行一个Make模式规则在所有匹配的文件上。

huangapple go评论55阅读模式
英文:

Run a Make pattern rule on all matching files

问题

我有一个Makefile,一旦简化,看起来像这样:

%.pdf: %.md
	pandoc $< -o $@

如何创建一个规则,使其对文件夹中的所有*.md文件运行此规则?

例如,我希望能够添加新的Markdown文件x.mdy.mdz.md,然后运行

make pdf

...以生成x.pdfy.pdfz.pdf,而无需更改Makefile或手动运行make x.pdf y.pdf z.pdf

目前,我的解决方法是touch x.pdf y.pdf z.pdftouch *.md,然后make *.pdf

英文:

I have a Makefile which, once simplified, looks like this:

%.pdf: %.md
	pandoc $&lt; -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 $&lt; -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.

huangapple
  • 本文由 发表于 2023年5月24日 20:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定