2016-08-04 111 views
1

Abort makefile if variable not set處接收到建議我試圖使用ifndef,但它不起作用。Makefile - 如果變量未設置,則發生錯誤

的Makefile:

foo: 
     ifndef BAR 
     $(error "Must set BAR") 
     endif 

make foo BAR=quux不起作用:

Makfile:2: *** "Must set BAR". Stop. 

是怎麼回事?它是否是空白問題?或者我完全誤解了?

(我使用GNU MAKE)

我也試過:

foo: 
ifndef BAR 
$(error "Must set BAR") 
endif 
     echo $(BAR) 

這似乎排序的工作,但還是引起了,如果要調用的ifndef一個目標在Makefile進一步下跌(不需要設置變量)被調用。

回答

3

從技術上講,是的,這是一個空白問題,但更根本的是它是一個範圍問題。

ifndef... endif使有條件。所以Make會在執行任何規則之前評估它。由於ifndef不是配方的一部分,因此它不應該以TAB開頭。這工作:

ifndef BAR 
$(error "Must set BAR") # this is a Make error 
endif 

foo: 
ifndef BAR 
    echo "Must set BAR" # this is a shell command 
    exit 1    # this is another shell command 
endif 

但你所做的是這樣的:

foo: 
    ifndef BAR 
    $(error "Must set BAR") 
    endif 

你把TAB在三線(ifndef...$(error...endif)的前面,所以要假定這些是命令,在執行foo配方時傳遞給shell。當Make執行規則時,它將Make-syntax語句擴展爲$(error...) - 並在傳遞任何內容之前將其終止並顯示一個錯誤。

0

您可以使用「if」功能來達到此目的。 foo : $(if $(BAR),,$(error Must set BAR))