首先应该知道在Makefile中函数的调用方式是这样子的,形如:
$(函数名 参数1,参数2,参数3,...)
$(funcanme arg1,arg2,arg3,...)
一定注意:函数名与参数之间用空格隔开,参数与参数之间用逗号隔开
1.subst :字符串替换函数
$(subst src,dst,input):将input字符串中的src子串替换为dst子串,返回替换后的字符串
comma:=,
colon:=:
foo:=/usr/bin,/usr/local/bin,/usr/sbin
$(subst $(comma),$(colon),$(foo)) 则返回:/urs/bin:/usr/local/bin:/usr/sbin
2. patsubst:模式字符串替换函数
$(patsubst %.c,%.o,input):将input中符合%.c模式的子串替换成%.o,返回替换后的字符串
e.g: $(patsubst %.c,%.o,main.c test.c)
3. strip:去掉字符串开头,结尾空格,以及中间的多余空格(中间如果有多个空格,则会保留一个空格)
$(strip " a b c ") 则返回:a b c
4. findstring 查找字符串函数
$(findstring str input):在 input 字符串中查找 str 字符串,如果找到则返回 str,否则返回空
e.g $(findstring hello,hello world) 则返回 hello
5. filter:过滤掉符合模式的字符串(可以有多个模式)
$(filter patterns,input):从input中过滤符合模式 pattern 的字符串
e.g $(filter %.c %.cpp,test.c test.cpp test.o) 则返回 test.o
6. filter-out:反过滤函数(保留符合模式的字符串)
$(filter-out patterns,input):从input中保留符合模式 pattern 的字符串
e.g $(filter-out %.c %.cpp,test.c test.cpp test.o) 则返回 test.c test.cpp
7. sort:排序函数
$(sort input-list):会按照字典序给input-list中的单词进行排序,会去除重复单词,默认升序
e.g $(sort world hello one person) 则返回 "hello one person world"
8. word :取单词函数
$(word index input-list):从input-list中取出第index个单词,index从1开始
e.g $(word 2,hello world one person) 则返回 "world"
9. wordlist :取单词串函数
$(wordlist From,To, input-list):从input-list中取出从From到To的单词串
e.g $(wordlist 2,4,hello world one person) 则返回 "world one person"
10. words:单词个数统计函数
$(words input-list):统计input-list中总共有多少个单词
e.g $(words hello world one person) 则返回 "4"
11. firstword:取字符串中的首个单词函数
$(firstword input-list):从input-list中取出第一个单词
e.g $(firstword,hello world one person) 则返回 "hello"
12. dir :取目录函数
$(dir inputfiles...):从输入的文件路径中取出目录部分,最后一个反斜杠"/"之前的部分,如果输入是文件没有反斜杠"/",则返回"./"
e.g $(dir /usr/local/test.cpp test.h) 则返回"/usr/local/ ./"
13. notdir:取文件函数
$(notdir inputfiles...):从输入的文件路径中取出非目录部分,最后一个反斜杠"/"之后的部分,则返回"./"
e.g $(notdir /usr/local/test.cpp test.h) 则返回"test.cpp test.h"
14. suffix:取后缀函数
$(suffix inputfiles...):返回输入的每个文件的后缀名,如果没有后缀,则返回空
e.g $(suffix /usr/local/test.cpp test.h testexe) 则返回".cpp .h"
15. basename:取前缀函数
$(basename inputfiles...):返回输入的每个文件的前缀,如果没有前缀,则返回空
e.g $(basename /usr/local/test.cpp test.h testexe) 则返回 "/usr/local/test test testexe"
16. addsuffix:添加后缀函数
$(addsuffix suffix,inputfiles...):给输入的每个文件添加后缀
e.g $(addsuffix .o, /usr/local/test.cpp test.h testexe) 则返回"/usr/local/test.cpp.o test.h.o testexe.o"
17. addprefix:添加前缀函数
$(addprefix prefix,inputfiles...):给输入的每个文件添加前缀
e.g $(addprefix /mnt/test/, test.cpp test.h testexe) 则返回"/mnt/test/test.cpp /mnt/test/test.h /mnt/test/testexe"
18. join:连接函数
$(join inputlist1...,inputlist2...):将inputlist2中的字符串对应的追加到inputlist1中的字符串后面并返回,如果inputlist1中的字符串个数多,那么多余的将保持原样输出;如果inputlist2中的字符串个数多,则将多余的字符串复制到inputlist1中并输出
list1:= /usr/local/ /mnt/test/ targetfile
list2:= bin test.cpp
e.g $(join $(list1), $(list2))
则返回 /usr/local/bin /mnt/test/test.cpp targetfile
e.g $(join $(list2), $(list1))
则返回 bin/usr/local/ test.cpp/mnt/test/ targetfile
以上则记录了Makefile中能常用到的一些函数用法及示例。