DEV Community


The meaning of the '.PHONY' target inside a Makefile

mcaci profile image Michele Caci ・1 min read

Inside the Makefile of a project you can find a .PHONY element followed by a label and then the same label used as a target for the Makefile.

For example:

.PHONY: build
        go build
Enter fullscreen mode Exit fullscreen mode

.PHONY is actually itself a target for the make commandand and it denotes labels that do not represent files of the project. What does it mean?

In the general case, assuming that there are no files called build in the project, when not using the .PHONY target, like so

        go build
Enter fullscreen mode Exit fullscreen mode

everytime the make build command is invoked it will execute the specified command (in this case go build). However, if there was a file named build, the make build would be bound by the presence of this file and executed only if that file was updated.

The .PHONY target overrides this last behaviour and let's the make build execute no matter of the presence of a file named build.

For a more in-depth overview head to the make command reference.

Discussion (1)

Editor guide
jtbonhomme profile image
Jean-Thierry BONHOMME

Actualy the better and simplier explanation about .PHONY directive.