Makefile 配方语法错误中的负 glob

Negative glob in Makefile recipe syntax error

如果我 运行 直接在 /bin/bash 中执行以下命令,它会完美执行:

cp test_!(*.o) ./tmp/

(我只想复制除对象之外的所有文件)

无论如何,如果我将此命令放入 Makefile 配方中,它会给出

bin/bash: -c: line 0: syntax error near unexpected token `('

生成文件示例

all:
        cp test_!(*.o) ./tmp/

这有什么问题吗?

你的世界:

test_!(*.o)

使用扩展的 globbing 语法。

看看这个:

$ shopt | grep extglob
extglob         on

然后在:

$ cat Makefile
SHELL := /bin/bash

all:
    shopt | grep extglob

$ make
shopt | grep extglob
extglob         off

在终端中调用,bash 处于交互模式(根据 bash -i),并且 shell 选择 extglobon.

make 调用,bash 未处于交互模式(根据 bash -c <command>), shell 选择 extgloboff.

您可以通过在 extglob 中启用您的食谱命令 .SHELLFLAGS 你的 makefile,例如

$ cat Makefile
SHELL := /bin/bash
.SHELLFLAGS := -O extglob -c

all:
    cp test_!(*.o) ./tmp/

$ make
cp test_!(*.o) ./tmp/
cp: cannot stat 'test_!(*.o)': No such file or directory
Makefile:5: recipe for target 'all' failed
make: *** [all] Error 1

如您所见,我的工作目录中没有匹配项,但没有语法错误。