Makefile shell 忽略 sed regexp 行尾

gnu*_*chi 4 shell make special-characters quoting

我有一个用于各种 C 代码测试的文件夹,其中包含许多名为*.c. 我的目标是让 Makefile 搜索以任何结尾的文件名.c,并将该文件名的根添加到目标中。

Makefile

TARGETS = $(shell ls *.c | sed "s/\.c$//g")
all: $(TARGETS)
Run Code Online (Sandbox Code Playgroud)

问题是 shell 命令在 sh 中按预期工作:

$ sh -c 'ls *.c | sed "s/\.c$//g"'
hello
Run Code Online (Sandbox Code Playgroud)

...虽然它失败了make

$ make
sed: 1: "s/\.c/g": unterminated substitute in regular expression
make: Nothing to be done for `all'.
Run Code Online (Sandbox Code Playgroud)

我尝试转义$as \$,而是产生:

`sed: 1: "s/\.c\/g": unterminated substitute pattern`
Run Code Online (Sandbox Code Playgroud)

"用单引号 ( ')替换双引号 ( ) 也是如此。

jor*_*anm 6

你需要逃离$. 在 中make,您可以使用$$. 你的线路应该是:

TARGETS = $(shell ls *.c | sed "s/\.c$$//g")
Run Code Online (Sandbox Code Playgroud)

虽然这个直接回答了问题,但@cas 的解决方案似乎更好。