在 Makefile if 语句中获取退出代码 1

Ger*_*rdo 3 makefile exit-code

如果该语句不正确,我试图获取 ifdef 语句上的退出代码,但我尝试使用 exit 1 和 $(call exit 1)

当在以下代码中使用第一个时,我得到“Makefile:11:*缺少分隔符。停止。”

...

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    exit 1
endif
Run Code Online (Sandbox Code Playgroud)

...

通过使用$(call exit 1),我没有得到任何错误,但 makefile 仍然继续执行。我想要完成的是在 else 上退出 Makefile,错误代码为 1

谢谢

Mad*_*ist 8

正如 geekosaur 所说,你不能像exit 1makefile 操作那样放置 shell 命令。Makefile 不是 shell 脚本,尽管它们可以包含shell 脚本。Shell 命令只能出现在目标配方中,而不能出现在其他地方。

如果您有足够新的 GNU make 版本,您可以使用该$(error ...)函数,如下所示:

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif
Run Code Online (Sandbox Code Playgroud)

另请注意,ifdef如果定义了变量,则该值为 true,即使它被定义为空字符串。您可能更喜欢:

ifneq ($(PACKAGE),)
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif
Run Code Online (Sandbox Code Playgroud)

确保变量设置为非空值。

而且,您的 GNU make 版本可能太旧而无法支持该$(error ...)功能,尽管它已经存在很长时间了。