【make】makefile 函数全解

发布于:2025-02-10 ⋅ 阅读:(110) ⋅ 点赞:(0)

在这里插入图片描述

makefile简介

  makefile 是一种类似shell的脚本文件,需要make工具进行解释 makefile 内的语句,然后执行内部语句。Makefile的作用是去管理工程项目,比如一个项目有很多c文件,需要利用Makefile去统一进行编译或者其他操作。[1]

函数全解介绍

以下脚本内容可有效作用于 make版本: GNU Make 4.2.1

相关链接

makefile变量全解

字符串处理函数

subst 函数—字符串替换

把字符串<text>中所有的<from>字符串替换成<to>

#$(subst <from>,<to>,<text>)
res = $(subst oo,OO,good food)
show:
    @echo $(res)   
#输出
gOOd fOOd

patsubst 函数 — 模式字符串替换

查找<text>中的单词末尾是否符合模式<pattern>,如果匹配的话,则以<replacement>替换。

#$(patsubst <pattern>,<replacement>,<text>)
res = $(patsubst %ood,%OOd,good food)
show:
    @echo $(res)    
#输出
gOOd fOOd

strip 函数 — 去空格

去掉<string>字串中开头和结尾的空字符

#$(strip <string>)
res = $(strip good food  time    show  get set)
show:
    @echo $(res)
#输出
good food time show get set

findstring 函数 — 查找字符串

在字串<in>中查找<find>字串,如果找到就返回字符串,没找到就返回空

#$(findstring <find>,<in>)
res = $(findstring foo,good food)
show:
    @echo $(res)

#输出
foo

filter 函数 — 过滤器

<pattern>模式过滤<text>字符串中的单词,保留符合模式<pattern>的单词。可以有多个模式,用空格分隔。

#$(filter <pattern...>,<text>) 
res = $(filter %d %c %ab,good food bab come luc)
show:
    @echo $(res)
#输出
good food bab luc

filter-out 函数 — 过滤器

功能和上面相反,去除符合匹配pattern的。以<pattern>模式过滤<text>字符串中的单词,去除符合模式<pattern>的单词。可以有多个模式。

#$(filter-out <pattern...>,<text>)
res = $(filter-out %d %c %ab,good food bab come luc)
show:
    @echo $(res)
#输出
come

sort 函数 — 排序

给字符串<list>中的单词排序(升序)。

#$(sort <list>)
res = $(sort a c z d w y b e)
show:
    @echo $(res)
#输出
a b c d e w y z

word 函数 — 取单词

取字符串<text>中第<n>个单词。(从一开始)

#$(word <n>,<text>)
res = $(word 2,good food time)
show:
    @echo $(res)
#输出
food

wordlist函数 — 取一串字符串

从字符串<text>中取从<s>开始到<e>的单词串。<s><e>是一个数字。

#$(wordlist <s>,<e>,<text>)
res = $(wordlist 2,3,good food time)
show:
    @echo $(res)   
#输出
food time

words 函数 — 计算单词数

统计<text>中字符串中的单词个数

#$(words <text>) 
res = $(words good food time)
show:
    @echo $(res)    
#输出
3

firstword 函数 — 返回第一个单词

取字符串<text>中的第一个单词。

#$(firstword <text>)
res = $(firstword good food time)
show:
    @echo $(res)   
#输出
good

lastword 函数 — 返回最后一个单词

返回字符串<text>的第一个单词

#$(lastword <text>)
res = $(lastword good food time)
show:
    @echo $(res)   
#输出
time

文件名操作函数

dir 函数 — 取文件夹部分

从文件名序列<names>中取出目录部分。目录部分是指最后一个反斜杠(“/”)之前的部分。如果没有反斜杠,那么返回“./”。

#$(dir <names...>)
res = $(dir main.c subdir/subfile.c)
show:
    @echo $(res)   
#输出
./ subdir/

notdir 函数 — 取非文件夹部分

从文件名序列<names>中取出非目录部分。非目录部分是指最后一个反斜杠(“ /”)之后的部分。

#$(notdir <names...>)
res = $(dir main.c subdir/subfile.c)
show:
    @echo $(res)   
#输出
main.c subfile.c

suffix 函数 — 取出各个文件名的后缀

从文件名序列<names>中取出各个文件名的后缀

#$(suffix <names...>) 
res = $(suffix main.c subdir/subfile.c)
show:
    @echo $(res)   
#输出
.c .c

basename 函数 — 取出各个文件名的前缀部

从文件名序列<names>中取出各个文件名的前缀部分。

#$(basename <names...>)
res = $(basename main.c subdir/subfile.c)
show:
    @echo $(res)   
#输出
main subdir/subfile

addsuffix 函数 — 添加后缀

把后缀<suffix>加到<names>中的每个单词后面

#$(addsuffix <suffix>,<names...>) 
res = $(addsuffix .c,main subdir/subfile)
show:
    @echo $(res)   
#输出
main.c subdir/subfile.c

addprefix 函数 — 添加前缀

把前缀<prefix>加到<names>中的每个单词前面。

#$(addprefix <prefix>,<names...>) 
res = $(addprefix  ./,main.c subdir/subfile.c)
show:
    @echo $(res)   
#输出
./main.c ./subdir/subfile.c

join 函数 — 各个单词进行连接

<list2> 中的单词对应地加到<list1>的单词后面。如果<list1>的单词个数要比的多,那么,中的多出来的单词将保持原样。如果<list2>的单词个数要比<list1>多,那么,<list2>多出来的单词将被复制到<list2>中。

#$(join <list1>,<list2>)
res = $(join good food time,show look find read get put)
show:
    @echo $(res)  
#输出
goodshow foodlook timefind read get put

foreach 函数 — 依次处理字符串

把参数<list>中的单词逐一取出放到参数<var>所指定的变量中,然后再执行<text>所包含的表达式。每一次<text>会返回一个字符串,循环过程中,<text>的所返回的每个字符串会以空格分隔,最后当整个循环结束时,<text>所返回的每个字符串所组成的整个字符串(以空格分隔)将会是 foreach 函数的返回值。

所以,<var>最好是一个变量名,<list>可以是一个表达式,而<text>中一般会使用<var>这个参数来依次枚举<list>中的单词。<var>参数是一个临时的局部变量,foreach 函数执行完后,参数<var>的变量将不在作用。

#$(foreach <var>,<list>,<text>)
names := a b c d  
files := $(foreach n,$(names),$(n).o) 
show:
    @echo $(files)   
#输出
a.o b.o c.o d.o

if 函数 — 分支判断

类似于关键字 ifeq

#$(if <condition>,<then-part>) 或则 $(if <condition>,<then-part>,<else-part>)
num=4
res := $(if num<5,smaller,bigger)
show:
    @echo $(res) 

#输出
smaller

call 函数 — 调用其他变量

<expression>参数中的变量,如$(1)$(2)$(3)等,会被参数<parm1><parm2><parm3>依次取代。而<expression>的返回值就是 call 函数的返回值。

#$(call <expression>,<parm1>,<parm2>,<parm3>...)
first = Donald
last = Trump
Donald_Trump = "I'M BACK!"
func = $(1)_$(2)
fullname = $(call func,$(first),$(last))
res := $($(call func,$(first),$(last)))
show:
    @echo $(fullname)
    @echo $(res)      
#输出
Donald_Trump
I'M BACK!

origin 函数 — 变量来源

获取变量来源,<variable> 是变量名,不需要加 $

  • undefined :如果<variable>从来没有定义过,origin 函数返回这个值“undefined”

  • default : 如果<variable>是一个默认的定义,比如“CC”这个变量

  • file : 如果<variable>这个变量被定义在 Makefile 中

  • command line : 如果<variable>这个变量是被命令行定义的

  • override : 如果<variable>是被 override 指示符重新定义的

  • automatic : 如果<variable>是一个命令运行中的自动化变量

#$(origin <variable>)

shell 函数 — 执行shell命令

用于执行shell 命令

#$(shell <shell command>)
num = 10
res = $(shell echo $(num))
show:
    @echo $(res)   
# 输出
10

日志输出函数

用于控制日志输出,以及make执行

$(info 'here is info.')
$(warning 'here is warning.')
$(error 'here is error.')
#输出
'here is info.'
Makefile:3: 'here is warning.'
Makefile:4: *** 'here is error.'.  Stop.

总结

其他函数可参考帮助文档。

参考链接

makefile变量全解
gnu make 帮助手册
百度百科— make
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到