$(<function> <arguments>)
或者如下${<function> <arguments>}
<function> 表示函数名,
<arguments>表示函数的参数列表。参数以逗号“,”分隔,而函数名和参数之间以空格分隔。
例:comma :=
empty :=
space := $(empty)
var := a b c
bar :=
all:
bar := $(subst $(space),$(comma),$(var))
.PHONY:all
2、字符串处理函数
a、字符串替换函数
表达式:$(subst<from>,<to>,<text>)函数功能:把字符串<text>中的<from>字符串替换成<to>
例:
result=$(subst a,A,how are you)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
结果:
the result is:echo how Are you
how Are you
b、模式字符串替换函数
表达式:$(patsubst<pattern>,<replacement>,<text>)
功能:查找<text>中的单词(单词以“空格”、“Tab”或“回车”“换行”分隔)是否符合模式<pattern>,如果匹配的话,则以<replacement>替换。这里,<pattern>可以包括通配符“%”,表示任意长度的字串。如果<replacement>中也包含“%”,那么,<replacement>中的这个“%”将是<pattern>中的那个“%”所代表的字串。
(可以用“\”来转义,以“\%”来表示真实含义的“%”字符)
返回:函数返回被替换过后的字符串。
例:
result=$(patsubst %.c,%.o, a.c b.c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
结果:
the result is:echo a.o b.o
a.o b.o
c、去空格函数
表达式 $(strip<string>)函数功能:去掉<string>字符串中开头和结尾的空字符
例:
result=$(strip hello world )
all:
@echo -n "the result is:"
@echo $(result)
.PHONY:all
结果:
the result is:hello world
d、查找字符串函数
表达式:$(findstring<find>,<in>)函数功能:在字符串<in>中查找<find>字符串
返回值:如果找到指定的字符串,则返回<find>,否则,返回空字符串。
例:
result=$(findstring hel,hello)
all:
@echo -n "the result is:"
@echo $(result)
.PHONY:all
结果:
the result is:hel
e、过滤函数
表达式:$(filter<pattern...>,<text>)函数功能:以<pattern>模式过滤<text>字符串中的单词,保留符合模式<pattern>的单词。可以有多个模式。
返回值:返回符合模式<pattern>的字符串。
例:
sources = a.c b.c c.c d.h e f
result=$(filter %.c %.h,$(sources))
all:
@echo -n "the result is:"
@echo $(result)
.PHONY:all
结果:
the result is :a.c b.c c.c d.h
f、反过滤函数
表示式:$(filter-out <pattern..>,<text>)函数功能:以<pattern>模式过滤<text>字符串中的单词,去除符合模式<pattern>的单词。可以有多个模式。
返回值:返回不符合模式<pattern>的字符串。
例:
sources = a.c b.c c.c d.h e f
result=$(filter-out %.c %.h,$(sources))
all:
@echo -n "the result is:"
@echo $(result)
.PHONY:all
结果:
the result is :e f
g、排序函数表达式:$(sort<list>)
函数名称:排序函数
函数功能:给字符串<list>中的单词排序(升序)
函数返回值:返回排序后的字符串
例:
result=$(sort hello world china)
all:
@echo -n "the result is:"
@echo $(result)
.PHONY:all
$make -s all
the result is :china hello world
sort函数会去掉<list>中相同的单词。
例:
result=$(sort hello world hello china)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s allthe result is :china hello world
h、取单词函数表达式:$(word <n>,<text>)
函数功能:取字符串<text>中第<n>个单词
返回值:返回字符串<text>中第<n>个单词
例:
result=$(word 2,how are you)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s allthe result is :are
i、取单词串函数表达式:$(wordlist <s>,<e>,<text>)
函数功能:从字符串<text>中取从<s>开始到<e>的单词串。<s>和<e>是一个数字
返回值:返回字符串<text>中从<s>到<e>的单词串。
例:
result1 =$(wordlist 1,2, hello world china)
result2 =$(wordlist 3,2, hello world china)
result3 =$(wordlist 2,5, hello world china)
all:
echo -n "the result is:"
echo $(result1)
echo -n "the result is:"
echo $(result2)
echo -n "the result is:"
echo $(result3)
.PHONY:all
$make -s all
the result1 is :hello worldthe result2 is :
the result3 is :world china
j、单词个数统计函数表示式:$(words <text>)
函数功能:统计<text>中字符串中的单词个数。
返回值:返回<text>中的单词数。
例:
result=$(words hello world china)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :3
k、首单词函数
表达式:$(firstword <text>)函数功能:取字符串<text>中的第1个单词
返回值:返回字符串<text>的第1单词
例:
result=$(firstword hello world china)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :hello
3、文件名操作函数
a、取目录函数表达式;$(dir <names...>)
函数功能:从文件名序列<names>中取出目录部分
目录部分是指最后一个“/”之前的部分。如果没有反斜杠,则返回"./"
返回值:返回文件名序列<names>的目录部分。
例:
result = $(dir test.c /home/admin/test.c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :./ /home/admin
b、notdir 取文件函数
表达式:$(notdir <names...>)函数功能:从文件名列<names>中取出非目录部分。非目录部分是指最后一个“/”之后的部分
返回值:返回文件名序列<names>的非目录部分。
例:
result = $(notdir test.c /home/admin/test.c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :test.c test.c
c、取后缀函数
表达式:$(suffix<names...>)函数功能:从文件名序列<names>中取出各个文件名的后缀。
返回值:文件名序列<names>的后缀序列,如果文件没有后缀,则返回空字符串
例:
result = $(suffix test.text /home/admin/test.c file)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :.text .c
d、取前缀函数表达式:$(basename <names...>)
函数功能:从文件名序列<names>中取各个文件名的前缀部分
返回值:文件名序列<names>的前缀序列,如果文件没有前缀,则返回空字符串
例:
result = $(basename test.text /home/admin/test.c file)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :test /home/admin/test file
e、加后缀函数
表达式:$(addsuffix <suffix>,<names...>)函数功能:把后缀<suffix>加到<names>中每个单词的后面。
返回值:返回加过后缀的文件名序列。
例:
result = $(addsuffix .c,a b c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :a.c b.c c.c
f、加前缀函数表达式:$(addprefix <prefix>,<names...>)
函数功能:把前缀<prefix>加到<names>中每个单词的后面
返回值:返回加过前缀的文件名序列
例:
result = $(addprefix /home/admin/,test1.c test2.c test3.c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :/home/admin/test1.c /home/admin/test2.c /home/admin/test3.c
g、连接函数
表达式:$(join<list1>,<list2>)函数功能:把<list2>中的单词对应地加到<list1>的单词后面
返回值:返回连接过后的字符串。
例:
result1 = $(join /home/ /usr/ /local/,test1.c test2.c test3.c)
result2 = $(join /home/ /usr/ /local/,test1.c test2.c)
result3 = $(join /home/ /usr/ ,test1.c test2.c test3.c)
all:
echo -n "the result1 is:"
echo $(result1)
echo -n "the result2 is:"
echo $(result2)
echo -n "the result3 is:"
echo $(result3)
.PHONY:all
$make -s all
the result1 is:/home/test1.c /usr/test2.c /local/test3.c
the result2 is:/home/test1.c /usr/test2.c /local/the result3 is:/home/test1.c /usr/test2.c test3.c
4、foreach函数
用来控制循环。makefile中的foreach函数几乎是仿照于UNXI标准shell(/bin/sh)中的for语句,其语法如下$(forcesch <var>,<list>,<text>)
var最好是一个变量名,<list>可以是一个表达式,而<text>中一般会使用<var>参数来依次枚举<list>中的单词。
例:
names := a b c d
result = $(foreach n,$(names),$(n).c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
$make -s all
the result is :a.c b.c c.c d.c
5、if函数$(if <condition>,<then-part>)或者是$(if <condition>,<then-part>,<else-part>)
<condition>参数表示一个条件表达式,返回的为非空字符串,<then-part>内的表达式会被计算,返回为空字符串则<else-part>内的表达式会被计算。例:
PHTR=
NAMEPHTR=/home/work
result1 = $(if $(PHTR),$(PHTR),NULL)
result2 = $(if $(NAMEPHTR),$(NAMEPHTR),NULL)
all:
echo -n "the result1 is:"
echo $(result1)
echo -n "the result2 is:"
echo $(result2)
.PHONY:all
$make -s all
the result1 is:NULL
the result2 is:/home/work
6、call函数
$(call <expression>,<parm1>,<parm2>,<parm3>...)当make执行这个函数时,<expression>参数中的变量,如$(1),$(2),$(3)等,会被参数<parm1>,<parm2>,<parm3>,依次取代, <expression>参数中的变量不一定是顺序的。如$(2)$(1)$(3).
而<expression>的返回值就是call函数的返回值。
例:
a+b = $(1)+$(2)
b+a = $(2)+$(1)
result1 = $(call a+b ,1,2)
result2 = $(call b+a ,1,2)
all:
echo -n "the result1 is:"
echo $(result1)
echo -n "the result2 is:"
echo $(result2)
.PHONY:all
$make -s all
the result is : = 1+2
7、origin函数origin 函数操作变量的值,该函数可以将变量的定义信息返回给用户,其语法如下
$(origin <variable>)
<varible>表示变量的名字。因此不应在<variable>中使用$字符。origin函数通过其返回值告诉用户这个变量的定义
情况,其返回值的情况如下
undefined : 如果<variable>表示变量没有定义过,origin函数返回undefined。
default: 如何<variable>表示的变量是一个make工具默认的变量,则origin函数返回default。
environment: 如果<variable>表示的变量是一个环境变量,并且当makefile被执行时,且-e参数没有被打开时,则 origin函数返回environment
file: 如果<variable>表示的变量被定义在makefile中,则origin函数返回file。
command line: 如果<variable>表示变量被命令定义,则origin函数返回command line。
override: 如果<variable>表示的变量被override指示符重新定义,则origin函数返回override。
automatic: 如果<variable>变量是一个命令运行中的自动化变量,则origin函数返回automatic。
例:
a=
c=1
override c=2
result1=$(origin b)
result2=$(origin CC)
result3=$(origin PATH)
result4=$(origin a)
result5=$(origin d)
result6=$(origin c)
result7=$(origin ^)
all:
echo -n "the result1 is:"
echo $(result1)
echo -n "the result2 is:"
echo $(result2)
echo -n "the result3 is:"
echo $(result3)
echo -n "the result4 is:"
echo $(result4)
echo -n "the result5 is:"
echo $(result5)
echo -n "the result6 is:"
echo $(result6)
echo -n "the result7 is:"
echo $(result7)
.PHONY:all
make -s -f d=10
the result1 is:undefined
the result2 is:default
the result3 is:environment
the result4 is:file
the result5 is:command line
the result6 is:override
the result7 is:automatic
8、shell函数
shell函数用来执行操作系统shell的命令。该函数把执行操作系统命令后的输出作为函数返回,用户可以通过shell函数,使用操作系统命令来生成一个变量。$(shell <command>, <parm1>, <parm2>, <parm3>...)
<cammand>表示需要执行的shell命令,<parm1>,<parm2>,<parm3>等是shell命令的参数。该函数的返回值为执行shell命令的输出结果。
例:
result1 = $(shell pwd)
result2 = $(shell ls)
all:
echo -n "the result1 is :"
echo $(result1)
echo -n "the result2 is :"
echo $(result2)
.PHONY:all
make -s all
the result1 is :/work/my/test/makefilethe result2 is :makefile
语法:$(wildcard PATTERN...)
在Makefile中,它被展开为已经存在的、使用空格分开的、匹配此模式的所有文件列表。如果不存在任何符合此模式的文件,函数会忽略模式字符并返回空。
例:
result = $(wildcard *.c ./makefile_wildcard_n/*c)
all:
echo -n "the result is:"
echo $(result)
.PHONY:all
make- s -all
the result is:makefile_wildcard.c ./makefile_wildcard_n/makefile_wildcard.c