Makefile 如果参数未通过,则停止make

Makefile 如果参数未通过,则停止make,makefile,Makefile,我有一个生成文件 PATH = "MyFolder_"{ver} compile: [list_of_commands] $PATH 然后像这样运行它 make ver="1.1" compile #input make compile 如果未指定任何ver,如何停止编译 我想要这样的东西 make ver="1.1" compile #input make compile 然后输出 No version specified. Compilation terminated.

我有一个生成文件

PATH = "MyFolder_"{ver}

compile:
    [list_of_commands] $PATH
然后像这样运行它

make ver="1.1" compile
#input
make compile
如果未指定任何ver,如何停止编译 我想要这样的东西

make ver="1.1" compile
#input
make compile
然后输出

No version specified. Compilation terminated.

有很多方法可以做到这一点。这在一定程度上取决于您使用的make版本,以及您运行make的操作系统(shell make调用的)

注意:您不应该在makefile中使用变量
PATH
;这是系统的
路径
变量,重置它将破坏所有配方

另外,在变量中包含这样的引号通常是个坏主意。如果你想引用它,那么在食谱中添加引号

如果您有GNU make,您可以这样做:

ifeq ($(ver),)
$(error No version specified.)
endif
MYPATH = MyFolder_$(ver)
compile:
        [ -n "$(ver)" ] || { echo "No version specified."; exit 1; }
        [list_of_commands] "$(MYPATH)"
如果您没有GNU make,但使用的是UNIX系统或带有UNIX shell的Windows,则可以执行以下操作:

ifeq ($(ver),)
$(error No version specified.)
endif
MYPATH = MyFolder_$(ver)
compile:
        [ -n "$(ver)" ] || { echo "No version specified."; exit 1; }
        [list_of_commands] "$(MYPATH)"

如果您将Windows与Windows command.com一起使用,则可以执行类似的操作,但我不确定详细信息。

我有GNU制作。我尝试了第一个变量,它抛出错误“未指定版本”,即使我指定它
make ver=“1.1”compile
抛出错误=(@GriMel请确保您拥有准确的if语句,因为它应该可以正常工作。可能值得指出的是,当
ver
未按
$的方式设置时,后一种解决方案不会阻止其他目标运行(错误)
解决方案确实如此。@EtanReisner,等等,我可以将ifeq endif放在编译内部,还是只能在外部使用?@GriMel两者都可以。但对于这段代码,我认为这不会有什么不同(尽管我可能会在外部使用)。