问题描述
文档提供:
但是,以下makefile显示,只有将.EXPORT_ALL_VARIABLES
设为 phony 目标,然后再将 only 设为目标,它将对makefile产生所需的效果,即导出所有变量.
Makefile(版本1)为:
ifeq "$(MAKELEVEL)" "0"
foo=bar
.DEFAULT:;
all: .EXPORT_ALL_VARIABLES
@$(MAKE)
else
all:
@echo 'foo is: $(foo)'
endif
运行,我们得到:
make[1]: Entering directory '/home/myname'
foo is:
make[1]: Leaving directory '/home/myname'
Makefile(版本2)为:
ifeq "$(MAKELEVEL)" "0"
foo=bar
.DEFAULT:;
all: .EXPORT_ALL_VARIABLES
@$(MAKE)
# This line is added in THIS version.
.PHONY: .EXPORT_ALL_VARIABLES
else
all:
@echo 'foo is: $(foo)'
endif
运行,我们得到:
make[1]: Entering directory '/home/myname'
foo is: bar
make[1]: Leaving directory '/home/myname'
现在,这两个版本的makefile之间唯一的区别是,在第二个版本中,.EXPORT_ALL_VARIABLES
被设置为伪造.
为什么需要耐心"才能工作?
为什么需要耐心"才能工作?
不是.您没有将.EXPORT_ALL_VARIABLES
声明为目标,而是将其声明为先决条件:
all: .EXPORT_ALL_VARIABLES
这是先决条件,而不是目标.如果您将其声明为目标:
.EXPORT_ALL_VARIABLES:
然后它将起作用,而您不必将其声明为伪造.
一个更准确的问题是,即使未将.EXPORT_ALL_VARIABLES
声明为目标,为什么仍将其声明为假冒呢?之所以会发生这种情况,是因为即使没有明确提及被标记为假"的事物,也被假定为目标.这可能是错误,也可能不是错误,具体取决于您如何解释.PHONY
的意图.
您最近的问题似乎遵循一种模式:阅读文档,然后编写一个makefile,使其执行与文档中所述内容相似但不相同的操作,观察它是否无法按照说明进行操作,然后问为什么不这样做. /p>
The docs provides:
However, the following makefiles show that only by making .EXPORT_ALL_VARIABLES
a phony target, then and only then, will it have the desired effect on the makefile, i.e. to export ALL variables.
Makefile(version 1) is:
ifeq "$(MAKELEVEL)" "0"
foo=bar
.DEFAULT:;
all: .EXPORT_ALL_VARIABLES
@$(MAKE)
else
all:
@echo 'foo is: $(foo)'
endif
Running, we get:
make[1]: Entering directory '/home/myname'
foo is:
make[1]: Leaving directory '/home/myname'
Makefile(version 2) is:
ifeq "$(MAKELEVEL)" "0"
foo=bar
.DEFAULT:;
all: .EXPORT_ALL_VARIABLES
@$(MAKE)
# This line is added in THIS version.
.PHONY: .EXPORT_ALL_VARIABLES
else
all:
@echo 'foo is: $(foo)'
endif
Running, we get:
make[1]: Entering directory '/home/myname'
foo is: bar
make[1]: Leaving directory '/home/myname'
Now, the only difference between these 2 versions of makefile, is that in the 2nd version, the .EXPORT_ALL_VARIABLES
was made phony.
Why is the 'phoniness' needed in order to work?
Why is the 'phoniness' needed in order to work?
It's not. You didn't declare .EXPORT_ALL_VARIABLES
as a target, you declared it as a prerequisite:
all: .EXPORT_ALL_VARIABLES
That's a prerequisite, not a target. If you declare it as a target:
.EXPORT_ALL_VARIABLES:
then it will work and you won't have to declare it phony.
A more accurate question would be, why does declaring .EXPORT_ALL_VARIABLES
as phony work even though it's not declared as a target? It happens because things that are marked phony are assumed to be targets even if they're not explicitly mentioned as such. That may or may not be a bug, depending on how you interpret the intent of .PHONY
.
Your questions recently seem to follow a pattern: read the documentation, then write a makefile that does something similar to but not the same as what the documentation says, observe it doesn't work as described, then ask why not.
这篇关于.EXPORT_ALL_VARIABLES仅在设为"phony"时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!