Makfile总结( 二 )


$(<函数名> <函数参数>)或者将()替换为{}函数的调用规则如上图所示,函数参数用.隔开 。
字符串函数subst$(subst <from>,<to>,<text>)

  • 字符串替换函数 。
  • 表示文本,这个函数会将text当中是的字符串替换为 。
s = ii am learning makefiless = $(subst ii, you, $(s))main: demo.c echo demo echo $(s) echo $(ss)patsubst$(patsubst <pattern>,<replacement>,<text>)
  • pattern 表示第一个参数,用于表示如何对 text 进行匹配 。
  • replacement 表示第二个参数 表示如何对匹配的字符进行重写 。
  • patsubst在进行匹配替换的时候,会先将字符串text根据空格或者tab键或者回车换行符进行分割,然后一一的进行替换和匹配 。
s = a.c b.c d.c abc.c abo.css = $(patsubst %.c, %.o, $(s))main: demo.c echo demo echo $(s) echo $(ss)Strip$(strip <string>)主要功能去掉字符串 string 首尾的空格 。
findstring$(findstring <find>,<text>)这个函数的作用是从字符串当中寻找字符串,如果找到了字符串就返回字符串,否则返回空 。
filter$(filter <pattern...>,<text>)这是一个过滤函数,这个函数执行时,首先会根据空格或者tab键或者回车换行符进行分割,然后一一的进行filter函数的操作 。然后遍历每一个被分割出来的字符,如果不满足pattern的规则的话对应的字符就会被过滤掉 。
s = a.c abo.c s.o s.y x.o x.yss = $(filter %.c %.o, $(s))main: demo.c echo $(ss)filter-out这个函数和filter的用法是一样的只不过,作用刚好相反,filter是保存符合条件的字符串,filter-out是保存不符合条件的字符串 。
s = a.c abo.c s.o s.y x.o x.yss = $(filter-out %.c %.o, $(s))main: demo.c echo $(ss)sort这个函数主要是用于帮助字符串排序的,同时还会取出分割之后相同的字符串 。
s = g a b c d e fa a a ass = $(sort $(s))main: demo.c echo $(ss)word$(word <n>,<text>)这个功能很简单,返回当中第个字符 。
s = g a b c d e fa a a ass = $(word 1, $(s)) # 取出第一个字符main: demo.c echo $(ss)wordlist$(wordlist <start>,<end>,<text>)这个也是从字符串当中取出字符,是取出第个字符一直到第个字符 。
s = g a b c d e fa a a ass = $(wordlist 1, 5, $(s))main: demo.c echo $(ss)words统计单词的个数 。
s = 1 2 3 4 5ss = $(words $(s))main: demo.c echo $(ss)firstword这个函数主要是用于返回第一个字符串的 。
s = 1 2 3 4 5ss = $(firstword $(s))main: demo.c echo $(ss)文件函数dir与notdir函数dir函数主要书获取文件路径当中的目录部分,而notdir函数主要是获取文件路径当中文件名的部分
file = ./files/a.cfdir = $(dir $(file))nfdir = $(notdir $(file))main: demo.c echo $(fdir) echo $(nfdir)suffix函数这个函数主要是用于获取文件的后缀名 。
file = ./files/a.cfdir = $(dir $(file))nfdir = $(notdir $(file))name = $(suffix $(file))main: demo.c echo $(fdir) echo $(nfdir) echo $(name)basename这个函数用于获取文件路径当中除去后缀名的部分 。
file = ./files/a.cbase = $(basename $(file))main: demo.c echo $(base)addsuffix这个函数主要是给文件加上后缀的 。
file = ./files/a.cbase = $(addsuffix .c, $(file))main: demo.c echo $(base)addprefix这个函数的主要作用就是在字符串的前面加上一串字符 。
file = files/a.cbase = $(addprefix ./src/main/, $(file))main: demo.c echo $(base)

经验总结扩展阅读