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

标签 makefile exit-code

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

在以下代码中使用第一个时,我得到“Makefile:11: * missing separator. Stop.”

...

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    exit 1
endif

...

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

谢谢

最佳答案

正如 geekosaur 所说,您不能将像 exit 1 这样的 shell 命令作为 makefile 操作。 Makefile 不是 shell 脚本,尽管它们可以包含 shell 脚本。 Shell 命令只能出现在目标配方中,不能出现在其他任何地方。

如果你有足够新的 GNU make 版本,你可以使用 $(error ...) 函数,像这样:

ifdef PACKAGE
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif

另外请注意,如果变量被定义,ifdef 将为真,即使它被定义为空字符串。您可能更喜欢:

ifneq ($(PACKAGE),)
    PACKAGEDIR = $(HOME)/$(PACKAGE)
else
    $(error You must define the PACKAGE variable)
endif

确保变量设置为非空值。

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

关于makefile - 在 Makefile if 语句中获取退出代码 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292132/

相关文章:

c++ - Eclipse CDT 显示一些错误,但项目已成功构建

visual-studio-2010 - Visual Studio 预生成事件 : Batch File Exit Code is -1

batch-file - 批量退出时指定退出码

makefile - 通过相同的 Makefile 生成 Makefile 的包含文件

c - 使用 GNU Make 迭代文件目录

c++ - 包含两个 .cpp 文件和一个头文件的基本 Makefile

bash - 我如何检查由 GNU Parallel 执行的并行运行的单个进程的退出代码

windows - Powershell where-object 返回代码

c++ - 如何捕获在 C++ 中运行的命令的 exit_code 和 stderr?

linux - 我的 Makefile 有问题吗?