【问题标题】:Error `*** missing separator. Stop.`: Is there a subtle difference in if statements in GNU Make preambles on Ubuntu and MacOs?错误 `*** 缺少分隔符。停止。`:在 Ubuntu 和 MacOs 上 GNU Make 序言中的 if 语句是否存在细微差别?
【发布时间】:2022-01-18 13:16:46
【问题描述】:

当我运行 makefile 时

 SHELL=/bin/bash
 
 a = False
 ifeq ("", "")
   a = True
 endif
 
 #b = False
 #ifeq("","")
 #  b = True
 #endif
 
 #c = False
 #if [ "" == "" ] \
 #then \
 #  c = True \
 #fi
 
 #d = False
 #if [[ "" == "" ]] \
 #then \
 #  d = True \
 #fi

 all :
     @\
     echo a = $(a); \
     echo b = $(b); \
     echo c = $(c); \
     echo d = $(d); \
     \
     c_loc=False; \
     if [ "" == "" ]; \
     then \
       c_loc=True; \
     fi; \
     \
     d_loc=False; \
     if [[ "" == "" ]]; \
     then \
       d_loc=True; \
     fi; \
     \
     echo c_loc = $${c_loc}; \
     echo d_loc = $${d_loc}; \

我得到了输出

a = True
b =
c =
d =
c_loc = True
d_loc = True

在 Ubuntu 20.04 上使用 GNU Make 4.2.1 时,取消注释 if 语句 c 或 d 会导致错误 Makefile:n: *** missing separator. Stop.n 是 if 语句 c 或 d 的行号。

另一方面,如果我在带有 GNU Make3.81 的 Mac OS Big Sur v11.6.1 上运行相同的 makefile(if 语句 c 和 d 未注释),我会得到输出

a = True
b =
c = False
d = False
c_loc = True
d_loc = True

取消注释 if 语句 b 会导致两种设置中缺少分隔符错误。

我的问题:

  • 为什么在一个设置中,如果语句 c 和 d 出现缺少分隔符错误,而在另一个设置中却没有?这是由于 GNU Make 3 和 GNU Make 4 之间的差异,还是 Ubuntu 和 MacOs 之间的差异?
  • 为什么我在两个设置中都没有得到 if 语句 c 和 d 的 True 输出?我在哪里可以找到关于序言中使用的 shell 的信息?

【问题讨论】:

    标签: bash makefile gnu-make


    【解决方案1】:

    我不知道你所说的“序言”是什么意思; makefile 中没有“序言”之类的东西。 Makefiles 不是 shell 脚本,尽管它们在配方中包含 shell 脚本。唯一可以使用 shell 脚本语法的地方是配方中。自从 1970 年代发明 make 以来,情况一直如此。 if [...]; then; fi 之类的内容是 shell 脚本语法,因此只能出现在食谱中。

    如果你想在配方之外使用 if 语句,你必须使用 makefile syntax,而不是 shell 语法。

    至于为什么它在 GNU make 3.81 中“有效”(注意,它真的无效,因为无论您输入什么值,它都不会重置 c 变量的值在这种情况下),这是因为在旧版本的 GNU 中,创建由空格组成的变量名过去是合法的。所以这个:

    c = False
    if [ "" == "" ] \
    then \
        c = True \
    fi
    

    被解释为这两行(反斜杠换行符减少后):

    c = False
    if [ "" == "" ] then c = True fi
    

    在旧版本的 GNU make 中,变量名可以包含空格,这会将名为 if [ "" == "" ] then c 的变量设置为值 True fi。这是合法的语法,但它显然没有达到你的预期。

    在较新版本的 GNU make 中,我们不允许变量名包含空格,因此上述文本是无效的语法,您会得到 make 的标准语法错误缺少分隔符

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 2022-01-05
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2020-05-02
      相关资源
      最近更新 更多