【问题标题】:What's wrong with the following GNU make shell variable expansion?下面的 GNU make shell 变量扩展有什么问题?
【发布时间】:2011-10-25 13:36:43
【问题描述】:

在这一行:

GCCVER:=$(shell a=`mktemp` && echo $'#include <stdio.h>\nmain() {printf("%u.%u\\n", __GNUC__, __GNUC_MINOR__);}' | gcc -o "$a" -xc -; "$a"; rm "$a")

我明白了:

*** unterminated call to function `shell': missing `)'.  Stop.

我愚蠢的迂回变量有什么问题?

更新0

$ make --version
GNU Make 3.81
$ bash --version
GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu)
$ uname -a
Linux 2.6.38-10-generic #46-Ubuntu SMP x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

【问题讨论】:

  • 顺便说一句,$(shell gcc -v 2&gt;&amp;1 | tail -1 | awk '{print $$3}') 有什么问题?
  • @reinierpost:除了我不知道之外什么都没有。

标签: shell gcc makefile gnu-make variable-expansion


【解决方案1】:

当在 Makefile 中对 Bash 使用 $ 时,您需要将它们加倍:例如 $$a。我不熟悉符号$',但我必须假设你知道你在做什么。除非它是一个 Makefile 结构,否则你也需要在那个结构上加倍美元符号。

此外,井号 # 正在终止 Make 评估中的 shell 扩展,这就是它永远不会看到正确括号的原因。逃避它会有所帮助,但我还没有让它正常工作。

我通过两个步骤对其进行调试:首先将 GCCVER 设置为不包含 $(shell) 的命令列表,然后在第二步设置 GCCVER := $(shell $(GCCVER))。您可能也想尝试一下,当它不起作用时注释掉$(shell) 步骤,使用export,并制作一个“设置”配方:

GCCVER := some commands here
#GCCVER := $(shell $(GCCVER))  # expand the commands, commented out now
export  # all variables available to shell
set:
        set  # make sure this is prefixed by a tab, not spaces

然后:

make set | grep GCCVER

[更新]这行得通:

GCCVER := a=`mktemp` && echo -e '\#include <stdio.h>\nmain() {printf("%u.%u\\n", __GNUC__, __GNUC_MINOR__);}' | gcc -o "$$a" -xc -; "$$a"; rm "$$a"
GCCVER := $(shell $(GCCVER))
export
default:
    set

jcomeau@intrepid:/tmp$ make | grep GCCVER
GCCVER=4.6

又绕了一圈,去掉了多余的步骤:

jcomeau@intrepid:/tmp$ make | grep GCCVER; cat Makefile 
GCCVER=4.6
GCCVER := $(shell a=`mktemp` && echo -e '\#include <stdio.h>\nmain() {printf("%u.%u\\n", __GNUC__, __GNUC_MINOR__);}' | gcc -o "$$a" -xc -; "$$a"; rm "$$a")
export
default:
    set

使用$' Bash 构造:

jcomeau@intrepid:/tmp$ make | grep GCCVER; cat Makefile 
GCCVER=4.6
GCCVER := $(shell a=`mktemp` && echo $$'\#include <stdio.h>\nmain() {printf("%u.%u\\n", __GNUC__, __GNUC_MINOR__);}' | gcc -o "$$a" -xc -; "$$a"; rm "$$a")
export
default:
    set

由于您的系统与我的系统运行方式不同,我将逃避并说要么使用 reinierpost 的建议,要么:

GCCVER := $(shell gcc -dumpversion | cut -d. -f1,2)

【讨论】:

  • 仍在测试...这不是您的全部问题。
  • 回显的 -e 选项对于插入 \n 是必需的。
  • $'' 像 C 字符串一样评估内容。
  • 好的,我相信你的话!但正如你所见,没有它它也能工作。
  • 非常努力,但我收到了&lt;stdin&gt;:1:1: error: stray ‘#’ in program
猜你喜欢
  • 1970-01-01
  • 2023-01-23
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
相关资源
最近更新 更多