GNU make支持內置函式以及用戶自定義函式,下面結合例子簡單介紹一下,
gnu make版本: 4.1
一、用戶自定義函式
格式: $(call macro-name{, param1 ···})
決議: macro-name可以是任意宏或變數,macro-name之后是宏的引數,并以逗號為分隔符,
例子:
1 define test-call2 echo "call has two parameters: $1, $2"3 endef4 5 .PTHONY: simple-test6 simple-test:7 @$(call test-call,one,two)
運行結果:
make simple-test
call has two parameters: one, two
二、內置函式
字串函式
1、filter
格式: $(filter pattern ···text)
決議: filter函式會將text視為一系列被空格隔開的單詞,與pattern比較之后接著會回傳相符者,
例子:
words := GNU is not unix and linux is not unix.PTHONY: simple-testsimple-test: @echo words: $(words) @echo unix matches: $(filter unix, $(words))
運行結果:
make simple-test
words: GNU is not unix and linux is not unix
unix matches: unix unix
2、filter-out
格式: $(filter-out patern...,text)
決議:這個函式功能與filter剛好相反
例子:
words := GNU is not unix and linux is not unix.PTHONY: simple-testsimple-test: @echo words: $(words) @echo unix matches: $(filter-out unix, $(words))
運行結果:
make simple-test
words: GNU is not unix and linux is not unix
unix matches: GNU is not and linux is not
3、findstring
格式: $(findstring string...,text)
決議: 此函式將會在text里面搜索string,如果該字符被找到了,此函式就會回傳string,否則,它會回傳空值,
例子:
words := GNU is not unix and linux is not unix.PTHONY: simple-testsimple-test: @echo words: $(words) @echo unix matches: $(findstring unix, $(words))
運行結果:
make simple-test
words: GNU is not unix and linux is not unix
unix matches: unix
4、subst
格式: $(subst search-string,replace-string, text)
決議:這是一個不具通配符能力的”搜索和替換“函式,它最常被用來在檔案名串列將一個擴展名替換成另一個擴展名
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(subst .c,.o,$(sourcelist))
運行結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: GNU.o is.o not.o unix.o and.o linux.o is.o not.o unix.o
這可以將在soucelist里面所有出現.c字樣的地方都替換成.o,
5、pathsubst
格式: $(pathsubst search-pattern,replace-pattern,text)
決議: 這是一個具有通配符能力的”搜索和替換“函式,
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(patsubst %nix.c, UNIX,$(sourcelist))
運行結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: GNU.c is.c not.c UNIX and.c linux.c is.c not.c UNIX
6、words
格式: $(words text)
決議:此函式會回傳text中單詞的數量
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(words $(sourcelist))
運行結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: 9
7、words后面帶n
格式:$(words n,text)
決議: 此函式會回傳text中的第n個單詞,第一個單詞的編號為1,如果n的值大于text中單詞的個數,則此函式將會回傳空值,
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(words 3,$(sourcelist))
測驗結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: 9
沒有回傳預想的值,好奇怪,
8、firstword
格式: $(firstword text)
決議: 此函式會回傳text中的第一個單詞,
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(firstword $(sourcelist))
運行結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: GNU.c
9、wordlist
格式: $(wordlist start,end,text)
決議: 此函式會回傳text中范圍從start(含)到end(含)的單詞,
例子:
sourcelist := GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c.PTHONY: simple-testsimple-test: @echo sourcelist: $(sourcelist) @echo unix matches: $(wordlist 1,3,$(sourcelist))
運行結果:
make simple-test
sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
unix matches: GNU.c is.c not.c
時間關系,先介紹到這,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/40764.html
標籤:嵌入式
