tags:

views:

642

answers:

3

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?

+8  A: 

Sometimes you want make to just execute some commands, without being tied to some target filename. This is when you use a "phony target".

The document you linked to supplies the rationale:

There are two reasons to use a phony target: to avoid a conflict with a file of the same name, and to improve performance.

There's also a really nice tutorial explaining it here.

P.S. if you don't understand this feature, then you probably don't need it.

Eli Bendersky
I think I have some idea about it now (having gone through the link that you mentioned).btw, why is it called `"phony"`?
Lazer
@eSKay: 'why is it called 'phony'?' -- because it's not a real target. That is, the target name isn't a file that is produced by the commands of that target.
Bernard
@Lazer: I don't know if you're a native english speaker. I'm not. the word phony does not mean what it sounds like. http://en.wiktionary.org/wiki/phony says: Fraudulent; fake; having a misleading appearance.
Bahbar
+2  A: 

It is a build target that is not a filename.

JohnMcG
A: 

Let's assume you have 'install' target, which is a very common in makefiles. If you do NOT use PHONY, and the file named "install" exists in the same directory with Makefile, the "make install" will do NOTHING as it will be interpreted as "execute such and such rules to create the install file". Since the file is already there, and its dependencies didn't change, nothing will be done.

However if you make the install target PHONY, it will tell the make tool that the target is fictional, and that make should not expect it to create the actual file. Hence it will not check whether the "install" file exists, meaning a) its behavior will not be altered and b) extra stat() will not be called.

Generally all targets in your Makefile, which do not produce the file with the target name, should be PHONY. This typically includes all, install, clean, distclean and so on.

George Yunaev