Makefile target dependency to generated files

旧时模样 提交于 2019-12-14 02:53:30

问题


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

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'.

Why does this happen?


回答1:


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

Or more concisely

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


来源:https://stackoverflow.com/questions/37738377/makefile-target-dependency-to-generated-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!