1. 程式人生 > >makefile之命令包&多行變量

makefile之命令包&多行變量

expand close 部分 code gin like clas pro 簡單使用

define&endef

1. 命令包(canned recipes)&多行變量(muti-line variables)

The define directive is followed on the same line by the name of the variable being
defined and an (optional) assignment operator, and nothing more. The value to give the
variable appears on the following lines. The end of the value is marked by a line containing
just the word endef

.Aside from this difference in syntax, define works just like any other
variable definition. The variable name may contain function and variable references, which
are expanded when the directive is read to find the actual variable name to use.

You may omit the variable assignment operator if you prefer. If omitted, make assumes
it to be ‘=’ and creates a recursively-expanded variable. When using a ‘+=’ operator, the value is appended to the previous value as with any other append operation: with a single space separating the old and new
values.

You may nest define directives: make will keep track of nested directives and report
an error if they are not all properly closed with endef. Note that lines beginning with
the recipe prefix character are considered part of a recipe, so any define or endef strings
appearing on such a line will not be considered make directives.

本質上就是通過define directive來定義一個變量,而這個變量包含一組命令,而這一組命令經常會被多個地方使用到,那麽通過對該變量的引用,就能相應召開這一組命令。就像c語言的函數一樣。也可以當作是makefile中“函數"的定義。需要註意的是該變量名不能與其他變量名沖突。

1.1 示例

1.1.1 命令包的簡單使用---自定義函數

all:                    
    @$(cmd)

define cmd
    echo "test define 1"
    echo "test define 2"
    echo "test define 3"
endef

cmd 是命令包的名字,在define 和 endef 間的部分即是命令主體。

技術分享圖片

從執行結果可以看出,命令包變量cmd被引用展開後,被執行。上圖中第一次make的結果中打印了命令本身,而第二次make的結果中只打印了命令結果。@符號表示不回顯命令本身。

makefile之命令包&多行變量