Linux 驱动学习笔记-Makefile 字符串处理函数(二)
本系列均为陈皓所著的《跟我一起写makefile》的学习笔记,只是为了加深笔者记忆,如果读者想进一步学习可阅读此书
subst
调用:
$(subst from,to,text)
解析
-
名称:字符串替换函数
-
功能:把字符串 text 中的 from 字符串替换成 to 字符串
-
返回:函数返回被替换过后的字符串
-
注意事项:暂无
使用示例
foo :=i am working on the street
bar := $(subst ee,EE,$(foo))
all:
@echo $(foo)
@echo $(bar)
输出结果
i am working on the street
i am working on the strEEt
patsubst
调用
$(patsubst pattern,replacement,text)
解析
-
名称:模式字符串替换函数
-
功能:查找 text 中的单词(单词以空格 Tab 或 回车分隔)是否符合模式 pattern ,如果匹配的话,则以 replacement 替换。 pattern 可以包含通配符
%
,表示任意长度的字符串。通过 replacement 中也包含%
,那么 replacement 中的这个%
将是 pattern 中的%
所代表的字符串(可以用 \ 来转义,用\%
来表示真正的%
) -
返回:函数返回被替换过后的字符串
使用示例
foo :=x.c.c bar.c
bar := $(patsubst %.c,%.o,$(foo))
all:
@echo $(foo)
@echo $(bar)
输出结果
x.c.c bar.c
x.c.o bar.o
strip
调用
$(strip string)
解析
-
名称:去空格函数
-
功能:去掉 string 字符串中开头和结尾的空字符
-
返回:返回被去掉空格的字符串值
使用示例
$(strip a b c )
把字符串 a b c 开头和结尾的空格去掉
findstring
调用
$(findstring find,in)
解析
-
名称:查找字符串函数
-
功能:在字符串 in 中查找 find 字符串
-
返回:如果找到,那么返回 find 否则返回空字符串
使用示例
$(findstring a,a b c)
$(findstring a,b c)
第一个函数返回 a 字符串,第二个返回空字符串
filter
调用
$(filter pattern...,text)
解析
-
名称:过滤函数
-
功能:以 pattern 模式过滤 text 字符串中的单词,保留符合模式 pattern 的单词,可以有多个模式
-
返回:返回符合模式 pattern 的字符串
使用示例
sources := foo.c bar.c baz.s ugh.h
filtered := $(filter %.c %.s,$(sources))
返回值就是:foo.c bar.c baz.s
filter-out
调用
$(filter-out pattern..,text)
解析
-
名称:反过滤函数
-
功能 以 pattern 模式过来 text 字符串中的单词,去除符合 pattern 的单词,可以有多个模式
-
返回:不符合模式 pattern 的字符串
使用示例
objects=main1.o foo.o main2.o bar.o
mains=main1.o main2.o
$(filter-out $(mains),$(objects))
返回值是 foo.o bar.o
sort
调用
$(sort <list>)
解析
-
名称:排序函数
-
功能:给字符串 list 中的单词排序(升序)
-
返回:返回排序后的字符串
-
备注:sort 会去掉 list 中相同的单词
使用示例
$(sort foo bar lose)
返回值就是 bar foo lose
word
调用
$(word n,text)
解析
-
名称:取单词函数
-
功能:取字符串 text 中第 n 个单词(从1开始)
-
返回:返回字符串 text 中的第 n 个单词。如果 n 比 text 的单词数要大,那么返回空字符串
使用示例
$(word 2,foo bar baz)
返回值就是 bar
wordlist
调用
$(wordlist s,e,text)
解析
-
名称:取单词串函数
-
功能:从 text 中取出从 s 开始到 e 的单词串。s 和 e 是一个数字
-
返回:返回字符串 text 中从 s 到 e 的单词字串。如果 s 比 text 中的单词数要大,那么返回空字符串。如果 e 大于 text 的单词数,那么返回从 s 开始,到 text 结束的单词串
### 使用示例
$(wordlist 2, 3, foo bar baz)
返回值就是 :bar baz
words
调用
$(words text)
解析
-
名称:单词个数统计函数
-
功能:统计 text 字符串中的单词个数
-
返回:text 中的单词数
使用示例
$(words foo bar baz)
返回值为 3
firstword
调用
$(firstword text)
解析
-
名称:首单词函数
-
功能:取 text 中的第一个单词
-
返回:返回 text 中的第一个单词
使用示例
$(firstword foo bar baz)
返回值是 foo