最近更新一个项目的makefile文件,在添加一个makefile target的时候,发现总是出现“is up to date”的提示,书写语法无任何不妥,始终找不到原因,最后google了下,找到了解决方案。
在stackoverflow上,有人给出的问题原因如下:
Your have directories with the same name as your targets.
Since your targets don't depend on anything, they'll always be considered up to date.
可见这是makefile target和dir名字冲突造成的,解决方法则是借助.PHONY。
在GNU的官网上,是这么解释的:
A phony target is one that is not really the name of a file; rather it is just a name for a recipe to be executed when you make an explicit request. There are two reasons to use a phony target: to avoid a conflict with a file of the same name, and to improve performance.
If you write a rule whose recipe will not create the target file, the recipe will be executed every time the target comes up for remaking.
出处: http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
不难看出,GNU默认makefile target是一个文件,因此他会先检测同级目录下是否已存在这个文件,如果存在,则会abort掉make 进程,但目标不是文件的话,则会出现up to date的情况,这种情况需要.PHONY来避免问题的出现,phony的意思是“赝品”,在这里可以形象的理解成“不是文件”。