2016-08-04 107 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))