【Scripts系列】之Makefile中条件分支ifeq/else/endif/else ifeq/ifneq/ifdef/ifndef用法详解

DATE: 2019-2-22


前言

      条件分支的用法在任何编程语言和脚本以及逻辑中都普遍使用。前文讲述过C语言中预处理的条件编译#ifdef/#if/#elif/#else/#endif用法详解,本文重点讲述Makefile编译脚本中条件分支ifeq/else/endif/else ifeq/ifneq/ifdef/ifndef的具体用法。linux

一、参考

https://blog.csdn.net/turkeyzhou/article/details/8613589
https://blog.csdn.net/u010312436/article/details/52459609
https://blog.csdn.net/tanyjin/article/details/66970155
https://blog.csdn.net/kwinway/article/details/79980616android

二、Makefile中条件分支的不一样使用形式

    使用条件判断,能够让make根据运行时的不一样状况选择不一样的执行分支。条件表达式能够是比较变量的值,或是比较变量和常量的值。ios

2.一、基本语法

条件表达式的语法为:编程

<conditional-directive>; 
    <text-if-true>; 
    endif 

以及: 

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

其中<conditional-directive>;表示条件关键字,如“ifeq”。这个关键字有四个,以下:编程语言

形式一:测试

ifeq(<arg1>, <arg2>)
语句1
else
语句2
endif

或者:spa

ifeq(<arg1>, <arg2>)
语句1
else ifeq(<arg3>, <arg4>)
语句2
else
语句3
endif

注意:其中<argv1>和<arg3>能够是make变量,好比$(PLATFORM).net

形式二:命令行

ifneq(<arg1>, <arg2>)
语句1
else
语句2
endif

形式三:code

ifdef  <variable-name>
语句1
endif

注意,ifdef只是测试一个变量是否有值,其并不会把变量扩展到当前位置。

形式四:

ifndef  <variable-name>
语句1
endif
2.二、多条件使用:单分支多个宏变量组合
ifeq ($(PLATFORM), $(findstring) $(PLATFORM), linux x86 android ios)
语句1
endif
三、ifdef, ifeq 使用及辨析
#能够用命令行传递变量
RELEASE = abc

#ifdef 变量名称不能加$()
ifdef RELEASE
$(warning RELEASE defined)
else
$(warning RELEASE not defined)
endif

#ifeq 后面参数要叫$(), 由于是值引用, 值能够为数值或字符串
ifeq ($(RELEASE),abc)
$(warning RELEASE eqal abc)
else
$(warning RELEASE not equal abc)
endif

all:
    @echo ok!

THE END!

相关文章
相关标签/搜索