重复使用Makefile prerequesite中的百分号作为子目录名称

mat*_*fee 5 unix bash makefile

我想了解我的makefile目标的prequisite如何重新使用%符号,假设目标是X.pdf,与prerequesite是X/X.tex.

详细说来,我目前有一个像这样的makefile:

all: foo.pdf

%.pdf: %.tex
    pdflatex $*.tex
Run Code Online (Sandbox Code Playgroud)

我还有一个文件foo.tex,当我键入make它时,它将foo.pdf通过运行pdflatex foo.tex.

现在由于各种原因我无法控制,我的目录结构发生了变化:

my_dir
|- Makefile
|- foo
   |- foo.tex
Run Code Online (Sandbox Code Playgroud)

我想修改我的Makefile,这样当它尝试制作时X.pdf,它会查找该文件X/X.tex.

我尝试了以下(我试图把'%/%.tex'告诉它寻找foo/foo.tex):

all: foo.pdf

%.pdf: %/%.tex
    pdflatex $*/$*.tex
Run Code Online (Sandbox Code Playgroud)

但是,这会产生:

No rule to make target `foo.pdf', needed by `all'. Stop.
Run Code Online (Sandbox Code Playgroud)

那样做%.pdf: $*/$*.tex.

如果我改变它%/%.tex,foo/%.tex它按预期工作,但我不想硬编码foo在那里,因为在未来我会做all: foo.pdf bar.pdf,它应该寻找foo/foo.texbar/bar.tex.

我是相当新的Makefile文件(经验仅限于修改别人的我的需要),也从来没做过更比绝对基础之一,因此,如果任何人都可以给我一个指针,这将有助于(我真的不知道搜索什么词为使Makefile文件中-这看起来前途无量唯一的是%$*,我不能去上班).

wis*_*ent 2

您可以使用 VPATH 指定 make 应搜索的目录列表。

示例性的 makefile:

# Find all tex files
tex := $(shell find -iname '*.tex')

# Make targets out of them
PDFS := $(notdir $(tex:%.tex=%.pdf))
# specify search folder
VPATH := $(dir $(tex))

all : $(PDFS)

%.pdf : %.tex
        pdflatex $<
Run Code Online (Sandbox Code Playgroud)

或者更好的是使用 vpath (小写):

vpath %.tex $(dir $(tex))
Run Code Online (Sandbox Code Playgroud)

它只会在这些目录中查找 .tex 文件。