在 Makefile 中使用$
for 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)