LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   makefile (.PHONY) (https://www.linuxquestions.org/questions/programming-9/makefile-phony-297055/)

blackzone 03-03-2005 02:18 AM

makefile (.PHONY)
 
I saw alot of makefile which contain a target .PHONY. what it do?

Hko 03-03-2005 04:39 AM

Re: makefile (.PHONY)
 
Quote:

Originally posted by blackzone
I saw alot of makefile which contain a target .PHONY. what it do?
A regular makefile-rule looks like this:
Code:

FileToBuild : Other target this one depends on
<tab> Command that generates FileToBuild

These kind of rules with proper dependecy lists after the colon ( : ) make it possible for "make" to do as little work as possible to (re-)build the requested target (file) by checking the modifcation times of sources and targets.

Often rules are used that do not really builds/generates some file. E.g. when you do "make clean" you don't expect "make" to build a file called "clean". Still, "make" cannot guess that, so with a make-rule like this:
Code:

clean:
<tab> rm *.o

... "make" will think "rm *.o" is the command to rebuild a file called "clean" if the file is not there or if it's old.

Normally this will do what we want it to do. But imagine a person creates some file called "clean" by accident or for some reason. Then "make" will not do the cleaning up command (rm *.o) when "make clean" is run, because the file "clean" is "up to date".

Labeling the target "clean" as "phony" will prevent that. It tells "make" that "clean" is a phony target, i.e. not a real file that needs to be kept up to date.

While it may sound like a unlikely thing to happen, it could introduce hard to find errors when it does happen. When makefile's are used with a lot of (phony) targets changes these accindents will actually happen increase. And makefile generated by configure-scripts contain quite a lot of phony targets. Also. listing target as "phony" helps documenting/explaining the makefile itself.


All times are GMT -5. The time now is 11:04 PM.