Makefile $ {command)不起作用,但是`command`起作用了 [英] Makefile $(command) not working but `command` did

查看:82
本文介绍了Makefile $ {command)不起作用,但是`command`起作用了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The thing is when I was writing a Makefile for my project, when I needed to detect the current branch name, in a make rule I did this :

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)" 不起作用,它返回了"空字符串. 但是,当我将 $()更改为``时,它运行得很好.

When I called make check_branch, the "$(git rev-parse --abbrev-ref HEAD)" didn't work, it returned "" empty string. But instead when I changed $() to ` `, it worked perfectly.

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

为什么$()不起作用而``起作用了?仅用于"git"命令.

Why is $() not working but `` did? Only for the "git" command.

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

Note that in my Makefile, I used $() normally in many rules.

谢谢:)

推荐答案

在内部配方中,您必须转义美元符号才能使其被外壳解释.否则,Make会将$(git ...)视为内置命令或变量,并尝试对其求值(当然,不成功).

Inside recipes you have to escape dollar signs in order to get them interpreted by the shell. Otherwise, Make treats $(git ...) as a built-in command or a variable and tries to evaluate it (of course, unsuccessfully).


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

这篇关于Makefile $ {command)不起作用,但是`command`起作用了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆