关于bash:使用shell变量传递选项时,autotools配置错误

autotools configure error when passing options using shell variable

我希望从bash脚本中调用configure命令(以编译nginx),如下所示:

1
2
CONF_OPTS=' --with-cc-opt="-O2 -g"'
./configure ${CONF_OPTS}

但出现以下错误:

1
./configure: error: invalid option"-g"

当我通过以下选项时:

1
./configure --with-cc-opt="-O2 -g"

我没有错误。

要重现:

1
2
3
4
5
6
curl -O  http://nginx.org/download/nginx-1.14.2.tar.gz
tar xfz nginx-1.14.2.tar.gz
cd nginx-1.14.2

OPTS='--with-cc-opt="-O2 -g"'
./configure ${OPTS}

结果

1
./configure: error: invalid option"-g""

但是:

1
./configure --with-cc-opt="-O2 -g"

没关系

我认为这与nginx无关,但我认为这是bash引用替换问题。


它将像这样工作:

1
2
$ CC_OPTS=--with-cc-opt='-O2 -g'
$ ./configure"$CC_OPTS"

,以便将$CC_OPTS的扩展作为单个参数传递给./configure

但是如果您还想通过,也许:

1
--with-ld-opt='-Wl,-gc-sections -Wl,-Map=mapfile'

通过变量,您需要:

1
2
3
$ CC_OPTS=--with-cc-opt='-O2 -g'
$ LD_OPTS=--with-ld-opt='-Wl,-gc-sections -Wl,-Map=mapfile'
$ ./configure"$CC_OPTS""$LD_OPTS"

因为您需要将两个参数传递给./configure和:

1
./configure"$CC_OPTS $LD_OPTS"

仅通过一个,将失败。