在make中未设置变量时获取默认值 [英] get a default value when variable is unset in make
问题描述
(根据@Michael反馈,问题更准确)
(edit: question more accurate based on @Michael feedback)
在bash中,我经常使用参数扩展:未设置$VARNAME
时,以下命令将显示"default value
",否则将显示VARNAME内容.
In bash, I often use parameter expansion: the following commands print "default value
" when $VARNAME
is unset, otherwise it prints the VARNAME content.
echo ${VARNAME:-default value} #if VARNAME empty => print "default value"
echo ${VARNAME-default value} #if VARNAME empty => print "" (VARNAME string)
我未找到的类似功能GNU make
.
我终于在我的Makefile
中写道:
I did not find a similar feature on GNU make
.
I finally wrote in my Makefile
:
VARNAME ?= "default value"
all:
echo ${VARNAME}
但是我对这种解决方案不满意:它总是创建变量VARNAME
,这可能会更改某些makefile上的行为.
But I am not happy with this solution: it always creates the variable VARNAME
and this may change the behavior on some makefiles.
是否有更简单的方法来获取未设置变量的默认值?
推荐答案
如果您要使用GNU make变量的扩展名(如果它是非空的)和默认值(如果它是空的),但不设置该变量,您可以执行以下操作:
If you want to use the expansion of a GNU make variable if it is non-empty and a default value if it is empty, but not set the variable, you can do something like this:
all:
echo $(or $(VARNAME),default value)
这篇关于在make中未设置变量时获取默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!