DEV Community

Michele Caci
Michele Caci

Posted on

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

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
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

build:
        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.

Latest comments (4)

Collapse
 
verthais profile image
verthais

Why would you ever use make in modern project.... anything seems like a better choice?

Collapse
 
phantomwhale profile image
Ben Turner

Common build tool against multiple languages in a polyglot development environment?

Anything else would likely come with extra baggage / installation required. Being able to know you download any codebase, without even knowing the language or framework it's written in, and run make to get the codebase into a runnable state or make clean to remove all the transitory files and get back to a clean copy, can make that first engagement much easier.

If you have experience with the underlying language / framework, you can always sidestep make and run the underlying commands it's calling (rake, npm, yarn etc...) but providing a top level file that works for everyone, because everyone has make installed, is pretty powerful.

Plus if used for the right category of task, I'm not sure if other tools offer anything make doesn't already do effectively, with very little extra baggage than the commands (shell script or lang-specific tools) it wraps.

Collapse
 
faroque_eee profile image
Md Omar Faroque

Simple and easier to understand.

Collapse
 
jtbonhomme profile image
Jean-Thierry BONHOMME

Actualy the better and simplier explanation about .PHONY directive.