Thursday, January 15, 2009

Use of .PHONY targets in Makefiles, an example

Targets that do not represent files are known as phony targets. Examples are standard phony targets such as "clean", "all". It also makes a target always out of date.

It is important to note that make cannot distinguish between a file target and phony target. If by chance the name of a phony target exists as a file, make will associate the file with the phony target name in its dependency graph. If, for example, the file clean happened to be created running make clean would yield the confusing message:
$ make clean
make: `clean' is up to date.

Since most phony targets do not have prerequisites, the clean target would always be considered up to date and would never execute.

To avoid this problem, GNU make includes a special target, .PHONY, to tell make that a target is not a real file. Any target can be declared phony by including it as a prerequisite of .PHONY:
.PHONY: clean
clean:
rm -f *.o

Now make will always execute the commands associated with clean even if a file named clean exists.

Here is a simple Makefile which demonstrates the problem and solution
------ Makefile --------------------------
all: print
print:
cat clean
clean:
rm *.o
--------------------------------------------
If you just call make, it outputs the contents of "clean" file present in the current working directory.
For Ex:
[siddesh@jadoo phony]$ make
cat clean
This is a clean script

Suppose if you want to run clean target to remove .o files, then you need to run "make clean". But you woudn't be getting desired output. Lets see what it reports

[siddesh@jadoo phony]$ make clean
make: `clean' is up to date.

Corrected Makefile
------------------------------------------
all: print
print:
cat clean

.PHONY: clean
clean:
rm *.o
--------------------------------------------
Now if you try "make clean", it will call clean target to clean .o files
[siddesh@jadoo phony]$ make clean
rm *.o

3 comments:

Anonymous said...

[url=http://kfarbair.com][img]http://www.kfarbair.com/_images/_photos/photo_big7.jpg[/img][/url]

מלון [url=http://www.kfarbair.com]כפר בעיר[/url] - אינטימיות, [url=http://kfarbair.com/services.html]שקט[/url] . אנו מציעים שירותי אירוח מגוונים גם יש במקום שירות חדרים הכולל [url=http://www.kfarbair.com/eng/index.html]אחרוחות רומנטיות[/url] במחירים מיוחדים אשר יוגשו ישירות לחדרכם.

לפרטים נא גשו לעמוד המלון - [url=http://kfarbair.com]כפר בעיר[/url] [url=http://www.kfarbair.com/contact.html][img]http://www.kfarbair.com/_images/apixel.gif[/img][/url]

Anonymous said...

Hello everybody!

My name is Eva, I live in Scottsdale, AZ.

I am here to network / interact with other people for sharing knowledge, discussing ideas, seeking advice, find business partners & the like.

Regards,
Eva

Anonymous said...

Зачитываюсь в данный момент сборником стихов с замысловатым названием "Книга, из-за коей объединились поэты, объединить коих невполне вероятно".
Тут и классики, и новые имена: Окуджава, Ахмадуллина, Диана Арбенина и многие другие...
Каждый вечер, засыпая, читаю по стишку, и утром просыпаюсь, незамедлительно за книжку... И весь день будто летаю, счастливая)))) Может, в этом кроется секрет красоты, а, женщины?)))
Читайте стихи и будете такими же счастливыми и привлекательными, как я в данный момент!!
[url=http://www.2nt.ru/go/ruserotik.php]Молодежное видео[/url]
[url=http://2nt.ru/go/serial.php]Вообще любой сериал и особенно новинки я качаю тут [/url]
[url=http://2nt.ru/go/teets.php]Молодежное видео тут[/url]