【问题标题】:Makefile -- error out if variable not setMakefile -- 如果未设置变量,则会出错
【发布时间】:2016-12-11 04:33:10
【问题描述】:

遵循Abort makefile if variable not set 的建议,我正在尝试使用ifndef,但它不起作用。

生成文件:

foo:
        ifndef BAR
        $(error "Must set BAR")
        endif

但是make foo BAR=quux 不行:

Makfile:2: *** "Must set BAR". Stop.

什么给了?是空格问题吗?还是我完全误解了?

(我正在使用 GNU Make)

我也试过了:

foo:
ifndef BAR
$(error "Must set BAR")
endif
        echo $(BAR)

这似乎有点工作,但如果调用 Makefile 中更下方的目标(不需要设置变量),仍然会导致调用 ifndef

【问题讨论】:

    标签: makefile


    【解决方案1】:

    从技术上讲,是的,这是一个空白问题,但更根本的是,它是一个范围问题。

    ifndef... endif 是一个 Make 条件。所以 Make 会在执行任何规则之前对其进行评估。并且由于ifndef 不是配方的一部分,因此不应在其前面加上TAB。这有效:

    ifndef BAR
    $(error "Must set BAR") # this is a Make error
    endif
    
    foo:
    ifndef BAR
        echo "Must set BAR" # this is a shell command
        exit 1              # this is another shell command
    endif
    

    但你所做的是:

    foo:
        ifndef BAR
        $(error "Must set BAR")
        endif
    

    您在三行(ifndef...$(error...endif)前面放置了一个 TAB,因此 Make 假设这些是命令,当 foo 配方为执行。当 Make 执行规则时,它扩展了 Make-syntax 语句 $(error...) -- 并以错误终止 -- 然后将任何内容传递给 shell。

    【讨论】:

      【解决方案2】:

      您可以为此目的使用“if”函数。 foo : $(if $(BAR),,$(error Must set BAR))

      【讨论】:

        猜你喜欢
        • 2012-06-07
        • 2013-08-27
        • 1970-01-01
        • 2023-01-13
        • 2016-04-28
        • 2020-03-19
        • 1970-01-01
        相关资源
        最近更新 更多