问题
I'm trying to have a makefile variable for the content of a directory after that directory has been updated by a recipe.
Why does this not work:
A_FILE = $(wildcard subdir/*)
all: a
@echo $(A_FILE)
a:
@mkdir ./subdir
@touch subdir/b
@touch a
$ rm -rf ./subdir && make
$
...whereas this does:
A_FILE = $(wildcard subdir/*)
all: a
@echo $(A_FILE)
a: subdir/b
@touch a
subdir/b:
@mkdir ./subdir
@touch subdir/b
$ rm -rf ./subdir && make
subdir/b
$
?
I thought lazy-evaluation meant the variable was not evaluated until actually used. In both versions, $(A_FILE)
is used in the same recipe, and after a prereq has been evaluated. In fact, I'd struggle to articulate a meaningful difference between the two rules, other than the superficial: the first is a chain of two rules/prereqs, and the second is a chain of three.
回答1:
You need to also delete a
:
$ rm -rf ./subdir a && make
Since you've deleted subdir
but not a
, the a:
rule isn't triggered. Only this rule runs:
all: a @echo $(A_FILE)
And since subdir
wasn't created, the $(wildcard subdir/*)
expansion is empty.
来源:https://stackoverflow.com/questions/64084461/what-makefile-lazy-evaluation-rule-governs-this-behavior