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

问题描述 投票:0回答:3

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

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

...

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

...

通过使用

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

谢谢

makefile exit-code
3个回答
8
投票

正如 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

将为 true,

即使
它被定义为空字符串。您可能更喜欢: ifneq ($(PACKAGE),) PACKAGEDIR = $(HOME)/$(PACKAGE) else $(error You must define the PACKAGE variable) endif

确保变量设置为非空值。

并且,您的 GNU make 版本可能太旧,无法支持

$(error ...)

功能,尽管它已经存在很长时间了。

    


0
投票
exit 1

)始终与某种构建规则相关联。


在这种情况下,您需要

$(error) 函数

。不过,将其放入 
else 中可能还不够,因为同样的原因,
exit 1
本身在那里不起作用;你可能需要将整个事情改写为

PACKAGEDIR := $(if $(flavor PACKAGE),undefined,$(error PACKAGE must be defined!),$(HOME)/$(PACKAGE))



0
投票
@exit 1

。一个例子:

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

© www.soinside.com 2019 - 2024. All rights reserved.