Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

gnu make - Cannot update variable used as prerequisites

I am trying to update a variable and then used it as prerequisite for a rule My code is as following:

OBJ:=origin
  
.PHONY: all update_variables compile_obj

all: update_variables compile_obj
        @echo "rule: $@"

update_variables:
        @echo "rule: $@"
        @echo "     updating OBJ"
        $(eval OBJ=updated)

compile_obj: $(OBJ)
        @echo "rule: $@"
        @echo "     OBJ seen from inside rule is $(OBJ)"
        @echo "     OBJ seen as prerequisite is $^" 

$(OBJ):
        @touch origin

in this code, I intend for "all" to run "update_variables" first, where $(OBJ) is updated and then compile_obj, where $(OBJ) is used as a prerequisite

After "make all", following lines printed out on my terminal:

rule: update_variables
     updating OBJ
rule: compile_obj
     OBJ seen from inside rule is updated
     OBJ seen as prerequisite is origin
rule: all

It seems to me that the rule order is what I wanted but $(OBJ) value is unexpected in rule "compile_obj": in the recipe, $(OBJ) is seen as "updated" but $^ is still "origin" while I though they would be the same

I'm still learning make, can you please kindly instruct me why updated value of $(OBJ) are not seen in prerequisite?

question from:https://stackoverflow.com/questions/65713599/cannot-update-variable-used-as-prerequisites

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As MadScientist mention,prerequisite is expanded at the very beginning, so updating it in recipes dont really work. I will close the question now, thank you all :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...