makefile中的僞目標,強制目標和雙冒號規則

----僞目標----

target

commands

如果makefile 所在目錄沒有target 同名文件:make target 則導致commands 總是被執行。

如果makefile 所在目錄下存在target 同名文件:make targetcommands 不被執行,認爲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

 

14make all 執行情況相同,無論makefile 所在目錄下存不存在與target 同名文件,commands1commands 都被執行。

23make all 執行情況在makefile 所在目錄下存在與target 同名文件,commads 不被執行( 認爲target 目標是最新的)commands1 將被執行;而如果不存在與target 同名的文件,則2 運行效果與14 相同,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 版本進行

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章