Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 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 Answer

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...