zoukankan      html  css  js  c++  java
  • Makefile系列之四 :条件判断

    一、示例
      下面的例子,判断$(CC)变量是否“gcc”,如果是的话,则使用GNU函数编译目标。
              libs_for_gcc = -lgnu
              normal_libs =
              foo: $(objects)
              ifeq ($(CC),gcc)
                $(CC) -o foo $(objects) $(libs_for_gcc)
              else
                $(CC) -o foo $(objects) $(normal_libs)
              endif
      当变量$(CC)值是“gcc”时,目标foo的规则是:
              foo: $(objects)
                $(CC) -o foo $(objects) $(libs_for_gcc)
      而当变量$(CC)值不是“gcc”时(比如“cc”),目标foo的规则是:
              foo: $(objects)
                $(CC) -o foo $(objects) $(normal_libs)
    二、语法
      条件表达式的语法为:
              <conditional-directive>
                <text-if-true>
              endif
      以及:
              <conditional-directive>
                <text-if-true>
              else
                <text-if-false>
              endif
      其中<conditional-directive>表示条件关键字.

      1)关键字“ifeq”。这个关键字有四个。第一个是我们前面所见过的“ifeq”
              ifeq (<arg1>, <arg2>)
              ifeq '<arg1>' '<arg2>'
              ifeq "<arg1>" "<arg2>"
              ifeq "<arg1>" '<arg2>'
              ifeq '<arg1>' "<arg2>" #比较参数“arg1”和“arg2”的值是否相同,相同则为真

      参数中使用make的函数。如:
              ifeq ($(strip $(foo)),)
                <text-if-empty>
              endif
      这个示例中使用了“strip”函数,如果这个函数的返回值是空(Empty),那么<textif-empty>就生效。
      2)关键字“ifneq”。语法是:
              ifneq (<arg1>, <arg2>)
              ifneq '<arg1>' '<arg2>'
              ifneq "<arg1>" "<arg2>"
              ifneq "<arg1>" '<arg2>'
              ifneq '<arg1>' "<arg2>" #比较参数“arg1”和“arg2”的值是否相同,如果不同,则为真。
      3)关键字“ifdef”。语法是:
              ifdef <variable-name>
      如果变量<variable-name>的值非空,表达式为真。否则,表达式为假。当然,<variable-name>同样可以是一个函数的返回值。注意,ifdef只是测试一个变量是否有值,其并不会把变量扩展到当前位置。
      示例一:
              bar =
              foo = $(bar)
              ifdef foo
                frobozz = yes
              else
                frobozz = no
              endif
      示例二:
              foo =
              ifdef foo
                frobozz = yes
              else
                frobozz = no
              endif
      第一个例子中,“$(frobozz)”值是“yes”,第二个则是“no”。
      4)关键字是“ifndef”。其语法是:
              ifndef <variable-name>
      它和“ifdef”是相反的意思。
      在<conditional-directive>这一行上,多余的空格是被允许的,但是不能以[Tab]键做为开始(不然就被认为是命令)。而注释符“#”同样也是安全的。“else”和“endif”也一样,只要不是以[Tab]键开始就行了。
      特别注意的是,make是在读取Makefile时就计算条件表达式的值,并根据条件表达式的值来选择语句,所以,你最好不要把自动化变量(如“$@”等)放入条件表达式中,因为自动化变量是在运行时才有的。

  • 相关阅读:
    Heapsort 堆排序算法详解(Java实现)
    GIve Me A Welcome Hug!
    linux系统救援模式拯救mv libc.so.6文件后无法使用命令的悲剧
    RabbitMQ集群部署
    使用Xshell通过堡垒机登录服务器
    dubbo + zookeeper环境部署
    zookeeper集群部署
    zabbix-3.0.1 添加微信报警
    zabbix-3.0.1结合grafana绘图
    Centos7.2安装zabbix3.0.1简要
  • 原文地址:https://www.cnblogs.com/lovemo1314/p/3486076.html
Copyright © 2011-2022 走看看