How to write a Make rule for files of certain type in a directory tree?

倖福魔咒の 提交于 2019-12-12 19:14:51

问题


Imagine a directory tree (which might be more than one level deep) containing several Markdown files. A PDF version of each file exist in the same directory and must be updated each time the corresponding Markdown file is updated. What rule must be written in a single Makefile in the root directory of this tree to achieve this?

I am looking a for a solution where files can be added or removed from the directory tree without a need for updating the Makefile.

Assumptions:

  • all markdown files follow a certain pattern in their name; for example they end with a .md postfix.
  • GNU Make is being used.

回答1:


You can use $(shell find) to find files recursively. For example:

markdown := $(shell find . -name '*.md')

all: $(patsubst %.md, %.pdf, $(markdown))

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


来源:https://stackoverflow.com/questions/36246713/how-to-write-a-make-rule-for-files-of-certain-type-in-a-directory-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!