【问题标题】:Makefile $(command) not working but `command` didMakefile $(command) 不起作用,但 `command` 起作用了
【发布时间】:2013-03-31 12:44:49
【问题描述】:

问题是当我为我的项目编写 Makefile 时,当我需要检测当前分支名称时,在 make 规则中我这样做了:

check_branch:
    if [ "$(git rev-parse --abbrev-ref HEAD)" == "master" ]; then \
    echo "In master"
    else \
    echo "Not in master"; \
    fi

当我调用 make check_branch 时,"$(git rev-parse --abbrev-ref HEAD)" 不起作用,它返回 "" 空字符串。 但是,当我将 $() 更改为 ` ` 时,它运行良好。

check_branch:
    if [ "`git rev-parse --abbrev-ref HEAD`" == "master" ]; then \
    echo "In master"
    else \
    echo "Not in master"; \
    fi

为什么 $() 不起作用但 `` 起作用了?仅适用于“git”命令。

请注意,在我的 Makefile 中,我在许多规则中都使用了 $()

谢谢:)

【问题讨论】:

  • 请参阅:Escaping in a Makefile。问题在于$(美元符号)对于make 是特殊的。如果你使用双美元,它应该可以工作。
  • 您还应该注意确保您位于git 层次结构中的目录中。
  • @artlessnoise:你说得对,非常感谢。 :)

标签: git makefile backticks


【解决方案1】:

在菜谱中,您必须转义美元符号才能让 shell 解释它们。否则,Make 将 $(git ...) 视为内置命令或变量并尝试对其求值(当然,不成功)。

check_branch: if [ "$$(git rev-parse --abbrev-ref HEAD)" == "master" ];然后 \ ...

【讨论】:

    【解决方案2】:

    在 shell 行中,您可以像在 shell 脚本中一样编写 shell 命令。这就是第二个操作有效的原因。

    如果你想让 Make 在 shell 之外评估 git 命令,你可以将操作包含在一个 shell 函数中,如下所示:

    $(shell git rev-parse --abbrev-ref HEAD)
    

    你应该很高兴,虽然我经常这样实现:

    branch := $(shell git rev-parse --abbrev-ref HEAD)
    target:dep
         mkdir -p build/$(branch)
    

    或者类似的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      相关资源
      最近更新 更多