Target Makefile Dependence on Generated Files

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

Given the source tree and the Makefile below, when I run it, it takes two runs to complete the “build”, although everything was created in 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

to do

$ 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 is this happening?

+1
source share
1 answer

You need to make the catalog only for ordering , otherwise the targets will be redone every time the catalog is changed; during the second call is worknewer than work/foobecause it work/barwas created after work/foo, so the worktimestamp is newer than work/foo.

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

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

Or more succinctly

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

Source: https://habr.com/ru/post/1689610/


All Articles