无法更新用作先决条件的变量
Cannot update variable used as prerequisites
我正在尝试更新一个变量,然后将其用作规则的先决条件
我的代码如下:
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
在此代码中,我打算首先将“所有”更改为 运行“update_variables”,其中更新 $(OBJ),然后更新 compile_obj,其中 $(OBJ)用作先决条件
“make all”后,我的终端上打印出以下行:
rule: update_variables
updating OBJ
rule: compile_obj
OBJ seen from inside rule is updated
OBJ seen as prerequisite is origin
rule: all
在我看来,规则顺序是我想要的,但 $(OBJ) 值在规则“compile_obj”中出乎意料:
在食谱中,$(OBJ) 被视为“已更新”,但 $^ 仍然是“原始”,而我认为它们是相同的
我还在学习 make,能否请您指点一下为什么 $(OBJ) 的更新值在先决条件中看不到?
正如 MadScientist 提到的那样,先决条件在一开始就被扩展了,所以在食谱中更新它并没有真正起作用。我现在将关闭问题,谢谢大家:)
我正在尝试更新一个变量,然后将其用作规则的先决条件 我的代码如下:
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
在此代码中,我打算首先将“所有”更改为 运行“update_variables”,其中更新 $(OBJ),然后更新 compile_obj,其中 $(OBJ)用作先决条件
“make all”后,我的终端上打印出以下行:
rule: update_variables
updating OBJ
rule: compile_obj
OBJ seen from inside rule is updated
OBJ seen as prerequisite is origin
rule: all
在我看来,规则顺序是我想要的,但 $(OBJ) 值在规则“compile_obj”中出乎意料: 在食谱中,$(OBJ) 被视为“已更新”,但 $^ 仍然是“原始”,而我认为它们是相同的
我还在学习 make,能否请您指点一下为什么 $(OBJ) 的更新值在先决条件中看不到?
正如 MadScientist 提到的那样,先决条件在一开始就被扩展了,所以在食谱中更新它并没有真正起作用。我现在将关闭问题,谢谢大家:)