在 GNU makefile 中使用 bash 算术扩展
Using bash arithmetic expansion in GNU makefile
我想在 Makefile(GNU make + bash on Debian)中对二进制操作使用 bash
的算术扩展。 expr
未涵盖这些内容。我在 Makefile 中需要这个:
$ x=$(( 255 & 2#11110000)); echo $x
240
不起作用的东西:
$ cat Makefile
all: a b
a: # $ interpreted by make
x=$(( 255 & 2#11110000)); echo $$x
b: # escaped $
x=$$(( 255 & 2#11110000)); echo $$x
(a)
显然不行,(b)
也不行:
$ make b
x=$(( 255 & 2#11110000)); echo $x
/bin/sh: 1: arithmetic expression: expecting EOF: " 255 & 2#11110000"
有什么可行的办法吗?大量的引用、反引号和转义也没有产生任何结果。
添加
SHELL = bash
到 makefile
$ cat Makefile
# Make variable (recommended)
x := $(shell bash -c 'echo $$((255 & 2\#11110000))')
$(info [$x])
# Shell variable (not recommended)
a:
x=`bash -c 'echo $$((255 & 2#11110000))'`; echo $$x
给予
$ make
[240]
x=`bash -c 'echo $((255 & 2#11110000))'`; echo $x
240
不要忘记 #
在 makefile 中引入注释!
我建议尽可能使用 make 工具而不是 shell 工具。它通常会变得更干净。 (正如@ensc 所说,我确实如此——我在我的 makefile 中到处都使用 bash :-)。)
我想在 Makefile(GNU make + bash on Debian)中对二进制操作使用 bash
的算术扩展。 expr
未涵盖这些内容。我在 Makefile 中需要这个:
$ x=$(( 255 & 2#11110000)); echo $x
240
不起作用的东西:
$ cat Makefile
all: a b
a: # $ interpreted by make
x=$(( 255 & 2#11110000)); echo $$x
b: # escaped $
x=$$(( 255 & 2#11110000)); echo $$x
(a)
显然不行,(b)
也不行:
$ make b
x=$(( 255 & 2#11110000)); echo $x
/bin/sh: 1: arithmetic expression: expecting EOF: " 255 & 2#11110000"
有什么可行的办法吗?大量的引用、反引号和转义也没有产生任何结果。
添加
SHELL = bash
到 makefile
$ cat Makefile
# Make variable (recommended)
x := $(shell bash -c 'echo $$((255 & 2\#11110000))')
$(info [$x])
# Shell variable (not recommended)
a:
x=`bash -c 'echo $$((255 & 2#11110000))'`; echo $$x
给予
$ make
[240]
x=`bash -c 'echo $((255 & 2#11110000))'`; echo $x
240
不要忘记 #
在 makefile 中引入注释!
我建议尽可能使用 make 工具而不是 shell 工具。它通常会变得更干净。 (正如@ensc 所说,我确实如此——我在我的 makefile 中到处都使用 bash :-)。)