About phony target in Makefile

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值