整理了一下Makefile中字符串的处理函数。
//因为尖括号在博文中无法显示,所以使用了双引号替换
1.字符串替换函数。
表达式:$(subst”from”,”to”,”text”)
函数功能:把字符串”text”中的”from”字符串替换成”to”
返回值:函数返回被替换后的字符串。该函数调用实例如下:
首先在Makefile文件中编辑下列代码:
result :=
all:
$(result) = $(subst a, A, how are you,China?)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : how Are you,ChinA?
2.模式字符串替换函数
表达式:$(patsubst”pattern”,”replacement”,”text”)
函数功能:查找”text”中的单词
返回值:函数返回被替换过后的字符串。实例如下:
result :=
all:
$(result) = $(patsubst %.mk, %.o, makefile.mk.mk a.mk)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : makefile.o a.o
3.去空格函数
表达式:$(strip”string”)
函数功能:去掉string字符串中开头和结尾的空字符
返回值:返回被去掉空格的字符串。实例如下:
result :=
all:
$(result) = $(strip hello world )#头尾各4个空格
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : hello world
4.查找字符串函数
表达式:$(findstring”find”,”in”)
函数功能:在字符串”in”中查找”find”字符串
返回值:如果找到指定的字符串,则返回”find”,否则,返回空字符串。实例如下:
result :=
all:
$(result) = $(findstrip h,hello)
echo -n "the result is:"
echo $(result)
$(result) = $(findstrip h,hello)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : h
the result is :
因为第一次返回‘h’,第二次由于‘h’已经出现过,所以第二次就找不到了。
5.过滤函数
表达式:$(filter”pattern...”,”text”)
函数功能:以”pattern”模式过滤”text”字符串中的单词,保留符合模式”pattern”的单词。可以有
多个模式
返回值:返回符合模式”pattern”的字符串。实例如下:将文件列表sources中的.c文件和.h文件过
滤掉
result :=
sources := a.c b.c c.c d.h e f
all:
$(result) = $(filter %.c %.h,$(sources))
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : a.c b.c c.c d.h
6.反过滤函数
表达式:$(filter-out”pattern...”,”text”)
函数功能:以”pattern”模式过滤”text”字符串中的单词,去除符合模式”pattern”的单词。可以有
多个模式
返回值:返回不符合模式”pattern”的字符串。实例如下:将文件列表sources中除了.c文件和.h外
的所有文件过滤掉
result :=
sources := a.c b.c c.c d.h e f
all:
$(result) = $(filter-out %.c %.h,$(sources))
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : e f
7.排序函数
表达式:$(sort”list”)
函数功能:给字符串”list”中的单词排序(升序)
返回值:返回排序后的字符串。实例如下:
result :=
all:
$(result) = $(sort hello world china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : china hello world
//sort函数会去掉字符串中相同的单词,例:
result :=
all:
$(result) = $(sort hello world hello china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : china hello world
8.取单词函数
表达式:$(word”n”,”text”)
函数功能:取字符串”text”中第”n”个单词
返回值:返回字符串中第n个单词。实例如下:
result :=
all:
$(result) = $(word 2,hello world china)
echo -n "the result is:"
echo $(result)
$(result) = $(word 5,hello world china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : world
the result is :
9.取单词函数
表达式:$(wordlist”s”,”e”,”text”)
函数功能:从字符串”text”中取从”s”开始到”e”的单词串,”s”和”e”是一个数字
返回值:返回取到的单词串。
result :=
all:
$(result) = $(wordlist 1,2,hello world china)
echo -n "the result is:"
echo $(result)
$(result) = $(word 3,2,hello world china)
echo -n "the result is:"
echo $(result)
$(result) = $(word 2,5,hello world china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : hello world
the result is :
the result is : world china
10.单词个数统计函数
表达式:$(words”text”)
函数功能:统计”text”字符串中单词个数
返回值:返回单词数
result :=
all:
$(result) = $(words hello world china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : 3
如何取文本中最后一个单词呢?实例:
result :=
all:
$(result) = $(word $(words hello world china))
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : china
11.首单词函数
表达式:$(firstword”text”)
函数功能:取字符串的第一个单词
返回值:返回第一个单词。
result :=
all:
$(result) = $(firstword hello world china)
echo -n "the result is:"
echo $(result)
.PHONY:all
执行该Makefile文件:
make -s all
the result is : hello
当然,使用word函数同样可以取第一个单词。
以上便是makefile中的字符串处理函数