set -o nounset
确实很有用,但是我不想在Makefile中的每一行都运行它。通过Makefile中的 global 设置能否获得相同的效果?
答案 0 :(得分:1)
您可以在.SHELLFLAGS
变量since GNU make 3.82中提供全局shell设置。像这样:
$ cat Makefile
.SHELLFLAGS := -o nounset -c
all:
echo "Foo is $${FOO}"
unset:
set -o nounset; echo "Foo is $${FOO}"
make-resolved:
echo "Foo is ${FOO}"
输出:
$ make all
echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:4: all] Error 127
$ make unset
set -o nounset; echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:7: unset] Error 127
在没有.SHELLFLAGS
的情况下,第一次调用成功:
$ make all .SHELLFLAGS=-c
echo "Foo is ${FOO}"
Foo is
$ make unset .SHELLFLAGS=-c
set -o nounset; echo "Foo is ${FOO}"
/bin/sh: FOO: unbound variable
make: *** [Makefile:7: unset] Error 127
请注意,您需要转义$
才能使它实际由shell解析,而不是make解析。最后一个目标表明make将解决${FOO}
并且shell不会抱怨:
$ make make-resolved
echo "Foo is "
Foo is
在这种情况下,您也可以从make处获得警告(不是错误)since GNU make 3.68:
$ make --warn-undefined-variables make-resolved
Makefile:10: warning: undefined variable 'FOO'
echo "Foo is "
Foo is
答案 1 :(得分:0)
哦,我多么希望--error-undefined-variables
与现有的--warn-undefined-variables
一起使用。空变量始终是我的makefile中的错误。
在过渡期间,对于更复杂的扩展,我经常使用诸如$(call expand,var)
之类的东西而不是普通的${var}
来捕获令人发指的错误等。
expand = $(or ${$1},$(error [$1] is empty!))
可用于检查哈希值是否正确。
codename<0.0.1> := Initial
codename<0.2.0> := Anniversary
codename<1.0.a> := Beta
codename := $(call expand,codename<${version}>)
等