Makefile的嵌套使用
调用子makefile
定义命令包
类似函数
条件判断
关键字有:ifeq,ifneq,ifdef,ifndef
ifeq ($(CC),gcc)
$(CC) -o xxx xxx
else
$(CC) -o xx xx
endif
自带函数
字符串函数
函数 | 含义 |
---|---|
$(subst ,
| 字符串替换函数 |
$(patsubst ,,
| 模式字符串替换函数 |
$(strip ) | 去空格函数 |
$(findstring ,) | 查找字符串函数 |
$(filter <pattern…>,
| 过滤函数 |
$(filter-out <pattern…>,
| 反过滤函数 |
$(sort ) | 排序函数 |
$(word ,
| 取单词函数 |
$(wordlist | 取单词串函数 |
$(words
| 单词个数统计函数 |
$(firstword
| 首单词函数 |
例:
sources := main.c foo.c bar.s other.c include.h
foo: $(sources)
cc $(filter %.c %.s,$(sources)) -o foo
$(filter %.c %.s,$(sources))
返回的值是:main.c foo.c bar.s other.c
文件名相关的函数
函数 | 含义 |
---|---|
$(dir <names…>) | 取目录函数 |
$(notdir <names…>) | 取文件函数 |
$(suffix <names…>) | 取后缀函数 |
$(basename <names…>) | 取前缀函数 |
$(addsuffix ,<names…>) | 加后缀函数 |
$(addprefix ,<names…>) | 加前缀函数 |
$(join ,) | 连接函数 |
例:$(basename src/main.c src/other.c hacks)
返回值是:src/main src/other hacks
循环
foreach
$(foreach ,,
names := a b c d
files := $(foreach n,$(names),$(n).o)
$(files)
的值是 : a.o b.o c.o d.o
条件
if
$(if <condition>,<then-part>)
$(if <condition>,<then-part>,<else-part>)
val := a
objects := $(if $(val),$(val).o,nothing)
其他
call
call函数是唯一一个可以用来创建新的参数化的函数
$(call ,,,…,)
rever = $(1)$(2)
foo = $(call rever,a,b)
shell
$(shell <shell命令>)
执行shell命令并将返回结果作为返回值
contents := $(shell cat foo)
files := $(shell echo *.c)