Makefile:条件判断、循环、自定义函数(十)

1、条件判断
  • ifeq:if equal的缩写,判断是否相等,相等返回true,不相等返回false
  • ifneq:if not equal的缩写,判断是否不相等,不相等返回true,相等返回false
  • ifdef:if definite的缩写,判断变量是否存在,存在返回true,不存在返回false
  • ifndef:if not definite的缩写,判断变量是否不存在,不存在返回true,存在返回false
  • else:否则(可有可无)
  • endif:结束if判断(一定要写)
  • 注意:
    • ifeq、ifneq、ifdef、ifndef与后面的条件需要有空格,否则会语法错误
    • Makefile中没有elseif,如果需要多个条件判断需要嵌套if-else
    • Makefile中也可以没有else
1.1、ifeq、ifneq
A:=123
B:=456

ifeq ($(A), 456)
	RS1:=Yes
else
	ifeq ($(A), 789)
		RS1:=789
	else
		RS1:=No
	endif
endif

ifneq ($(B), 123)
	RS2:=Yes
else
	RS2:=No
endif

show:
	@echo "RS1 = "$(RS1)
	@echo "RS2 = "$(RS2)

# 输出
# RS1 = No
# RS2 = Yes
1.2、ifdef、ifndef
tar1:=z
ifdef tar1
tar1:=main
else
tar1:=$(tar1).o
endif

tar2=main
ifndef tar2
	tar2:=main
else
	tar2:=$(tar2).o
endif

show1:
	@echo $(tar1)
	@echo $(tar2)
# 输出
main
main.o
1.3、向Makefile中传入宏
ifndef t
	t:=default-main
endif

show:
	@echo "t="$(t)
        
# 输入:make show -f Makefile
# 输出:t=default-main
        
# 输入:make show -f Makefile t=main
# 输出:t=main
2、Makefile中的循环
  • makefile中支持循环的,并且循环有两种:
    • GNU平台下Makefile有默认的foreach循环
    • 其他平台可以调用shell脚本的循环
  • 调用shell指令可以通过$(shell 指令)或者指令的形式都可以
  • 下面举例分别利用GNU和shell的循环创建文件和文件夹
target:=a b c d
filename=$(foreach v, $(target), $v.txt)


show:
	@echo $(target)
	@echo $(foreach v, $(target), $v)
#	$(shell touch $(target))
#	touch $(filename)
#	mkdir $(foreach v, $(target), $v-txt)
	for v in $(target);\
		do touch $$v.txt;\
	done
	$(shell for v in $(target); do mkdir $$v-txt;done)

clean:
	${RM} -rf $(target) $(filename) *txt
3、自定义函数
  • Makefile中提供了自定义函数,但并非是真正的函数。本质是将多行命令放在了外面,使用时将其包含进来
    • 调用格式:$(call 函数名)
# 自定义函数并不是真正的函数,本质上是多行命令放在了外面,使用时将其包含进来
a:=123
b:=$(a)

define FUNC
	@echo "func"
	@echo $(shell ls)
	@echo $(a) $(b)
endef
a:=456

show:
	$(call FUNC)
	@echo "-----------------"
	@echo "func"
	@echo $(shell ls)
	
a:=789


# 输出
func
main.cpp Makefile
789 123
-----------------
func
main.cpp Makefile
789 123
  • 函数的传参问题,函数可以传参,但是没有返回值
    • 与C/C++的main参数一样,0号参数是本身,1号参数开始才是传入的参数
    • 调用格式:调用格式:$(call 函数名, arg1, arg2, …)
define FUNC
	@echo $(0)
	@echo $(1) $(2)
	@echo $(shell ls)
	return 123
endef

show:
	$(call FUNC,"123",abc)

# 输出
FUNC
123 abc
main.cpp Makefile
  • 自定义函数与引用=和:=赋值的组合效果
ans_reference:=$(call FUNC,"abc","def")
ans_assignment=$(call FUNC,"abc","def")
define FUNC
	@echo $(0)
	@echo $(1) $(2)
	@echo $(shell ls)
endef

ans:=$(call FUNC,"abc","def")

show:
	$(call FUNC,"123",abc)
	@echo "------------------"
	@echo $(ans_reference)
	@echo "------------------"
	@echo $(ans_assignment)
	@echo "------------------"
	@echo $(ans)

# 输出
FUNC
123 abc
main.cpp Makefile
------------------

------------------
@echo FUNC
abc def
main.cpp Makefile
------------------
@echo FUNC
abc def
main.cpp Makefile
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值