7.Makefile中的条件语句

不能用于控制recipes

warning:不能在Makefile.am中使用。

例子

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

带有tab才能算作recipe,这里的条件语句被当作文本来看待。

语法

conditional-directive-one
text-if-one-is-true
else conditional-directive-two
text-if-two-is-true
...
else
text-if-one-and-two-are-false
endif

else可省略。

测试变量是否相等,有五种条件conditional-directive的写法:

ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"

测试变量是否为空值(类似于""):

ifeq ($(strip $(foo)),)
text-if-empty
endif

测试变量是否不等,也有五种写法:

ifneq (arg1, arg2)
ifneq 'arg1' 'arg2'
ifneq "arg1" "arg2"
ifneq "arg1" 'arg2'
ifneq 'arg1' "arg2"
ifdef variable-name

ifdef获取变量的名字作为参数,而不是变量值的引用。如果变量的值是非空的,则执行text-if-truevariable-name是可扩展的,所以其可以是一个扩展成一个变量名的变量或函数。 noteifdef只测试值是否为非空。所以除了像这样定义的变量:foo =,都会返回真。 如果想测试空值,使用ifeq

例如:

bar =
foo = $(bar)
ifdef foo
frobozz = yes
else
frobozz = no
endif

结果是:frobozz = yes

foo =
ifdef foo
frobozz = yes
else
frobozz = no
endif

结果是:frobozz = no

ifndef variable-name

ifdef相反。

make是在读取Makefile的时候判断条件的,所以不能在条件的测试中使用自动变量,因为直到recipes运行时他们才被定义。参考自动变量