Top Tweets for #MakefileMagic
π§ Demystifying Makefile's .PHONY: Why and When to Use It π #DevTips #CodingEssentials #MakefileMagic
In make, the .PHONY target is used to specify that a certain target is not associated with a file. This is useful because make is designed to build files from other files. By default, make uses the timestamps of files to determine whether they need to be rebuilt. If there's a file with the same name as a target, make can become confused and may not execute the target when expected.
The .PHONY declaration helps you avoid this issue.
Here are a few reasons to use .PHONY:
============================
=> Non-File Targets: In many makefiles, there are targets that do not produce a file. Common targets like clean, all, and install might not correspond to actual files, but are instead just commands you want to run.
=> Force Execution: Even if there is no dependency change, marking a target as .PHONY will ensure its commands get executed every time it's called.
=> Avoid File/Target Name Confusion: If by any chance a file with the same name as the target gets created, it would prevent make from running that target. Declaring the target as .PHONY avoids this problem.
Here's an example: (see attachment)
==============
In this example, all and clean are declared as .PHONY targets. This means:
=> When you run make or make all, it will always try to build myprogram, even if there's a file named all in the directory.
=> When you run make clean, it will always run the rm command, even if there's a file named clean in the directory.
=> It's a good practice to use .PHONY for any target that does not produce a file to avoid unexpected behaviors.

Trends for you
Most Popular Users

Elon Musk 
@elonmusk
241.5M followers

Barack Obama 
@barackobama
118.8M followers

Cristiano Ronaldo 
@cristiano
114.5M followers

Donald J. Trump 
@realdonaldtrump
111.9M followers

Narendra Modi 
@narendramodi
107.1M followers

Rihanna 
@rihanna
98.7M followers

NASA 
@nasa
92.3M followers

Justin Bieber 
@justinbieber
91.8M followers

KATY PERRY 
@katyperry
90M followers

Taylor Swift 
@taylorswift13
84M followers

Lady Gaga 
@ladygaga
75.5M followers

Virat Kohli 
@imvkohli
73.5M followers

Kim Kardashian 
@kimkardashian
70.9M followers

YouTube 
@youtube
68.8M followers

Neymar Jr 
@neymarjr
66.5M followers

Bill Gates 
@billgates
65.3M followers

Selena Gomez 
@selenagomez
63.1M followers

The Ellen Show
@theellenshow
62.2M followers

CNN 
@cnn
61.7M followers

X 
@x
60.7M followers
