Makefile高级语法2

本文详细介绍了Makefile中的高级语法,包括条件判断关键字如ifeq、ifneq等,遍历函数、条件判断函数的使用,字符串处理和文件名处理函数的应用,以及Origin、Call、Shell函数的功能解析。通过实例展示了如何在Makefile中执行shell命令,以及错误、警告和信息函数的使用。目前的内容涵盖了GNU Makefile官方手册的前8章,后续章节敬请期待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章,我们学习条件关键字和常用函数

条件判断关键字

<conditional-directive>
<text-if-true>
else
<text-if-false>
endif

常用的有ifeq ifneq ifdef ifndef等,可在Conditonal_Makefile中看到案例。 终于ifeq等关键字之前没有tab。

遍历函数

$(foreach <var>,<list>,<text>)

遍历list, 赋给var,给text用。可在Foreach_Makefile中看到案例

条件判断函数

$(if condition,then-part[,else-part])
$(or condition1[,condition2[,condition3…]])
$(and condition1[,condition2[,condition3…]])

可在If_and_or_Makefile中看到案例

字符串处理函数

$(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)
$(wordlist s,e,text)
$(words text)
$(firstword names…)
$(lastword names…)

可在String_Makefile中看到案例 更详细的信息可在GNU Makefile官方手册-Text Function

文件名处理函数

$(dir names…)
$(notdir names…)
$(suffix names…)
$(basename names…)
$(addsuffix suffix,names…)
$(addprefix prefix,names…)
$(join list1,list2)
$(wildcard pattern)
$(realpath names…)
$(abspath names…)

可在File_Name_Makefile中看到案例 更详细的信息可在GNU Makefile官方手册-File-Name-Functions

Origin 函数

$(origin variable)

判断变量的来源,输出结果为undefined, default,environment等。由于简单,不做案例。 更详细的信息可在GNU Makefile官方手册-Origin-Function

Call 函数

$(call <expression>,<parm1>,<parm2>,<parm3>...)

创建新的参数化函数。示例如下

reverse = $(2) $(1)
foo = $(call reverse,a,b)

Shell 函数

$(shell <shell command>)

执行shell命令。示例如下

.PHONY : all
all: check_shell

check_shell:
	@echo $(shell echo "Hello World")
	@echo "Hello World"
	@echo `echo Hello World`

其三种形式的执行shell指令都是可以的。该案例可在Shell_Makefile中看到。

Error/Warn/Info函数

$(error text…)
$(warning text…)
$(info text…)

'error'函数可直接输出函数,并可以终止Makefile。'warning'和'info'则会输出相应的提示信息。案例可在Error_Warn_Info_Makefile中找到。

到目前为止,我们学习GNU Makefile官方手册前8章内容。 后续章节内容,请保持持续关注。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值