makefile中一个依赖关系的一个目标 [英] one target for one dependency in makefile

查看:152
本文介绍了makefile中一个依赖关系的一个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用make通过键入make all来生成照片的缩略图。如果缩略图尚未生成,则全部生成它们,否则将生成所修改照片的缩略图。为此,我需要一个目标(缩略图)每个依赖(照片)。我的代码是这样的:

  input = pictures / *。jpg 
output = $(subst图片, $(通配符$(输入)))
全部:$(输出)
echo缩略图生成!

$(输出):$(input)
echo转换...
convert -thumbnail 100 $(subst thumbs,pictures,$ @)$ @

如何修改它以获得所需的结果?

解决方案

您的问题是这一行

  $(输出):$ )

输出变量是每个输出文件。



输入变量是通配符模式。



每个输出目标的先决条件设置为通配符模式,这意味着如果任何文件更改每个输出文件将被看到需要重建。



此修复程序是使用 static pattern rule like this

  $输出):thumbs /%:图片/%

表示通过将它们与模式 thumbs /% $(output)中的所有文件c $ c>并使用匹配(称为)的部分在先决条件模式(图片/%)。



或者,您可以为每个文件构建一组特定的输入/输出匹配, / p>

  infiles = $(通配符图片/ *。jpg)
$(foreach文件,$(infiles) eval $(subst pictures /,thumbs /,$(file)):$(file)))

$(输出):
echo转换...
convert -thumbnail 100 $(subst thumbs,pictures,$ @)$ @

哪个使用 eval 函数为每个输入文件创建明确的 thumbs / file.jpg:pictures / file.jpg 目标/先决条件对。 / p>

I am trying to use make to generate thumbnails of photos by typing "make all". 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.

The input variable is the wildcard pattern.

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/%).

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.

这篇关于makefile中一个依赖关系的一个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆