makefile 常用函数

调用

shell

${shell cmd}

path = $(shell pwd)
all: 
	@echo $(path)
----
nli@nli-lab:~/Documents/makefile$ make
/home/nli/Documents/makefile

字符串

findstring

$(findstring <find>,<in>)
在字串 中查找 字串,如果找到,那么返回,否则返回空字符串。

text = 'Hello world!'
str = $(findstring He, $(text))
all: 
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
He
strip

$(strip <string>)
去掉< string >字串中开头和结尾的空字符,并将中间的多个连续空字符(如果有的话)合并为一个空字符。空字符包括[ 空格,tab等] 不可显示的字符。

text = '    Hello  world    !'
str = $(strip $(text))
all: 
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
 Hello world !
sort

$(sort <list>)
给字符串< list >中的单词排序(升序),去掉< list > 中相同的单词。

text = a.c a.a a.c b.c c.h 
str = $(sort $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
a.a a.c b.c c.h

筛选替换

filter

$(filter <pattern...>,<text>)
以 <pattern…> 模式过滤 < text > 字符串中的单词,保留符合模式<pattern…> 的单词,可以
有多个模式;返回符合模式<pattern…> 的字串。

text = a.c a.s b.c c.h 
str = $(filter %.c %.h, $(text))
all: 
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
a.c b.c
filter-out

$(filter-out <pattern...>,<text>)
以 模式过滤 字符串中的单词,去除符合模式 的单词,可以有多个模式。

text = a.c a.s b.c c.h 
str = $(filter-out %.s b.c, $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
a.c c.h
wildcard

$(wildcard <pattern...>)
以 <pattern…> 模式匹配单词,保留符合模式<pattern…> 的单词,可以
有多个模式;返回符合模式<pattern…> 的字串。

└───subdir
    ├───main.c
    ├───config.h
    └───child.c
str = $(wildcard subdir/*.c)
all: 
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
subdir/child.c subdir/main.c
subst

$(subst <from>,<to>,<text>)
对于< text >字符串中每一个< from >字符(串),替换为< to >字符(串)。

text = a.c a.s b.c c.h 
str = $(subst a, start, $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
start.c start.s b.c c.h
patsubst

$(patsubst <pattern>,<replacement>,<text>)
以< pattern >模式匹配< text >中字符串(若多个字符串以空格分隔),使用< replacement >替换匹配的< text >,其余内容不变,< pattern >可以包括通配符%表示任意长度的字串。如果< replacement >中也包含%,则< replacement >中的这个%将是< pattern >中的那个%所代表的字符串。

str = $(wildcard subdir/*.c)
str := $(patsubst %.c, %.o, $(str))
all: 
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
subdir/child.o subdir/main.o

文件路径

notdir

$(notdir <names...>)
去掉< names… >文件的路径,只保留文件名称。

└───subdir
    ├───main.c
    ├───config.h
    └───child.c
str = $(wildcard subdir/*.c subdir/*.h)
all:
	@echo $(str)
	@echo $(notdir $(str))
----
nli@nli-lab:~/Documents/makefile$ make
subdir/child.c subdir/main.c subdir/config.h
child.c main.c config.h
dir

$(dir <names...>)
去掉< names… >文件的名称,只保留文件路径。

└───subdir
    ├───main.c
    ├───config.h
    └───child.c
str = $(wildcard subdir/*.c subdir/*.h)
all:
	@echo $(str)
	@echo $(dir $(str))
----
nli@nli-lab:~/Documents/makefile$ make
subdir/child.c subdir/main.c subdir/config.h
subdir/ subdir/ subdir/
suffix

$(suffix <names...>)
从文件名序列<names…> 中取出各个文件名的后缀。如果文件没有后缀,则返回空字串。

text = a.c a a.s b.c c.h 
str = $(suffix $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
.c .s .c .h
basename

$(basename <names...>)
从文件名序列<names…> 中取出各个文件名的前缀。如果文件没有前缀,则返回空字串。

text = src/a.c .a a.s src/b.c inc/c.h 
str = $(basename $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
src/a a src/b inc/c
addsuffix

$(addsuffix <suffix>,<names...>)
把后缀< suffix > 加到<names…> 中的每个单词后面。

text = a.c a.s b.c c.h
str = $(addsuffix .o, $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
a.c.o a.s.o b.c.o c.h.o
addprefix

$(addprefix <prefix>,<names...>)
把前缀< prefix > 加到<names…> 中的每个单词后面。

text = a.c a.s b.c c.h
str = $(addprefix path/, $(text))
all:
	@echo $(str)
----
nli@nli-lab:~/Documents/makefile$ make
path/a.c path/a.s path/b.c path/c.h

其它

origin

$(origin <variable>)
识别< variable >的输入来源,返回值分为[undefine, command line, environment, file, default, override, automatic]

all:
	@echo $(origin V)

nli@nli-lab:~/Documents/makefile$ make
undefined
----
V = 1
all:
	@echo $(origin V)

nli@nli-lab:~/Documents/makefile$ make
file
----
all:
	@echo $(origin V)

nli@nli-lab:~/Documents/makefile$ make V=1
command line
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怦然心动如往昔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值