Makefile 文本处理函数(下)
更多资料下载,冲击月薪过万,一线经验工程师录制,
干货满满的嵌入式视频教程,请点击淘宝店:wanglitao.taobao.com
sort函数:单词排序
$(sort LIST)
sort函数对字符串LIST中的单词以首字母为准进行排序,并删除重复的单词。
.PHONY: all
FILE = a.c b.h c.s d.cpp
SRC = $(filter-out %.c, $(FILE))
all:
@echo "FILE = $(FILE)"
@echo "SRC = $(SRC)"
执行make,运行结果为:
# make
LIST = banana pear apple peach apple orange
STR = apple banana orange peach pear
word函数:取单词
word函数的作用是从一个字符串TEXT中,按照指定的数目N取单词:
$(word N,TEXT)
函数的返回值是字符串TEXT中的第N个单词。如果N的值大于字符串中单词的个数,返回空;如果N为0,则出错。
.PHONY: all
LIST = banana pear apple peach orange
word1 = $(word 1, $(LIST))
word2 = $(word 2, $(LIST))
word3 = $(word 3, $(LIST))
word4 = $(word 4, $(LIST))
word5 = $(word 5, $(LIST))
word6 = $(word 6, $(LIST))
all:
@echo "word1 = $(word1)"
@echo "word2 = $(word2)"
@echo "word3 = $(word3)"
@echo "word4 = $(word4)"
@echo "word5 = $(word5)"
@echo "word6 = $(word6)"
执行make,运行结果为:
# make
word1 = banana
word2 = pear
word3 = apple
word4 = peach
word5 = orange
word6 =
如果N的值为0,Makefile含有下面的语句:
word0 = $(word 0, $(LIST))
则会报错:
makefile:9: *** first argument to 'word' function must be greater than 0. Stop.
wordlist函数:取字串
wordlist函数用来从一个字符串TEXT中取出从N到M之间的一个单词串:
$(wordlist N, M, TEXT)
N 和 M都是从1开始的一个数字,函数的返回值是字符串TEXT中从N到M的一个单词串。当N比字符串TEXT中的单词个数大时,函数返回空。
.PHONY: all
LIST = banana pear apple peach orange
sub_list = $(wordlist 1, 3, $(LIST))
all:
@echo "LIST = $(LIST)"
@echo "sub_list = $(sub_list)"
执行make时,wordlist函数会将字符串LIST中的前三个单词赋值给sub_list:
# make
LIST = banana pear apple peach orange
sub_list = banana pear apple
words函数:统计单词数目
words函数用来统计一个字符串TEXT中单词的个数:
$(words TEXT)
words函数的返回值为字符串TEXT中单词的个数。
.PHONY: all
LIST = banana pear apple peach orange
all:
@echo "LIST = $(LIST)"
@echo "LIST len = $(words $(LIST))
执行make,运行结果为:
# make
LIST = banana pear apple peach orange
LIST len = 5
firstword函数:取首个单词
firstword函数用来取一个字符串中的首个单词。
$(firstword NAMES…)
$(word 1,TEXT)
firstword函数其实就相当于$(word 1,TEXT):
.PHONY: all
LIST = banana pear apple peach orange
all:
@echo "LIST = $(LIST)"
@echo "first word = $(firstword $(LIST))"
执行make,运行结果为:
# make
LIST = banana pear apple peach orange
first word = banana
《Makefile工程实践》视频教程,一线开发工程师独家录制,网上首家讲解Makefile的实战课程。从零开始,教你一步一步编写一个工程项目的Makefile,支持使用第三方静态库、动态库,支持指定模块或目录编译生成静态库、动态库,赠送企业级的Makefile模板,学完即可拿来使用,投入项目开发实战,具备独立开展项目开发和管理的能力。详情请点击淘宝链接:Linux三剑客