前言

学习make和makefile的主要目的是分析大型项目的源代码的关系,上一节我们讲述了makefile 中的变量,本节主要学习一下 makefile 中的函数,首先函数肯定可以分为几部分:

  • 内置函数
  • 用户自动义函数
  • 函数的调用
  • ..

函数的语法

make 下所有函数都有如下形式:

1
$(function-name arg1[,arg2,arg3,..])

我们首先来看内置函数:

内置函数

make 的内置函数可分类如下:

字符串函数

$(filter pattern …,text)

pattern 中可以使用 % 来做通配符,值得注意的是: 每个 pattern 只支持 1 个 % ,比如:

1
2
3
4
5
6
//makefile
words := he the hen other the%
get-word:
@echo he matches: $(filter he,$(words))
@echo th% matches: $(filter th%,$(words))
@echo %th% matches: $(filter %th%,$(words))

结果为:

1
2
3
4
//result
he matches: he
th% matches: the the%
%th% matches: #最后一个不匹配,因为words中不存在以th%为结尾的单词

$(filter-out pattern ..,text)

filter-out同filter相反,比如:

1
2
3
4
5
//makefile
words := he the hen other the%
get-word:
@echo %th% matches: $(filter %th%,$(words))
@echo %th% matches: $(filter-out %th%,$(words))

结果为:

1
2
3
//result
%th% matches:
%th% matches: he the hen other the%

$(findstring string…,text)

此函数所返回会的只是“搜索字符”而不是包含搜索字符的字符,另外不支持pattern

1
2
3
4
5
words := he the hen other the%
get-word:
@echo %th% matches: $(findstring %th%,$(words))
@echo he matches: $(findstring he,$(words))
@echo he% matches: $(findstring he%,$(words))

结果为:

1
2
3
%th% matches:
he matches: he
he% matches: he%

$(subst search-string,replace-string,text)

不具有通配符能力的搜索替换函数,最经常使用于替换文件名列表的扩展名,如:

1
2
3
4
5
sources := fun.c foo.c
objects := $(subst .c,.o,$(sources)) target:
@echo $(objects)

结果为:

1
fun.o foo.o

$(patsubst search-pattern,replace-pattern,text)

具有通配符能力的替换功能,有几个注意点:

  • 此处的模式只可以包含一个 % 字符
  • replace-pattern中的百分比符号会被替换成与符号相符的文字
  • search-pattern必须和text的整个值进行匹配
1
2
3
4
source := main.c
object := $(patsubst %.c,%.o,$(source))
target:
gcc -c $(source) -o $(object)

结果为:

1
gcc -c main.c -o main.o

单词函数

$(words text)

统计text中单词个数 ,按照”space(空格)”进行拆分

1
2
3
4
CURRENT_PATH := $(subst /, ,$(PWD))
words:
@echo current path $(PWD)
@echo current path has $(words $(CURRENT_PATH)) directories

结果为:

1
2
current path /Users/hejianglin/study/makefile
current path has 4 directories

$(word n,text)

“space(空格)”进行拆分,返回 text 中的第 n 个单词,开始编号为 1,如果找不到相应的位置则返回空

1
2
3
4
CURRENT_PATH := $(subst /, ,$(PWD))
words:
@echo current path $ 大专栏  makefile(3)函数(PWD)
@echo current path second word: $(word 2,$(CURRENT_PATH))

结果为:

1
2
current path /Users/hejianglin/study/makefile
current path second word: hejianglin

$(wordlist start,end,text)

“space(空格)”进行拆分,返回 text 中的第 start(含) 到 end(含) 的单词,开始编号为 1,如果找不到相应的位置则返回空

文件名函数

$(wildcard pattern..)

文件列表

1
2
3
4
5
source := $(wildcard src/*.c include/*.h)

.PHONY: test
test:
@echo $(source)

结果为:

1
src/foo.c src/fun.c src/main.c include/foo.h include/fun.h

$(dir list…)

目录列表

1
2
3
4
5
source := $(wildcard src/*.c include/*.h)

.PHONY: test
test:
@echo $(dir $(source))

结果为:

1
src/ src/ src/ include/ include/

$(notdir list..)

删除目录的文件列表

1
2
3
4
5
source := $(wildcard src/*.c include/*.h)

.PHONY: test
test:
@echo $(notdir $(source))

结果为:

1
foo.c fun.c main.c foo.h fun.h

$(suffix name…)

返回每个单词的后缀

$(basename name…)

suffix的反函数

$(addsuffix suffix, name…)

添加后缀名称

$(addprefix prefix,name…)

addsuffix的反函数,添加前缀

$(join prefix-list,suffix-list)

连接字符,dir 和 notdir 的反函数

1
2
3
4
5
6
source := $(wildcard src/*.c include/*.h)
prefix_dir := $(dir $(source))
suffix_file := $(notdir $(source))
.PHONY: test
test:
@echo join $(join $(prefix_dir),$(suffix_file))

结果为:

1
join src/foo.c src/fun.c src/main.c include/foo.h include/fun.h

其他

$(sort list)

排序 list 并移除重复项,排序优先级: 数字 > 字母,字母按照升序排序:

1
2
3
source := b c 1 2 d 0 c
target:
@echo sort source: $(sort $(source))

结果为:

1
sort source: 0 1 2 b c d

$(shell command)

执行 shell 命令,输出的换行被替换成单一的空格符号,错误和状态都不会返回

1
2
3
4
.PHONY: test
CURRENT_DATE = $(shell date +%Y%m%d)
test:
@echo $(CURRENT_DATE)

结果为:

1
20180610

流程控制

if

$(if condition, then-part,else-part)

只要 condition 返回不为空,则为 true 便会执行 then-part, 否则执行 else-part

1
2
3
4
5
6
$(if $(filter $(MAKE_VERSION),3.79 3,80),,
$(error requires makefile version -.)) .PHONY: test
test:
@echo join $(join $(prefix_dir),$(suffix_file))

结果为:

1
makefile:1: *** requires makefile version -..  Stop.

因为本机的 make 版本是 3.81

for/while

$(foreach variable,list,body)

==这个函数实在难以理解(用法…),跳过吧==

makefile(3)函数的更多相关文章

  1. 跟我一起写Makefile:使用函数

    跟我一起写Makefile:使用函数 两个排版不一样 书籍下载 书籍下载

  2. Makefile常用函数(转)

    一.字符串处理函数 1.$(subst FROM,TO,TEXT) 函数名称:字符串替换函数-subst. 函数功能:把字串"TEXT"中的"FROM"字符替换 ...

  3. makefile常用函数

    标签(空格分隔): makefile 1.字符串替换和分析函数 $(subst from,to,text) #在文本"text"中使用"to"替换每一处&quo ...

  4. 调试makefile—subst函数

    操作系统:ubuntu10.04 Makefile里的subst用法是$(subst FROM,TO,TEXT),即将TEXT中的东西从FROM变为TO Makefile中的字符串处理函数格式:    ...

  5. Makefile常用函数总结

    在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具 有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 数的返回值可以当做变量来使用. 一 ...

  6. makefile 常用函数

    Linux下编译c/c++源码需要编写makefile文件,文章参看 http://blog.sina.com.cn/s/blog_4c4d6e74010009jr.html 一函数的调用语法 二字符 ...

  7. Makefile eval函数

    https://www.cnblogs.com/gaojian/archive/2012/10/04/2711494.html对 makefile 中 eval 函数的学习体会 http://blog ...

  8. makefile的函数集合

    strip函数:$(strip text) 函数功能:去除字符串空格函数 示例: STR =        a    b c      LOSTR = $(strip $(STR)) #结果是&quo ...

  9. Makefile 赋值 函数定义 等小知识点

    1.赋值 == 到用的时候实际才去赋值:= 立刻赋值?= 未赋值才赋值+= 2.多层变量 多层变量引用(各种复杂组合...)a =bb= cc= dd =1$($($($(a)))) 最终等于1 3. ...

随机推荐

  1. 关于nginx配置的一个报错connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory)

    针对配置php的情况: linux服务器一般提示这个 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) ...

  2. Python笔记_第三篇_面向对象_3.重载(overloading)和重写(overriding)

    1. 重载: overloading:就是将函数重新定义一遍. 1.1 __str__( )和__repr__( )的重载: 格式: __str__( ):在调用print打印对象时自动调用,是给用户 ...

  3. 当年写的C代码

    #ifndef KMIN_H_ #define KMIN_H_ /******************************************************************* ...

  4. Codeforces Round #600 (Div. 2)E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  5. CodeForces 527C. Glass Carving (SBT,线段树,set,最长连续0)

    原题地址:http://codeforces.com/problemset/problem/527/C Examples input H V V V output input H V V H V ou ...

  6. 吴裕雄--天生自然python机器学习:决策树算法

    我们经常使用决策树处理分类问题’近来的调查表明决策树也是最经常使用的数据挖掘算法. 它之所以如此流行,一个很重要的原因就是使用者基本上不用了解机器学习算法,也不用深究它 是如何工作的. K-近邻算法可 ...

  7. D. Salary Changing(找中位数)

    题:https://codeforces.com/contest/1251/problem/D 题意:给你n个单位需要满足达到的区间,再给个s,s是要分配给n的单位的量,当然∑l<=s,问经过分 ...

  8. 主效应|处理误差 |组间误差|处理效应|随机误差|组内误差|误差|效应分析|方差齐性检验|SSE|SSA|SST|MSE|MSA|F检验|关系系数|完全随机化设计|区组设计|析因分析

    8 什么是只考虑主效应的方差分析? 就是不考虑交互效应的方差分析,即认为因素之间是不相互影响的,就是无重复的方差分析.   什么是处理误差 (treatment error).组间误差(between ...

  9. Web Service概述 及 应用案例

    Web Service的定义  W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用 ...

  10. Date类与SimpleDateFormat类中parse()方法和format()方法

    package ppt11util类; import java.text.ParseException; import java.text.SimpleDateFormat; import java. ...