shell - 以下 GNU make shell 变量扩展有什么问题?

标签 shell gcc makefile gnu-make variable-expansion

在这一行:

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

最佳答案

当在 Makefile 中使用 $ 进行 Bash 时,您需要将它们加倍:例如 $$a。我不熟悉 $' 符号,但我必须假设您知道您正在用它做什么。除非它是 Makefile 构造,否则您还需要将其上的美元符号加倍。

此外,哈希符号 # 会终止 Make 求值中的 shell 扩展,这就是为什么它永远看不到正确的括号。转义它有帮助,但我还没有让它正常工作。

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

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)

关于shell - 以下 GNU make shell 变量扩展有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7036352/

相关文章:

makefile - 为什么环境变量在 $(shell) 命令中不起作用?

linux - 在 make 规则中使用时,如何让 MAKEFILE 保留字符串中的反斜杠序列?

c - Ubuntu 上的 makefile 问题

shell - gawk:为什么 "next;"不抑制与模式匹配的行?

c++ - 模板特化静态初始化 icc+vc vs gcc+clang

python - 可以自动从服务器下载新数据到我本地备份的脚本

使用 GCC 生成连续的 Hex 文件

gcc 4.9 通用 lambda

linux - 仅列出文件; iconv 和目录?

unix - "x$VARIABLE"中 x 的 shell 脚本用途