2011-06-29 153 views
7

我想根據條件在Makefile中定義變量。由於ifeq只能在規則中運行,因此我爲每個規則添加了一個額外的規則(def_rule)。使用ifeq在Makefile中定義條件變量

例子:

def_rule: 
ifeq ($(TARGET), android) 
    CC=arm-linux-androideabi-gcc 
else 
    echo "native build" 
endf 

all: def_rule tp xi_eid_chipset.o 

不幸的是,調用使所有返回此:

ifeq (linux, android)
/bin/sh: Syntax error: word unexpected (expecting ")")
make: *** [def_rule] Error 2

我想不通爲什麼。我剛剛遵循GNU Make文檔中的例子。

你知道如何在Makefiles中做條件定義嗎?

回答

9

條件句可以的規則之外:

ifeq ($(TARGET), android) 
$(info Android) 
CC=arm-linux-androideabi-gcc 
else 
$(info native build) 
CC=something else 
endif 

(請注意,我在幾個領先的空間已經扔了,只是爲了更容易地read--他們沒有必要,也不是有害的。)