define func1 @echo "my name is $(0)" endef define func2 @echo "my name is $(0)" @echo "Param => $(1)" endef
test : $(call func1) $(call func2, Kevin)
.PHONY : test define func1 @echo "my name is $(0)" endef define func2 @echo "my name is $(0)" @echo "Param 1 => $(1)" @echo "Param 2 => $(2)" endef # 調用 func1 ,返回值賦值給 var # 結果確是 fun1 多行變量的值 var := $(call func1) new := $(func1) test : @echo "var => $(var)" @echo "new => $(new)" $(call func1) # call 關鍵字調用函數 $(call func2,kevin,666) # func2 裏面給參數在上面給出
book@www.100ask.org:/mnt/hgfs/100ask$ make var => @echo my name is func1 new => @echo my name is my name is func1 my name is func2 Param 1 => kevin Param 2 => 666
$(call func1)
作的兩件事編程
$(0)
替換成 func1my name is func1
define 用於定義多行變量的,只不過這個多行變量能夠在 call 的做用下看成自定函數使用,這個使用有限制,必須在規則中使用。
所以對於 var := $(call func1)
這樣寫是不會啓動函數調用的結果的,僅僅把它func1 看成變量處理了 ubuntu
new 的值沒有函數名,說明 call 的做用將實參值替換到對應的位置上去,這裏就是 $(0)
函數
var := $(abspath ./) test : @echo "var => $(var)"
爲何自定義函數和預約義函數的調用形式徹底不一樣?spa
.PHONY : test define func1 @echo "my name is $(0)" endef func2 := @echo "my name is $(0)" test : $(call func1) $(call func2)
zhabin666@ubuntu:/mnt/hgfs/Fileshare_LinuxC$ make my name is func1 my name is
func2 不是 call 處理的範疇code
.PHONY : test define func1 @echo "My name is $(0)" endef define func2 @echo "My name is $(0)" endef var1 := $(call func1) var2 := $(call func2) var3 := $(abspath ./) var4 := $(abspath test.cpp) test : @echo "var1 => $(var1)" @echo "var2 => $(var2)" @echo "var3 => $(var3)" @echo "var4 => $(var4)"
makefile 中真正意義上的函數調用都只能是預約義的函數調用,而自定義的函數調用在本質上是不存在的,僅僅是模擬上的行爲blog