Next: Force Targets, Previous: Directory Search, Up: Rules
A phony target is one that is not really the name of a file. It is just a name for some commands 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 commands will not create the target file, the commands will be executed every time the target comes up for remaking. Here is an example:
clean: rm *.o temp
Because the rm
command does not create a file named clean,
probably no such file will ever exist. Therefore, the rm
command
will be executed every time you say `make clean'.
The phony target will cease to work if anything ever does create a file
named clean in this directory. Since it has no prerequisites, the
file clean would inevitably be considered up to date, and its
commands would not be executed. To avoid this problem, you can explicitly
declare the target to be phony, using the special target .PHONY
(see Special Built-in Target Names) as follows:
.PHONY : clean
Once this is done, `make clean' will run the commands regardless of whether there is a file named clean.
Since it knows that phony targets do not name actual files that could be
remade from other files, make
skips the implicit rule search for
phony targets (see Implicit Rules). This is why declaring a target
phony is good for performance, even if you are not worried about the
actual file existing.
Thus, you first write the line that states that clean
is a
phony target, then you write the rule, like this:
.PHONY: clean clean: rm *.o temp