m.s*_*tov 5 bash makefile conditional-compilation
我有一个带有如下命令的 Makefile:
#Makefile
hello:
echo 'hello'
echo $(TAG)
ifdef TAG
$(warning MYWARNING)
else
$(error MYERROR)
endif
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
# make TAG='1.0' hello
Run Code Online (Sandbox Code Playgroud)
我希望该命令执行 echo 'hello',然后 echo $(TAG) 和 $(warning MYWARNING) 但我得到:
Makefile:17: MYWARNING
Makefile:19: *** MYERROR. Stop.
Run Code Online (Sandbox Code Playgroud)
怎么了?
让我们尝试一些更简单的情况(*)。
hello:
echo hello
$(error MYERROR)
Run Code Online (Sandbox Code Playgroud)
这会产生:
Makefile:3: *** MYERROR. Stop.
Run Code Online (Sandbox Code Playgroud)
请注意,error阻止了echo,甚至认为它是随后出现的。
现在让我们尝试一些愚蠢的事情:
hello:
ifdef TAG
Run Code Online (Sandbox Code Playgroud)
结果是:
ifdef TAG
make: ifdef: No such file or directory
Run Code Online (Sandbox Code Playgroud)
“ifdef TAG”,解释为 shell 命令,没有任何意义。它被解释为 shell 命令,因为它位于配方中并且前面有一个 TAB。
现在让我们将它们结合起来:
hello:
ifdef TAG
$(error MYERROR)
Run Code Online (Sandbox Code Playgroud)
结果是Makefile:3: *** MYERROR. Stop.所以error掩盖了错误的事实ifdef...。
我们想要一个shell条件语句,还是一个Make条件语句?如果我们想让 Make 对其进行操作(使用error或warning),那么它必须是 Make 条件,因此我们不能在它前面加上 TAB:
hello:
ifdef TAG
$(warning MYWARNING)
else
$(error MYERROR)
endif
Run Code Online (Sandbox Code Playgroud)
这按预期工作。
(*) 正如您在发布之前应该尝试过的那样。