【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的具体用法。

1、参考

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/79980616

2、Makefile中条件分支的不同使用形式

    使用条件判断,可以让make根据运行时的不同情况选择不同的执行分支。条件表达式可以是比较变量的值,或是比较变量和常量的值。

2.1、基本语法

条件表达式的语法为:

    <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

或者:

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

注意:其中<argv1>和<arg3>可以是make变量,比如$(PLATFORM)

形式二:

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

形式三:

ifdef  <variable-name>
语句1
endif

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

形式四:

ifndef  <variable-name>
语句1
endif
2.2、多条件使用:单分支多个宏变量组合
ifeq ($(PLATFORM), $(findstring) $(PLATFORM), linux x86 android ios)
语句1
endif
3、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!

原文地址:https://www.cnblogs.com/SoaringLee/p/10532151.html