Makefile中.PHONY的作用是什么? [英] What is the purpose of .PHONY in a makefile?

查看:141
本文介绍了Makefile中.PHONY的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.PHONY在Makefile中是什么意思?我已经经历过,但是它太复杂了

有人可以简单地向我解释吗?

解决方案

默认情况下,Makefile目标是文件目标"-用于从其他文件构建文件. Make假设其目标是文件,这使得编写Makefile相对容易:

foo: bar
  create_one_from_the_other foo bar

但是,有时您希望Makefile运行不代表文件系统中物理文件的命令.常见的目标干净"和全部"就是很好的例子.可能不是这种情况,但是您可能可能在主目录中有一个名为clean的文件.在这种情况下,Make会感到困惑,因为默认情况下clean目标将与此文件关联,而Make仅在该文件关于其依赖项似乎不是最新时才运行它.

这些特殊目标称为 phony ,您可以明确告知Make与文件无关,例如:

.PHONY: clean
clean:
  rm -rf *.o

现在,即使您有一个名为clean的文件,make clean也将按预期运行.

就Make而言,虚假目标只是始终过期的目标,因此,每当您问make <phony_target>时,它都会运行,而与文件系统的状态无关.常见的通常会发声的make目标是:allinstallcleandistcleanTAGSinfocheck.

What does .PHONY mean in a Makefile? I have gone through this, but it is too complicated.

Can somebody explain it to me in simple terms?

解决方案

By default, Makefile targets are "file targets" - they are used to build files from other files. Make assumes its target is a file, and this makes writing Makefiles relatively easy:

foo: bar
  create_one_from_the_other foo bar

However, sometimes you want your Makefile to run commands that do not represent physical files in the file system. Good examples for this are the common targets "clean" and "all". Chances are this isn't the case, but you may potentially have a file named clean in your main directory. In such a case Make will be confused because by default the clean target would be associated with this file and Make will only run it when the file doesn't appear to be up-to-date with regards to its dependencies.

These special targets are called phony and you can explicitly tell Make they're not associated with files, e.g.:

.PHONY: clean
clean:
  rm -rf *.o

Now make clean will run as expected even if you do have a file named clean.

In terms of Make, a phony target is simply a target that is always out-of-date, so whenever you ask make <phony_target>, it will run, independent from the state of the file system. Some common make targets that are often phony are: all, install, clean, distclean, TAGS, info, check.

这篇关于Makefile中.PHONY的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆