makefile中的伪目标,强制目标和双冒号规则

----伪目标----

target :

commands

如果makefile 所在目录没有target 同名文件:make target 则导致commands 总是被执行。

如果makefile 所在目录下存在target 同名文件:make target 则commands 不被执行,认为target 总是最新的。

 

.PHONY:target

target:

commands

不论makefile 所在目录下存不存在与target 同名文件,make target 导致commands 的执行,与上一种写法的区别是引用'.PHONY' 的意义所在。

 

-----强制目标-----

target:

因为没有命令,所以make target ,与makefile 所在目录下是否存在与target 同名的文件没有直接关系。

 

-----双冒号规则-----

target::

commands

无论makefile 所在目录下存不存在与target 同名文件,make target 导致commands 的执行,与使用'.PHONY' 定义的伪目标效果相同。


-----------------------------------------------------------------

----------1

.PHONY target

all:target

commands1

# 伪目标

target:

commands

----------2

all:target

commands1

# 伪目标

target:

commands

---------3

all:target

commands1

# 强制目标

target:

---------4

all:target

commands1

# 双冒号规则

target::

commands

 

1 和4make all 执行情况相同,无论makefile 所在目录下存不存在与target 同名文件,commands1 和commands 都被执行。

2 和3make all 执行情况在makefile 所在目录下存在与target 同名文件,commads 不被执行( 认为target 目标是最新的) ,commands1 将被执行;而如果不存在与target 同名的文件,则2 运行效果与1 和4 相同,3 运行将导致commands1 的执行。

 

>rm *

>vim Makefile

>cat -n Makefile

1 .PHONY:target1

2 all1:target1

3 echo "all1_target1_.PHONY_command_all1"

4 target1:

5 echo "all1_target1_.PHONY_command_target1"

6

7 all2:target2

8 echo "all2_target2_command_all2"

9 target2:

10 echo "all2_target2_command_target2"

11

12 all3:target3

13 echo "all3_target3_force_command_all3"

14 target3:

15

16 all4:target4

17 echo "all4_target4_::_command_all4"

18 target4::

19 echo "all4_target4_::_command_target4"

20


>make all1

echo "all1_target1_.PHONY_command_target1"

all1_target1_.PHONY_command_target1

echo "all1_target1_.PHONY_command_all1"

all1_target1_.PHONY_command_all1

>make all2

echo "all2_target2_command_target2"

all2_target2_command_target2

echo "all2_target2_command_all2"

all2_target2_command_all2

>make all3

echo "all3_target3_force_command_all3"

all3_target3_force_command_all3

>make all4

echo "all4_target4_::_command_target4"

all4_target4_::_command_target4

echo "all4_target4_::_command_all4"

all4_target4_::_command_all4

 

>touch target{1,2,3,4}

>ls

>make all1

echo "all1_target1_.PHONY_command_target1"

all1_target1_.PHONY_command_target1

echo "all1_target1_.PHONY_command_all1"

all1_target1_.PHONY_command_all1

>make all2

echo "all2_target2_command_all2"

all2_target2_command_all2

>make all3

echo "all3_target3_force_command_all3"

all3_target3_force_command_all3

>make all4

echo "all4_target4_::_command_target4"

all4_target4_::_command_target4

echo "all4_target4_::_command_all4"

all4_target4_::_command_all4

-----------

以上测试在GNU Make 3.81 版本进行

原文地址:https://www.cnblogs.com/hzcya1995/p/13318890.html