拾遗是自己平时查阅另一个资料,然后引申出来的知识了解,答应自己,写完这个赶紧去睡觉咯,明早还有大物课

PHONY的用法以及意义:以下内容来自于stackoverflow上面的一个问题的翻译以及对一些博客的总结和自己的一些理解https://stackoverflow.com/questions/2145590/what-is-the-purpose-of-phony-in-a-makefile

这是大牛的原回答,我把大概意思精简一下:

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: barcreate_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: allinstallcleandistcleanTAGSinfocheck.

就是说makefile认为你写一个命令,比如说是clean,是一个目标文件,这样Make这个命令会认为clean是一个文件,这样让编写makefile更加的容易,但是有个问题,如果说和你的makefile同级目录下还有一个clean的文件,那么你去make clean就会失败,这时候你就需要用PHONY,说明这个是一个假的目标文件,这样你的make clean 就会成功执行,否则就会弹出 makefile is

up to date,你需要使用PHONY 的场合还有:allinstallcleandistcleanTAGSinfocheck.这些时候

a phony target is simply a target that is always out-of-date 这句话很经典啊,一个phony的目标文件就像是一个过时的目标文件

有个人提出了一个问题:So, if a: bb: cc: ph where ph is .PHONY, you ask for a, and b is up-to-date, your comment implies that ph will be run. In fact, none of abc can ever be up-to-date, actually.

这个东西我会这周周末的时候试一下,因为要准备概率论的考试,累的一匹。。。

有这样一个例子大家可以参考:

NOTE: The make tool reads the makefile and checks the modification time-stamps of the files at both the side of ':' symbol in a rule.

Example

In a directory 'test' following files are present:

prerit@vvdn105:~/test$ ls
hello  hello.c  makefile

In makefile a rule is defined as follows:

hello:hello.ccc hello.c -o hello

Now assume that file 'hello' is a text file containing some data, which was created after 'hello.c' file. So the modification (or creation) time-stamp of 'hello' will be newer than that of the 'hello.c'. So when we will invoke 'make hello' from command line, it will print as:(假设hello这个file包含一些数据,)

make: `hello' is up to date.

Now access the 'hello.c' file and put some white spaces in it, which doesn't affect the code syntax or logic and then save and quit. Now the modification time-stamp of hello.c is newer than that of the 'hello'. Now if you invoke 'make hello', it will execute the commands as:(这段和上面那段的大概意思就是你本来有一个hello的文本文件,但是你gcc之后生成了一个hello的可执行文件,这个就会覆盖原本的hello文本文件)

cc hello.c -o hello

And the file 'hello' (text file) will be overwritten with a new binary file 'hello' (result of above compilation command).

If we use .PHONY in makefile as follow:

.PHONY:hellohello:hello.ccc hello.c -o hello

and then invoke 'make hello', it will ignore if any file present in the pwd named 'hello' and execute the command every time.

Now suppose if no dependencies of target is there in makefile:(假设他没有依赖文件)

hello:cc hello.c -o hello

and 'hello' file is already present in the pwd 'test', then 'make hello' will always show as:

make: `hello' is up to date.

还有一个例子,我会在周末的时候一并翻译,另外一个关于PHONY用法的范例:

There's also one important tricky treat of ".PHONY" - when a physical target depends on phony target that depends on another physical target:

TARGET1 -> PHONY_FORWARDER1 -> PHONY_FORWARDER2 -> TARGET2

You'd simply expect that if you updated TARGET2, then TARGET1 should be considered stale against TARGET1, so TARGET1 should be rebuild. And it really works this way.

The tricky part is when TARGET2 isn't stale against TARGET1 - in which case you should expect that TARGET1 shouldn't be rebuild.

This surprisingly doesn't work because: the phony target was run anyway (as phony targets normally do), which means that the phony target was considered updated. And because of that TARGET1 is considered stale against the phony target.

Consider:

all: fileallfileall: file2 filefwdecho file2 file1 >fileallfile2: file2.srcecho file2.src >file2file1: file1.srcecho file1.src >file1echo file1.src >>file1.PHONY: filefwd
.PHONY: filefwd2filefwd: filefwd2filefwd2: file1@echo "Produced target file1"prepare:echo "Some text 1" >> file1.srcecho "Some text 2" >> file2.src

You can play around with this:

  • first do 'make prepare' to prepare the "source files"
  • play around with that by touching particular files to see them updated

You can see that fileall depends on file1 indirectly through a phony target - but it always gets rebuilt due to this dependency. If you change the dependency in fileall from filefwd to file, now fileall does not get rebuilt every time, but only when any of dependent targets is stale against it as a file.

如果你想了解更多的关于PHONY的知识还是得浏览官方文档,周末我会浏览官方文档: https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html

PHONY的用法以及意义相关推荐

  1. python中return_Python 中return用法及意义

    1.python 中 return用法 The key word "return" which should be used only in a function in Pytho ...

  2. 详解rel=”nofollow”的用法与意义

    近年来在网站的链接中我们经常会看到类似rel="nofollow"或rel="external nofollow"的属性定义,有很多朋友并不明白它们的语义,今天 ...

  3. self的用法与意义(一)

    在烧掉脑子前,先温习一下最最基本的,self的意义. 如何理解class中的self ? 一般self作为第一个参数,但在SocketServer源代码中,居然有self作为最后一个参数的情况,如: ...

  4. C++中switch用法的意义

    switch,开关的意思,电路中单刀多掷的开关,开关所掷方向都是并列的,地位相同,即如同if-else中的多个if if {} else if{} else if{} //以上3个if地位相同,如同电 ...

  5. git tag的用法及意义

    git tag 介绍 命令是用来给当前项目状态(在某次commit后)打标签的,目的是便于以后将项目状态回滚到当时打标签的状态.可以把它与虚拟机的snapshot(快照)进行类比. 回想当时在看< ...

  6. super 的用法及意义

    super出现在继承了父类的子类中. 有三种存在方式: 第一种 super.xxx;(xxx为变量名或对象名) 这种方法意义为,获取父类中的名字为xxx的变量或方法引用. 使用这种方法可以直接访问父类 ...

  7. python中with open as f什么意思_Python中 with open(file_abs,'r') as f: 的用法以及意义

    Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题 ...

  8. python as f是什么意思_Python中 with open(file_abs,'r') as f: 的用法以及意义

    Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件读写,也需要注意编码问题 ...

  9. C#自学36一return的用法及意义return 0和return 1和return -1

    1.返回值int 类型的函数返回: return语句用来结束循环,或返回一个函数的值. return 0:一般用在主函数结束时,按照程序开发的一般惯例,表示成功完成本函数. return -1::表示 ...

最新文章

  1. mysql的索引的区别_MYSQL索引区别
  2. 安装rocketmq并配置管理界面
  3. php hsetnx,HSETNX命令_视频讲解_用法示例-redis编程词典-php中文网
  4. 向量交点坐标公式_高中数学必修1-5常用公式(定理)
  5. nvivo12安装包下载
  6. 只有一百行的xss扫描工具——DSXS源码分析
  7. extension(扩展)使用详情
  8. git报错warning: Clone succeeded, but checkout failed
  9. 求求你了!别瞎提交代码了,看人家 Git 提交规范那叫一个舒服!
  10. [转载]SAP生产工单结算的差异种类分析
  11. 打工人颤抖!蓝色光标宣布:全面用 AI 代替外包,股价一度飙涨 18%!
  12. 利用计算机本地文档重装系统,电脑如何用本地模式重装win10
  13. 音频质量评价体系那些事
  14. 计算机excel实发工资高中低,EXCEL表格中计算实发工资的公式
  15. vue-chartjs
  16. 数据库基础知识ACID,隔离级别RC,RR,RU,SERIALIZABLE,Phantom Rows幻读,解决幻读,脏读dirty read
  17. 谈一下3月22日线下肉山小课堂感受
  18. 听见丨HTC发布新款Vive Pro,采用高分辨率OLED屏并内置耳机 高通在CES上发布新芯片,可将蓝牙耳机续航延长3倍
  19. php 读取cad文件路径,cad备份文件在哪里找
  20. SAP中发票校验时贷方凭证和后续借贷的区别

热门文章

  1. 解决Selenium WebDriver 加载页面时出现浏览器闪退
  2. 【每日一题Day8】LC862和至少为K的最短子数组
  3. 深入了解zipline
  4. linux中ldconfig的使用介绍
  5. Python实现将列表按比例和数量拆分成子列表
  6. 人机混合智能中跨域的研究
  7. 希望计算机专业学生都知道这些宝藏老师
  8. 透过数据看国产CPU性能排行
  9. [转载]linux下的几个开源软件
  10. 为什么新版手机 QQ 2013 好友列表不再区分在线状态?