makefile使用条件判断

来源:互联网 发布:毛新宇是不是装的知乎 编辑:程序博客网 时间:2024/05/17 04:54
1、条件关键字四个

a、ifeq

ifeq (<arg1>,<arg2>)

ifeq '<arg1>' '<arg2>'

ifeq "<arg1>" "<arg2>"

ifeq "arg1" 'arg2'

ifeq 'arg1' "arg2"

endif

关键字比较参数arg1和arg2的值是否相同,如果相同则表达式为真,否则为假。

例:

a=1b=2all:ifeq ($a,$b)echo "a=b"elseecho "a!=b"endififeq '$a' '$b'echo "a=b"elseecho "a!=b"endififeq "$a" "$b"echo "a=b"elseecho "a!=b"endififeq '$a' "$b"echo "a=b"elseecho "a!=b"endififeq "$a" '$b'echo "a=b"elseecho "a!=b"endif

make -s -all

a!=b
a!=b
a!=b
a!=b
a!=b

b、ifneq

ifneq (<arg1>,<arg2>)

ifneq '<arg1>' '<arg2>'

ifneq "<arg1>" "<arg2>"

ifneq "arg1" 'arg2'

ifneq 'arg1' "arg2"

endif

关键字比较参数arg1和arg2的值是否相同,如果不相同则表达式为真,否则为假。

例:

a=1b=2all:ifneq ($a,$b)echo "a=b"elseecho "a!=b"endififneq '$a' '$b'echo "a=b"elseecho "a!=b"endififneq "$a" "$b"echo "a=b"elseecho "a!=b"endififneq '$a' "$b"echo "a=b"elseecho "a!=b"endififneq "$a" '$b'echo "a=b"elseecho "a!=b"endif
make  -s -all

a=b
a=b
a=b
a=b
a=b

c、ifdef 

ifdef关键字表示如果值非空则执行,其格式如下
ifdef <variable-name>

ifdef关键字判断变量<variable-name>的值,如果非空,则表达式为真否则为假。

例:

a=ab=ball:ifdef aecho $aelseecho nullendififdef becho $belseecho nullendififdef cecho $celseecho nullendif

make -s -all

a
b
null

d、ifndef关键字表示如果为空则执行,其格式如下

ifndef <variable-name>

ifndef关键字判断变量<variable-name>的值。如果为空则表示为真,否则为假。

例:

a=ab=bc=all:ifndef aecho $aelseecho nullendififndef becho $belseecho nullendififndef cecho celseecho nullendif

make -s -all

null
null
c

<conditionnal-directive> 所在的行中,多余的空格是被允许的,但是不能以Tab键作为开始。而注释符“#”同样也是安全的。else和endif关键字也一样,不能以Tab键开始。


循环编译每个子目录的Makefile

SUBDIRS=src\secall:   @if test "$(SUBDIRS)" != ""; then \   it="$(SUBDIRS)" ; \   for i in $$it ; do \      echo "making all in `pwd`/$$i"; \      ( cd $$i ; $(MAKE) ) ; \      if test $$? != 0 ; then \         exit 1 ; \      fi  \   done \fi




原创粉丝点击