Makefile 的函数-1

来源:互联网 发布:游奇网络礼包 编辑:程序博客网 时间:2024/05/20 14:42
轉載自 http://malihou2008.blog.163.com/blog/static/21182004520124158390765/
 
Makefile Function

make 提供了很多函数(包括内置函数和自定义的函数)。这些函数为我们处理变量、文本内容、文件名、命令提供了方便。我们在需要的地方调用函数来处理指定的文本(参数)。函数在调用的地方被替换成它的处理结果。

函数的调用语法
make 内置函数的调用语法(function call syntax)类似于变量的引用(variable reference),它能出现在变量引用可以出现的地方。make 使用和变量引用相同的规则对函数进行展开。

语法格式如下:

    $(function arguments)
    ${function arguments}

1 语法格式中的 function 是函数名。函数名可以是 make 内置函数名,也可以是使用 call 内建函数建立的自定义的函数名。函数名称区分大小写,make 的内置函数都是小写的。
2 语法格式中的 arguments 是函数的参数列表。
3 参数和函数名之间使用若干空白( spaces or tabs 空格或者 Tab 字符,建议使用空格)分隔;如果存在多个参数,参数之间使用逗号(commas ,)分隔。这些空白和逗号(white-space and commas)不是参数列表的一部分,仅仅表示分隔符的作用。
4 函数调用需要使用 $ 符号开头,并使用成对的圆括号(parentheses ())或者大括号(braces {})。参数中存在变量或者函数的引用时,它们的分隔符建议和函数使用的相同(if the arguments themselves contain other function calls or variable references, it is wisest to use the same kind of delimiters for all the references)。
5 函数的参数中不能直接出现逗号(commas)、没有匹配的圆括号(parentheses)、没有匹配的大括号(brace),而前导空格(leading spaces)也不能直接作为第一个参数值。因为逗号是作为参数之间的分隔符;圆括号或者大括号是函数调用时使用的分隔符;而函数名和第一个参数直接的空格会作为它们之间的分隔符。因此,如果需要使用逗号、没有匹配的圆括号或者大括号、前导空格作为函数的参数,可以把它们赋值给变量,然后在函数的参数中使用变量引用的方式来使用它们。

示例 1
Makefile 文件的内容如下

null=
space = $(null) $(null)

string1 ="Functions for Transforming Text"

print:
@echo $(subst $(space),-, $(string1))


测试结果如下

malihou@ubuntu:~$ make
-Functions-for-Transforming-Text
malihou@ubuntu:~$

 
我们可以看到,在示例 1 中的结果中,我们的本意是使用 - 字符替换字符串中的空格字符,但是结果不是我们想要的(在单词之间有一个空格和一个 - 字符,而字符串的前面也有一个 - 字符)。其原因是我们在 subst 函数调用的时候,分隔参数的逗号分隔符的前面有空格引起的,因此,subst 函数的第二个参数是 " -" 而不是 "-";第三个参数是 " $(string1)" 而不是 "$(string1)"。

示例 2
Makefile 文件的内容如下

null=
space = $(null) $(null)
comma =,
lparenthes =(
rparenthes =)
lbrace ={
rbrace =}

# string1 = \(a\) \{b\} c
string1 = $(space)\$(lparenthes)a\$(rparenthes) \$(lbrace)b\$(rbrace) c

print:
@echo $(subst $(space),$(comma),$(string1))
@echo $(subst $(space),$(comma), $(string1))
@echo $(subst $(lparenthes),*,$(string1))
@echo $(subst $(rparenthes),*,$(string1))
@echo $(subst $(lbrace),*,$(string1))
@echo $(subst $(rbrace),*,$(string1))


测试结果如下

malihou@ubuntu:~$ make
,(a),{b},c
,,(a),{b},c
*a){b} c
(a*{b} c
(a)*b} c
(a){b* c
malihou@ubuntu:~$

 
内置函数语法的定义
在 make 源代码的 function.c 文件中定义了 function_table_init 结构体数组,这个数组里面定义了 make 所有内置函数的入名称、参数个数、函数的入口。从这个定义中,我们可以得知,make 在处理函数调用的时候,函数名称是区分大小写的,在这里都是定义为小写。

staticstruct function_table_entry function_table_init[]=
{
/* Name MIN MAX EXP? Function */
FT_ENTRY ("abspath",0,1,1, func_abspath),
FT_ENTRY ("addprefix",2,2,1, func_addsuffix_addprefix),
FT_ENTRY ("addsuffix",2,2,1, func_addsuffix_addprefix),
FT_ENTRY ("basename",0,1,1, func_basename_dir),
FT_ENTRY ("dir",0,1,1, func_basename_dir),
FT_ENTRY ("notdir",0,1,1, func_notdir_suffix),
FT_ENTRY ("subst",3,3,1, func_subst),
FT_ENTRY ("suffix",0,1,1, func_notdir_suffix),
FT_ENTRY ("filter",2,2,1, func_filter_filterout),
FT_ENTRY ("filter-out",2,2,1, func_filter_filterout),
FT_ENTRY ("findstring",2,2,1, func_findstring),
FT_ENTRY ("firstword",0,1,1, func_firstword),
FT_ENTRY ("flavor",0,1,1, func_flavor),
FT_ENTRY ("join",2,2,1, func_join),
FT_ENTRY ("lastword",0,1,1, func_lastword),
FT_ENTRY ("patsubst",3,3,1, func_patsubst),
FT_ENTRY ("realpath",0,1,1, func_realpath),
FT_ENTRY ("shell",0,1,1, func_shell),
FT_ENTRY ("sort",0,1,1, func_sort),
FT_ENTRY ("strip",0,1,1, func_strip),
FT_ENTRY ("wildcard",0,1,1, func_wildcard),
FT_ENTRY ("word",2,2,1, func_word),
FT_ENTRY ("wordlist",3,3,1, func_wordlist),
FT_ENTRY ("words",0,1,1, func_words),
FT_ENTRY ("origin",0,1,1, func_origin),
FT_ENTRY ("foreach",3,3,0, func_foreach),
FT_ENTRY ("call",1,0,1, func_call),
FT_ENTRY ("info",0,1,1, func_error),
FT_ENTRY ("error",0,1,1, func_error),
FT_ENTRY ("warning",0,1,1, func_error),
FT_ENTRY ("if",2,3,0, func_if),
FT_ENTRY ("or",1,0,0, func_or),
FT_ENTRY ("and",1,0,0, func_and),
FT_ENTRY ("value",0,1,1, func_value),
FT_ENTRY ("eval",0,1,1, func_eval),
FT_ENTRY ("file",1,2,1, func_file),
#ifdef EXPERIMENTAL
FT_ENTRY ("eq",2,2,1, func_eq),
FT_ENTRY ("not",0,1,1, func_not),
#endif
};

 内置函数
make 4.0 版本默认提供了 36 个内置函数,如果在宏 EXPERIMENTAL 定义(在 make 4.0 的正式 release 版本中没有定义,仅仅是试验性的)的情况下,有 38 个内置函数。这些内置函数根据功能主要分为 12 类。
 
 函数分类 个数 字符串替换和分析函数Functions for String Substitution and Analysis) 12 文件名函数(Functions for File Names) 10 条件语句函数(Functions for Conditionals) 3 foreach 函数(The foreach Function) 1 file 函数(The file Function)  1 call 函数(The call Function) 1 value 函数(The value Function) 1 eval 函数(The eval Function) 1 origin 函数(The origin Function) 1 flavor 函数(The flavor Function) 1 make 控制函数(Functions That Control Make) 3 shell 函数(The shell Function) 1
 
字符串替换和分析函数(Functions for String Substitution and Analysis)
make 的字符串替换和分析函数(Functions for String Substitution and Analysis)是处理字符串的,实现替换、搜索、过滤、排序、取子字符串、取单词等操作。
 
 函数语法 函数功能描述 $(subst from,to,text) 字符串替换 $(patsubst pattern,replacement,text) 模式字符串替换 $(strip string) 去空白 $(findstring find,in) 字符串查找 $(filter pattern...,text) 过滤 $(filter-out pattern...,text) 反过滤 $(sort list) 排序 $(word n,text) 取第 n 个位置单词 $(wordlist s,e,text) 取单词列表 $(words text) 单词个数 $(firstword names...) 取首单词 $(lastword names...) 取末尾单词   
subst 函数

    $(subst from,to,text)

1 subst 函数对 text 执行文本替换(textual replacement)操作,使用 to 替换 text 中所有的 from。

示例 3
Makefile 文件的内容如下

string="feet on the street"

print:
@echo $(subst ee,EE,$(string))


测试结果如下

malihou@ubuntu:~$ make
fEEt on the strEEt
malihou@ubuntu:~$


patsubst 函数

    $(patsubst pattern,replacement,text)

1 patsubst 函数查找 text 中使用空白分隔(whitespace-separated)的并匹配 pattern 的单词,然后将这些查找到的匹配单词使用 replacement 进行替换。
2 pattern 中可以使用模式匹配符 % 来代表一个单词中的若干字符。如果 replacement 中也包含 %,那么 replacement 中的 % 是 pattern 中的 % 所代表的字符串。
3 在 pattern 和 replacement 中,只有第一个 % 被作为模式匹配符处理,之后所有的 % 仅作为普通字符处理。
4 在 pattern 和 replacement 中,模式字符串 % 之前可以使用反斜线 backslash(\)进行引用,表示 % 字符本身。另外反斜线(\)也可以引用反斜线(\)。
5 text 中的单词之间的多个空格在处理时被合并为一个空格。

示例 4
Makefile 文件的内容如下

sources =1.c2.c test.c
objects = $(patsubst %.c,%.o,$(sources))

file1 =%1.%.tmp 1%1.%45.%
file2 = $(patsubst \%%.%,%.bak,$(file1))
file3 = $(patsubst %.%,%.bak,$(file1))

print:
@echo"sources=$(sources)"
@echo"objects=$(objects)"
@echo"file1=$(file1)"
@echo"file2=$(file2)"
@echo"file3=$(file3)"


测试结果如下

malihou@ubuntu:~$ make
sources=1.c2.c test.c
objects=1.o2.o test.o
file1=%1.%.tmp 1%1.%45.%
file2=%1.%.tmp 1%1.%45.%
file3=%1.%.tmp 1%1.bak45.bak
malihou@ubuntu:~$


变量的替换引用(substitution references)是 patsubst 函数的简化实现。

    $(var:pattern=replacement)
    $(var:suffix=replacement)

等同于

    $(patsubst pattern,replacement,$(var))
    $(patsubst %suffix,%replacement,$(var))

示例 5
Makefile 文件的内容如下

cpps = foo.cpp bar.cpp baz.cpp

obj1 = $(cpps:cpp=o)
obj2 = $(cpps:.cpp=.o)
obj3 = $(patsubst %cpp,%o,$(cpps))
obj4 = $(patsubst %.cpp,%.o,$(cpps))

print:
@echo obj1=$(obj1)
@echo obj2=$(obj2)
@echo obj3=$(obj3)
@echo obj4=$(obj4)

 
测试结果如下

malihou@ubuntu:~$ make
obj1=foo.o bar.o baz.o
obj2=foo.o bar.o baz.o
obj3=foo.o bar.o baz.o
obj4=foo.o bar.o baz.o
malihou@ubuntu:~$


 strip 函数

    $(strip string)

1 strip 函数去掉 string 的前置和后置空白(leading and trailing whitespace),并将 string 中多个连续的空白字符合并为一个空白字符。
2 strip 函数是非常有用的,其经常用在条件语句的表达式中(conditionals)。例如,我们再判断一个变量的值是否为空字符串的时候,需要使用 ifeq 或者 ifneq 条件表达式,通常我们会先需要去掉字符串的前置和后置空白字符,这样才能准确判断一个字符串是否为空字符串。

示例 6
Makefile 文件的内容如下

string= hello world

ifeq ($(string),hello world)
output_str1 = equal
else
output_str1 =not equal
endif

ifeq ($(strip $(string)),hello world)
output_str2 = equal
else
output_str2 =not equal
endif

print:
@echo"$(string)"
@echo"$(strip $(string))"
@echo"$(output_str1)"
@echo"$(output_str2)"


测试结果如下

malihou@ubuntu:~$ make
hello world
hello world
not equal
equal
malihou@ubuntu:~$


findstring 函数

    $(findstring find,in)

1 findstring 函数在 in 中查找 find。如果在 in 中存在 find,则返回 find,否则返回空。
2 findstring 这个函数很有用,通常用在一个条件表达式中去测试一个字符串中是否存在指定的字符串。

示例 7
Makefile 文件的内容如下

string= hello world

ifeq '$(findstring world,$(string))'''
output_str1 = search fail
else
output_str1 = search success
endif

ifeq '$(findstring World,$(string))'''
output_str2 = search fail
else
output_str2 = search success
endif

print:
@echo $(output_str1)
@echo $(output_str2)


测试结果如下

malihou@ubuntu:~$ make
search success
search fail
malihou@ubuntu:~$


filter 函数

    $(filter pattern...,text)

1 filter 函数过滤掉 text 中所有不匹配 pattern 的单词,保留所有匹配 pattern 的单词。
2 模式列表可以使用多个 pattern。多个模式之间使用空格分隔。
3 filter 函数常用于过滤变量值中不同类型的字符串(different types of strings)。
4 如果在 text 中匹配了多个相同的单词,在 filter 函数的返回结果中相同的单词都会出现。

filter-out 函数

    $(filter-out pattern...,text)

1 filter-out 函数和 filter 函数的功能相反,它过滤掉 text 中所有匹配 pattern 的单词,保留所有不匹配 pattern 的单词。
2 模式列表可以使用多个 pattern。多个模式之间使用空格分隔。

示例 8
Makefile 文件的内容如下

lists =1.c phone.Camera.o computer.obj 0.mak phone.c


print:
@echo filter=$(filter %.c %.mak,$(lists))
@echo filter_out=$(filter-out%.c %.mak,$(lists))

测试结果如下

malihou@ubuntu:~$ make
filter=1.c phone.0.mak phone.c
filter_out=Camera.o computer.obj
malihou@ubuntu:~$


sort 函数
 
    $(sort list)
 
1 sort 函数将 list 中的单词按照词典升序排序(lexical order),并去掉重复的单词。
2 sort 函数的输出结果是使用单空格分隔的单词序列(the output is a list of words separated by single spaces),原 list 中多余的空格会被去掉。

示例 9
Makefile 文件的内容如下

lists =1 phone Camera computer 0 phone

print:
@echo"$(sort $(lists))"


测试结果如下

malihou@ubuntu:~$ make
01Camera computer phone
malihou@ubuntu:~$

 
word 函数
 
    $(word n,text)
 
1 word 函数取 text 中第 n 个单词。
2 n 需满足条件 0 < n <= $(words text),否则返回空值或者出错(if n is bigger than the number of words in text, the value is empty)。
3 n 为 0 时,make 将提示错误(first argument to 'word' function must be greater than 0)。
 
示例 10
Makefile 文件的内容如下
lists = 1 phone Camera computer 0 phone
 
print :
@echo word=$(word $(words $(lists)),$(lists))
@echo word=$(word 7,$(lists))
 
测试结果如下
malihou@ubuntu:~$ make
word=phone
word=
malihou@ubuntu:~$
 
wordlist 函数
 
    $(wordlist s,e,text)
 
1 wordlist 函数从 text 中取出从 s 开始到 e 结束之间的单词串。
2 s 需满足条件 0 < s <= $(words text) ,否则返回空值或者出错(if s is bigger than the number of words in text, the value is empty)。
3 e 需满足条件 0 < e,否则出错。
4 如果 e > $(words text),则返回从 s 开始到 $(words text) 结束之间的单词串(if e is bigger than the number of words in text, words up to the end of text are returned)。
5 如果 s > e,则返回空值(if s is greater than e, nothing is returned)。
 
示例 11
Makefile 文件的内容如下
lists = 1 phone Camera computer 0 phone
 
print :
@echo word_list=$(wordlist 1,3,$(lists))
@echo word_list=$(wordlist 2,7,$(lists))
@echo word_list=$(wordlist 7,2,$(lists))
 
测试结果如下
malihou@ubuntu:~$ make
word_list=1 phone Camera
word_list=phone Camera computer 0 phone
word_list=
malihou@ubuntu:~$
 
words 函数
 
    $(words text)
 
1 words 函数统计 text 中单词的个数。
2 text 最后一个单词为 $(word $(words text),text)。
 
示例 12
Makefile 文件的内容如下
lists = 1 phone Camera computer 0 phone
 
print :
@echo words=$(words $(lists))
 
测试结果如下
malihou@ubuntu:~$ make
words=6
malihou@ubuntu:~$
 
firstword 函数
 
    $(firstword names...)
 
1 firstword 函数取 names 中的第一个单词(the value is the first name in the series)。names 展开后被认为是一系列单词的集合(a series of names),这些单词使用空白分隔。
2 $(firstword text) 等同于 $(word 1,text)。
3 $(firstword text) 等同于 $(wordlist 1,1,text)。
 
lastword 函数
 
    $(lastword names...)
 
1 lastword 函数取 names 中的最后一个单词(the value is the last name in the series)。names 展开后被认为是一系列单词的集合(a series of names),这些单词使用空白分隔。
2 $(lastword text) 等同于 $(word $(words text),text)。
3 $(lastword text) 等同于 $(wordlist $(words text),$(words text),text)。
 
示例 13
Makefile 文件的内容如下
lists = 1 phone Camera computer 0 phone
 
print :
@echo first_word=$(firstword $(lists))
@echo first_word=$(word 1,$(lists))
@echo first_word=$(wordlist 1,1,$(lists))
@echo last_word=$(lastword $(lists))
@echo last_word=$(word $(words $(lists)),$(lists))
@echo last_word=$(wordlist $(words $(lists)),$(words $(lists)),$(lists))
 
测试结果如下
malihou@ubuntu:~$ make
first_word=1
first_word=1
first_word=1
last_word=phone
last_word=phone
last_word=phone
malihou@ubuntu:~$
 
makefile 的 VPATH 变量的值通常是一系列目录的集合,make 使用 VPATH 的值去搜索目标的依赖文件。在实际使用中,GCC 编译器也需要
这一系列的目录去寻找头文件。如下示例 14 通过使用字符串替换和分析函数 subst 和 patsubst 从 VPATH 变量的值获取 GCC 编译器的 -I 参数的值。
 
示例 14
Makefile 文件的内容如下

VPATH="src:../headers"

print:
@echo CFLAGS=$(patsubst %,-I%,$(subst :,,$(VPATH)))

 
测试结果如下

malihou@ubuntu:~$ make
CFLAGS=-Isrc-I../headers
malihou@ubuntu:~$

 
文件名函数(Functions for File Names)
make 的文件名函数(Functions for File Names)主要用来对使用空格分隔的文件名进行转换,得到文件名的目录部分、非目录部分、后缀部分、前缀部分、路径等。
 
 函数语法 函数功能描述 $(dir names...) 取文件的目录部分 $(notdir names...) 取文件的非目录部分 $(suffix names...) 取文件的后缀 $(basename names...) 取文件的文件名 $(addsuffix suffix,names...) 给文件附加后缀 $(addprefix prefix,names...) 给文件附加前缀 $(join list1,list2) 列表单词连接 $(wildcard pattern) 文件通配 $(realpath names...) 取真实路径 $(abspath names...) 取绝对路径   
dir 函数
 
    $(dir names...)
 
1 dir 函数从 names 中取出各个文件名的目录部分。
2 文件名的目录部分是指文件名中的最后一个斜线(/)之前的部分(包含斜线)。
3 如果文件名没有斜线,则目录部分为 "./"(if the file name contains no slash, the directory part is the string ‘./’)。
 
notdir 函数
 
    $(notdir names...)
 
1 notdir 函数从 names 中取出各个文件名的非目录部分。
2 文件名的非目录部分是指文件名中的最后一个斜线(/)之后的部分。
3 如果文件名以斜线结尾,则返回空字符串(a file name that ends with a slash becomes an empty string)。
 
示例 15
Makefile 文件的内容如下
files = /home/guest/Pictures/1.png Makefile /home/guest /home/guest/Videos/ test.c
 
print :
@echo dir=$(dir $(files))
@echo notdir=$(notdir $(files))
 
测试结果如下
malihou@ubuntu:~$ make
dir=/home/guest/Pictures/ ./ /home/ /home/guest/Videos/ ./
notdir=1.png Makefile guest test.c
malihou@ubuntu:~$
 
suffix 函数
 
    $(suffix names...)
 
1 suffix 函数从 names 中取出各个文件名的后缀部分。
2 文件名的后缀部分是文件名中最后一个点号(.)之后的部分(包含点号)。
3 如果文件名中不包含一个点号,则返回空字符串(if the file name contains a period, the suffix is everything starting with the last period. Otherwise, the suffix is the empty string)。
 
basename 函数
 
    $(basename names...)
 
1 basename 函数从 names 中取出各个文件名的前缀部分。
2 文件名的前缀部分指的是文件名中最后一个点号(.)之前的部分。
3 如果文件名以点号开头,则返回空字符串。
4 如果文件名中没有点号,则返回整个文件名(if there is no period, the basename is the entire file name)。
 
示例 16
Makefile 文件的内容如下
files = /home/guest/Pictures/1.png Makefile /home/guest /home/guest/Videos/ test.c
 
basenames = $(basename $(files))
 
print :
@echo suffixs=$(suffix $(files))
@echo basenames=$(basename $(files))
 
测试结果如下
malihou@ubuntu:~$ make
suffixs=.png .c
basenames=/home/guest/Pictures/1 Makefile /home/guest /home/guest/Videos/ test
malihou@ubuntu:~$
 
addsuffix 函数
 
    $(addsuffix suffix,names...)
 
1 addsuffix 函数为 names 中的每一个文件名添加后缀 suffix(在文件名之后添加)。
2 返回结果中文件名之间使用单空格连接(the resulting larger names are concatenated with single spaces between them)。
 
addprefix 函数
 
    $(addprefix prefix,names...)
 
1 addprefix 函数为 names 中的每一个文件名添加前缀 prefix(在文件名之前添加)。
2 返回结果中文件名之间使用单空格连接(the resulting larger names are concatenated with single spaces between them)。
 
示例 17
Makefile 文件的内容如下
print :
@echo $(addsuffix .c,1 2 3 4)
@echo $(addprefix /home/guest/,1.c 2.c 3 4)
 
测试结果如下
malihou@ubuntu:~$ make
1.2.c 3.c 4.c
/home/guest/1.c /home/guest/2.c /home/guest/3 /home/guest/4
malihou@ubuntu:~$
join 函数
 
    $(join list1,list2)
 
1 join 函数将 list1 和 list2 各单词进行连接(concatenates the two arguments word by word)。将 list2 的第一个单词追加到 list1 第一个单词后合并为一个单词,依次类推。
2 如果 list1 和 list2 中单词的数目不一致时,两者中多余部分将被作为返回值。
3 list1 和 list2 中单词之间多余的空格将不会保留,连接后的单词之间使用单空格进行分隔(whitespace between the words in the lists is not preserved; it is replaced with a single space)。
 
示例 18
Makefile 文件的内容如下
dirs = home/guest/Picture/  home/guest/Video/
files = 1.png  2.wav  3.c
 
print :
@echo $(join $(dirs),$(files))
 
测试结果如下
malihou@ubuntu:~$ make
home/guest/Picture/1.png home/guest/Video/2.wav 3.c
malihou@ubuntu:~$
 
wildcard 函数
 
    $(wildcard pattern)
 
1 wildcard 函数列出当前目录下存在的符合通配符模式 pattern 的文件名。
2 pattern 使用 shell 可识别的通配符,包括:?、*、[...]、[!...]、[^...]、[c1-c2]。
3 pattern 不支持 shell 的通配符 {string1,string2...}。
4 pattern 可以使用多个通配符模式。多个通配符模式之间使用空格分隔。
5 wildcard 函数常用于判断一个文件或者目录是否存在。
 
示例 19
Makefile 文件的内容如下
# exist files 1.c 2.c test.c 3.c 1.h
file1 = $(wildcard *.c)
file2 = $(wildcard [12].?)
file3 = $(wildcard [1-3].c)
file4 = $(wildcard [!12].*)
file5 = $(wildcard [^12].*)
 
print :
@echo file1=$(file1)
@echo file2=$(file2)
@echo file3=$(file3)
@echo file4=$(file4)
@echo file5=$(file5)
 
测试结果如下
malihou@ubuntu:~$ make
file1=1.c 2.c 3.c test.c
file2=1.c 1.h 2.c
file3=1.c 2.c 3.c
file4=3.c
file5=3.c
malihou@ubuntu:~$
 
示例 20
Makefile 文件的内容如下
# exist files 1.c 2.c test.c 3.c 1.h
file = 1.c
 
ifeq '$(file)' '$(wildcard $(file))'
output_str = $(file) is exist
else
output_str = $(file) is not exist
endif
 
print :
@echo $(output_str)
 
测试结果如下
malihou@ubuntu:~$ make
1.is exist
malihou@ubuntu:~$
 
realpath 函数
 
    $(realpath names...)
 
1 realpath 函数获取 names 中存在的文件和目录的绝对路径(the canonical absolute name)。
2 如果 names 中包含 . 或者 ..,分别表示当前目录和上一级目录。
3 realpath 函数会判断文件和目录是否存在,如果不存在,则返回空。
 
abspath 函数
 
    $(abspath names...)
 
1 abspath 函数获取 names 中文件和目录的绝对路径(the canonical absolute name)。
2 如果 names 中包含 . 或者 ..,分别表示当前目录和上一级目录。
3 abspath 函数不会检查文件或者目录是否存在,如果文件或者目录不存在则指定为当前目录。
3 realpath 函数和 abspath 函数的区别在于前者只返回存在的文件或者目录的绝对路径(in contrast to realpath function, abspath does not resolve symlinks and does not require the file names to refer to an existing file or directory. Use the wildcard function to test for existence)。
 
示例 21
Makefile 文件的内容如下
# 当前目录下存在文件 make
files = make 2.c 4.c . .. . 2.c
 
print :
@echo realpath=$(realpath $(files))
@echo abspath=$(abspath $(files))
 
测试结果如下
malihou@ubuntu:~$ make
realpath=/home/malihou/make /home/malihou /home /home/malihou
abspath=/home/malihou/make /home/malihou/2.c /home/malihou/4.c /home/malihou /home/home/malihou /home/malihou/2.c
malihou@ubuntu:~$
0 0
原创粉丝点击