Makefile目标对生成文件的依赖

Makefile目标对生成文件的依赖

本文介绍了Makefile目标对生成文件的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行依赖于生成文件的目标时,我对Make的行为有疑问.

I have a question regarding the behavior of Make when running targets that are dependent on generated files.

考虑到下面的源代码树和Makefile,即使我在第一次运行时生成了所有内容,当我运行它时也需要两次运行才能完成构建".

Given the source tree and Makefile below, when I run this it takes two runs to complete the "build" even though everything was generated on the first run.

$ ls -R
.:
bar  foo  Makefile

Makefile

all: foobar

work:
    mkdir -p work

work/foo: work foo
    cp foo work/foo

work/bar: work bar
    cp bar work/bar

foobar: work/foo work/bar

make

$ make
mkdir -p work
cp foo work/foo
cp bar work/bar
$ ls -R
.:
work/  bar  CMakeLists.txt  foo  Makefile

./work:
bar  foo
$ make
cp foo work/foo
$ make
make: Nothing to be done for 'all'.

为什么会这样?

推荐答案

您需要将目录设置为 仅订购 的先决条件,否则每次目录更改时都将重新建立目标;在第二次调用期间,workwork/foo更新,因为work/bar是在work/foo之后创建的,所以work的时间戳比work/foo更新.

You need to make the directory an order-only prerequisite, otherwise the targets will be remade each time the directory changes; during the second invocation work is newer than work/foo because work/bar was created after work/foo, so work's timestamp is newer than work/foo.

work/foo: foo | work
    cp foo work/foo

work/bar: bar | work
    cp bar work/bar

或更简洁

work/foo work/bar: work/%: % | work
    cp $< $@

这篇关于Makefile目标对生成文件的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:27