【问题标题】:make: Execution of multiple targets dependant on first one stops after making the first targetmake: 依赖于第一个目标的多个目标的执行在创建第一个目标后停止
【发布时间】:2018-12-09 16:18:33
【问题描述】:

我想用 make 做以下“东西”:

  1. 将文件夹中的 .pdf 文件转换为 .txt 文件,但重命名其扩展名 从 .txt 到 .textfile
  2. 删除其页码并将其另存为.nopage 文件。
  3. 将所有 .nopage 文件保存到存档中。档案名称应为archive.tgz

我的目标是:textfilenopagearchiveall 应该和 archive 做同样的事情。

这是我的代码:

SRC=$(wildcard *.pdf)
OBJ=$(SRC:.pdf=.converted)

.PHONY: all archive trimmed converted search-% help clean

.SECONDARY:

help:
    @echo "$$helptext"

textfile: $(OBJ)                        
%.textfile: %.pdf
    pdftotext $< $@

nopage: textfile
%.nopage: %.textfile
    @cat $< | sed 's/Page [0-9]*//g' > $@

archive: nopage
archive.tgz: %nopage
    tar -c --gzip $@ $<

all: archive

使用变量SRCOBJ,我收集了所有的.pdf 文件名,以便textfile 可以从“第一个操作”开始,以下目标都依赖于此。 但是,我的 Makefile 在执行textfile 后停止。因此,如果我调用“make nopage”,它只会创建 .textfile 文件并停在那里。后面的目标也一样。

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: makefile dependencies chain targets


    【解决方案1】:

    看看这两条规则:

    nopage: textfile
    %.nopage: %.textfile
        @cat $< | sed 's/Page [0-9]*//g' > $@
    

    是的,这是两条规则。第一个是您调用的那个;它的目标是nopagee,它有textfile 作为先决条件,它没有命令。所以在 make 执行了textfile 规则之后,is 就不再执行了。模式规则 (%.nopage: ...) 根本不会执行。

    这是一个很好的方法:

    NOPAGES := $(patsubst %.pdf, %.nopage, $(SRC))
    # or you could use NOPAGES := $(SRC:.pdf=.nopage)
    
    .PHONY: nopages
    nopages: $(NOPAGES)
    %.nopage: %.textfile
        @sed 's/Page [0-9]*//g' $< > $@
    

    并且一定要更改存档规则:

    archive.tgz: $(NOPAGES)
        tar -c --gzip $@ $^
    

    注意在最后一个命令中使用$^ 而不是$&lt;。另请注意,我从您的 makefile 中复制了 tar 语法;我的 tar 版本不接受该命令,它需要在 "$@" 之前使用 "-f"。

    【讨论】:

    • 所以最后,我必须创建另一个变量?
    • @Two-Tu:你不必必须,你可以解决它,但我认为你会朝着错误的目标努力。如果创建变量可以使事情变得更简单,那么它没有错。
    • 谢谢!那样工作:)
    • 哦,顺便说一句,为什么tar 命令是$^ 而不是$&lt;
    • 参见gnu.org/software/make/manual/html_node/Automatic-Variables.html 变量$&lt; 仅扩展到first 先决条件。变量$^ 扩展为所有先决条件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多