[英]What's wrong with the following GNU make shell 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.
我愚蠢的迂回变量有什么问题?
$ 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 构造,否则您也需要将美元符号加倍。
此外,hash 符号#
正在终止 Make 评估中的 shell 扩展,这就是它永远不会看到正确括号的原因。 escaping 它有帮助,但我还没有让它工作得很好。
我通过两个步骤对其进行调试:首先将 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.