Makefile语法进阶

2 篇文章 0 订阅

Makefile基本语法

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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值