一、示例
- 使用 ifeq 、else、endif 来执行满足条件的语句
# 定义库的变量
libs_for_gcc = -lgnu
normal_libs
# 生成的目标
foo : $(objects)
# 判断$(CC)是否为gcc
ifeq($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
# $(CC)不是gcc
else
$(CC) -o foo $(objects) $(normal_libs)
# 结束符
endif
libs_for_gcc = -lgnu
normal_libs
ifeq($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
foo : $(objects)
$(CC) -o foo $(objects) $(libs)
二、语法
- 条件表达式的语法为
-
关键字
有 ifeq、ifneq、ifdef、ifndef
<conditional-directive> #关键字
<text-if-true>
endif
# 以及
<conditional-directive> #关键字
<text-if-true>
else
<text-if-false>
endif
-
第一个关键字 ifeq
- 若、相同则为真,则执行真的命令语句
ifeq (<arg1>,<arg2>)
ifeq '<arg1>''<arg2>'
ifeq "<arg1>""<arg2>"
ifeq "<arg1>"'<arg2>'
ifeq '<arg1>'"<arg2>"
# strip函数判断变量foo的值是否为空,
# 若为空则执行<text-if-empty>
ifeq ($(strip,$(foo)))
<text-if-empty>
endif
-
第二个关键字 ifneq
- 若、不相同则为真,则执行真的命令语句
ifneq (<arg1>,<arg2>)
ifneq '<arg1>''<arg2>'
ifneq "<arg1>""<arg2>"
ifneq "<arg1>"'<arg2>'
ifneq '<arg1>'"<arg2>"
# 若<variable-name>非空,ifdef则为真
# 此示例 $(frobozz)的值为yes
bar = # bar值为空
foo = $(bar) # 但是$(bar)这个变量名不是空的
ifdef foo
frobozz = yes
else
forbozz = no
endif
# 此示例 $(frobozz)的值为no
foo = # foo为空
ifdef foo
frobozz = yes
else
frobozz = no
endif
# 若<variable-name>w为空,ifdef则为真