GNU make支持内置函数以及用户自定义函数,下面结合例子简单介绍一下。

gnu make版本: 4.1

一、用户自定义函数

格式: $(call macro-name{, param1 ···})

解析: macro-name可以是任意宏或变量,macro-name之后是宏的参数,并以逗号为分隔符。

例子:

 define test-call
echo "call has two parameters: $1, $2"
endef .PTHONY: simple-test
simple-test:
@$(call test-call,one,two)

运行结果:

  make simple-test

  call has two parameters: one, two

二、内置函数

字符串函数

1、filter

格式: $(filter pattern ···text)

解析: filter函数会将text视为一系列被空格隔开的单词,与pattern比较之后接着会返回相符者。

例子:

words :=  GNU is not unix and linux is not unix

.PTHONY: simple-test
simple-test:
@echo words: $(words)
@echo unix matches: $(filter unix, $(words))

运行结果:

  make simple-test

  words: GNU is not unix and linux is not unix
  unix matches: unix unix

2、filter-out

格式: $(filter-out patern...,text)

解析:这个函数功能与filter刚好相反

例子:

words :=  GNU is not unix and linux is not unix

.PTHONY: simple-test
simple-test:
@echo words: $(words)
@echo unix matches: $(filter-out unix, $(words))

运行结果:

  make simple-test

  words: GNU is not unix and linux is not unix

  unix matches: GNU is not and linux is not

3、findstring

格式: $(findstring string...,text)

解析: 此函数将会在text里面搜索string。如果该字符被找到了,此函数就会返回string,否则,它会返回空值。

例子:

words :=  GNU is not unix and linux is not unix

.PTHONY: simple-test
simple-test:
@echo words: $(words)
@echo unix matches: $(findstring unix, $(words))

运行结果:

  make simple-test
  words: GNU is not unix and linux is not unix
  unix matches: unix

4、subst

格式: $(subst search-string,replace-string, text)

解析:这是一个不具通配符能力的”搜索和替换“函数。它最常被用来在文件名列表将一个扩展名替换成另一个扩展名

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(subst .c,.o,$(sourcelist))

运行结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: GNU.o is.o not.o unix.o and.o linux.o is.o not.o unix.o

这可以将在soucelist里面所有出现.c字样的地方都替换成.o。

5、pathsubst

格式: $(pathsubst search-pattern,replace-pattern,text)

解析: 这是一个具有通配符能力的”搜索和替换“函数。

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(patsubst %nix.c, UNIX,$(sourcelist))

运行结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: GNU.c is.c not.c UNIX and.c linux.c is.c not.c UNIX

6、words

格式: $(words text)

解析:此函数会返回text中单词的数量

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(words $(sourcelist))

运行结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: 9

7、words后面带n

格式:$(words n,text)

解析: 此函数会返回text中的第n个单词,第一个单词的编号为1。如果n的值大于text中单词的个数,则此函数将会返回空值。

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(words ,$(sourcelist))

测试结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: 9

没有返回预想的值,好奇怪。

8、firstword

格式: $(firstword text)

解析: 此函数会返回text中的第一个单词。

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(firstword $(sourcelist))

运行结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: GNU.c

9、wordlist

格式: $(wordlist start,end,text)

解析: 此函数会返回text中范围从start(含)到end(含)的单词。

例子:

sourcelist :=  GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c

.PTHONY: simple-test
simple-test:
@echo sourcelist: $(sourcelist)
@echo unix matches: $(wordlist ,,$(sourcelist))

运行结果:

  make simple-test
  sourcelist: GNU.c is.c not.c unix.c and.c linux.c is.c not.c unix.c
  unix matches: GNU.c is.c not.c

时间关系,先介绍到这。

makefile学习之函数的更多相关文章

  1. [转]Windows平台下Makefile学习笔记

    Windows平台下Makefile学习笔记(一) 作者:朱金灿 来源:http://blog.csdn.net/clever101 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译 ...

  2. makefile学习(1)

    GNU Make / Makefile 学习资料 GNU Make学习总结(一) GNU Make学习总结(二) 这篇学习总结,从一个简单的小例子开始,逐步加深,来讲解Makefile的用法. 最后用 ...

  3. JavaScript学习09 函数本质及Function对象深入探索

    JavaScript学习09 函数本质及Function对象深入探索 在JavaScript中,函数function就是对象. JS中没有方法重载 在JavaScript中,没有方法(函数)重载的概念 ...

  4. makefile学习小结

    =============2016/08/15================ 上午完成makefile的试验,缩短了代码量,现在make强大,有缺省的变量,能自己推导关系,不需要gcc –MM -M ...

  5. Linux makefile教程之函数七[转]

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

  6. C++学习之函数指针

     C++学习之函数指针          和数据项类似,函数也有地址,函数的地址是存储在机器语言代码的内存的开始地址.通常,这些地址对用户而言,不重要也没什么用处,但对程序而言,它却很有用. 一.函数 ...

  7. Javascript学习5 - 函数

    原文:Javascript学习5 - 函数 在Javascript中,函数和对象是交织在一起的.有些函数的特性与对象相关联.这一点的内容在第六部分会讨论到. 这一部分主要讨论函数与其它比较熟悉的语言( ...

  8. linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words

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

  9. [arm学习]makefile学习总结

    makefile不仅仅是一个命令的集合体,其中有一些规则是需要理解掌握的. 首先,了解makefile的规则: //-----------格式---------- 目标 : 依赖1,依赖2 (TAP键 ...

随机推荐

  1. ssm整合的springmvc.xml的配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. Canal——增量同步MySQL数据到ElasticSearch

    1.准备 1.1.组件 JDK:1.8版本及以上: ElasticSearch:6.x版本,目前貌似不支持7.x版本:       Kibana:6.x版本:     Canal.deployer:1 ...

  3. HashPump用法

    做哈希长度扩展攻击的时候用到这个工具,但是没找到这个工具详解办法 我这篇不算是详解,只是收集例子做出的一个用法 HashPump一种在各种散列算法中利用散列长度扩展攻击的工具.目前支持的算法:MD5, ...

  4. Java之属性和普通方法

    一.定义类 上一节讲了很多深奥的理论,那么这节我们就得实践一下,先简单描述一下我们的实体世界:有一个学生小明,那么这个学生就是一个对象,这个对象有哪些属性和方法呢,我们可以先简单抽象一下,属性有(姓名 ...

  5. ETH挖矿

    转载声明:http://www.120btc.com/baike/coin/1021.html (仅为了方便自己及原文章被删除) 最近以太坊大涨市值成为仅次比特币的第二大数字货币,那么以太坊(ETH) ...

  6. Java垃圾收集器与内存分配策略

    程序的计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭:栈中的栈侦随着方法的进入和退出而有条不紊地执行出栈和如栈操作. 判断对象是不是已经死亡的方法: 一.引用计数算法: 给对象添加一个引用 ...

  7. C#_初识之HelloWorld

    C#————是一个我很不愿意去碰的语言,但是还是得低头,巨硬还是巨硬....... ———————————————————————————————————————————————— 前几天在Githu ...

  8. win系统常用命令

    windows常用命令 net user 用户名 密码 /add (建立用户) net localgroup administrators 用户名 /add (将用户加到管理员,使其拥有管理权限) n ...

  9. 【LOJ】#3042. 「ZJOI2019」麻将

    LOJ#3042. 「ZJOI2019」麻将 如何判定一个集合牌有没有胡的子集是不是胡的 就用一个\(dp[j][k][0/1]\)表示有j个连续两个的串,有k个连续1个串,有没有对子,再记一下这个集 ...

  10. ABP领域层创建实体

    原文作者:圣杰 原文地址:ABP入门系列(2)——领域层创建实体 在原文作者上进行改正,适配ABP新版本.内容相同 这一节我们主要和领域层打交道.首先我们要对ABP的体系结构以及从模板创建的解决方案进 ...