简体   繁体   English

makefile中一个依赖项的一个目标

[英]one target for one dependency in makefile

I am trying to use make to generate thumbnails of photos by typing "make all". 我正在尝试使用make通过键入“全部制作”来生成照片的缩略图。 If the thumbnails are not yet generated make all generates them, else make all just generate the thumbnails of modified photos. 如果尚未生成缩略图,则让所有人都生成它们,否则使所有人都只生成修改后的照片的缩略图。 For this I need one target (thumbnail) for each dependency (photo) . 为此,我需要为每个依赖项(照片)提供一个目标(缩略图)。 My code is like this : 我的代码是这样的:

input = pictures/*.jpg
output = $(subst pictures,thumbs,$(wildcard $(input)))
all : $(output)
    echo "Thumbnails generated !"

$(output) : $(input)
    echo "Converting ..."
    convert -thumbnail 100 $(subst thumbs,pictures,$@) $@

How can I modify it to get the desired result ? 如何修改它以获得所需的结果?

Your problem is this line 你的问题是这条线

$(output) : $(input)

The output variable is the list of every output file. output变量是每个输出文件的列表。

The input variable is the wildcard pattern. input变量是通配符模式。

This sets the prerequisites of every output target as the wildcard pattern which means if any file changes every output file will be seen as needing to be rebuilt. 这将每个输出目标的先决条件设置为通配符模式,这意味着如果任何文件发生更改, 每个输出文件都将被视为需要重建。

The fix for this is to either use a static pattern rule like this 解决方法是使用这样的静态模式规则

$(output) : thumbs/% : pictures/%

which says to build all the files in $(output) by matching them against the pattern thumbs/% and using the part that matches % (called the stem ) in the prerequisite pattern ( pictures/% ). 它说通过将它们与模式thumbs/%进行匹配,并在先决模式( pictures/% )中使用与%匹配的部分(称为stem )来构建$(output)的所有文件。

Alternatively, you could construct a set of specific input/output matches for each file with something like 或者,您可以为每个文件构造一组特定的输入/输出匹配项,例如

infiles = $(wildcard pictures/*.jpg)
$(foreach file,$(infiles),$(eval $(subst pictures/,thumbs/,$(file)): $(file)))

$(output):
    echo "Converting ..."
    convert -thumbnail 100 $(subst thumbs,pictures,$@) $@

Which uses the eval function to create explicit thumbs/file.jpg: pictures/file.jpg target/prerequisite pairs for each input file. 它使用eval函数为每个输入文件创建显式thumbs/file.jpg: pictures/file.jpg目标/先决条件对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM