Sourav Ghosh
Sourav Ghosh

Reputation: 134346

.PHONY usage in makefile

While searching for the usage of .PHONY in makefile, i came accross this which says something like

.PHONY also allows you to have targets that do not have an associated rule

I put the example metioned in that post in a makefile and I ran that but it is not showing me any error as mentioned in the OP. Can someone please detail it?

The makefile

target1: dostuff

.PHONY: target2
target2: dostuff



dostuff:
        @echo "Stuff gets done!!!!"

O/P

[sourav@titan temp]$ make target1
Stuff gets done!!!!
[sourav@titan temp]$ make target2
Stuff gets done!!!!
[sourav@titan temp]$ make
Stuff gets done!!!!
[sourav@titan temp]$

As per the OP, make target1 should throw an error. Please enlight.

Some Info

[sourav@titan temp]$ uname -r
2.6.18-194.el5PAE
[sourav@titan temp]$ make --version
GNU Make 3.81
[sourav@titan temp]$

Upvotes: 5

Views: 6869

Answers (1)

Claudio
Claudio

Reputation: 10947

True. A .PHONY target can have only dependencies, without any rule. In this case, make will not execute any rule, but will check if the dependencies are satisfied (and, if not, will execute their rules).

Therefore, in your example, it is correct that both target1 and target2 call dostuff because it is a dependency.

Upvotes: 3

Related Questions