About phony target in Makefile
Before we talk about phony target in Makefile, let us take a rough look at the rules of Makefile.
target ... : prerequisites ...
command
The target is a ObjectFile or a executable file.
The prerequisite is the file on which the target depends or a rule defined below.
When both the file on which the target depends on and a rule defined below exist simultaneiously, Makefile will give priority to the former. And in such a situation, if we want to regard the rule as the prerequisite, we need to define it as a phony target.
We can clearly explain it with the code below.
1st: Note that there are three files and two rules in this code without any phony target.
all:demo1 demo2 demo3
echo this is a test
demo1:
echo I am demo1
demo2:
echo I am demo2
xxx@ubuntu:~/Desktop$ touch demo1 demo2 demo3
xxx@ubuntu:~/Desktop$ ls
demo1 demo2 demo3 Makefile
xxx@ubuntu:~/Desktop$ make all
echo this is a test
this is a test
2nd: Note that there are three files and two rules in this code with two phony targets.
all:demo1 demo2 demo3
echo this is a test
demo1:
echo I am demo1
demo2:
echo I am demo2
.PHONY:demo1 demo2
xxx@ubuntu:~/Desktop$ ls
demo1 demo2 demo3 Makefile
xxx@ubuntu:~/Desktop$ make all
echo I am demo1
I am demo1
echo I am demo2
I am demo2
echo this is a test
this is a test