makefile 里的 sed 命令,里面的$*是 makefile 的一个自动变量吗

5 天前
 amiwrong123

makefile 正在学习中,看到一个规则里面用了 sed 命令,sed 命令里用了$*,感觉只用正则表达式来解释$解释为行尾解释不通。

ldd@CD:~/hello$ gcc -M hello.c
hello.o: hello.c /usr/include/stdc-predef.h /usr/include/stdio.h \
 /usr/include/x86_64-linux-gnu/bits/libc-header-start.h \
 /usr/include/features.h /usr/include/x86_64-linux-gnu/sys/cdefs.h \
 /usr/include/x86_64-linux-gnu/bits/wordsize.h \
 /usr/include/x86_64-linux-gnu/bits/long-double.h \
 /usr/include/x86_64-linux-gnu/gnu/stubs.h \
 /usr/include/x86_64-linux-gnu/gnu/stubs-64.h \
 /usr/lib/gcc/x86_64-linux-gnu/9/include/stddef.h \
 /usr/lib/gcc/x86_64-linux-gnu/9/include/stdarg.h \
 /usr/include/x86_64-linux-gnu/bits/types.h \
 /usr/include/x86_64-linux-gnu/bits/timesize.h \
 /usr/include/x86_64-linux-gnu/bits/typesizes.h \
 /usr/include/x86_64-linux-gnu/bits/time64.h \
 /usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h \
 /usr/include/x86_64-linux-gnu/bits/types/__mbstate_t.h \
 /usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h \
 /usr/include/x86_64-linux-gnu/bits/types/__FILE.h \
 /usr/include/x86_64-linux-gnu/bits/types/FILE.h \
 /usr/include/x86_64-linux-gnu/bits/types/struct_FILE.h \
 /usr/include/x86_64-linux-gnu/bits/stdio_lim.h \
 /usr/include/x86_64-linux-gnu/bits/sys_errlist.h

另外随便写了一个 hellowoald ,看了 gcc -M 的输出如上。

另外,为啥一定要用这个 d 文件,感觉有点没理解它的妙处?

PS:脚本方面比较弱,正在学习中,大佬们帮忙解答下。

653 次点击
所在节点    程序员
10 条回复
ho121
5 天前
amiwrong123
5 天前
@ho121
意思$*是$* 代表所有命令行参数呗?

但是放在这里是啥意思啊,是给 sed 的命令行参数吗?还是我给 make 的命令行参数啊?
ho121
5 天前
@amiwrong123 不妨贴一下那条 sed 命令
amiwrong123
5 天前
@ho121
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
amiwrong123
5 天前
@ho121
规则下面的命令的缩进没了
alexsz
5 天前
问过 AI 了吗?
$* 实际上是 Makefile 中的一个自动变量( automatic variable ),代表目标文件的名称,但不包括文件扩展名。
amiwrong123
5 天前
@alexsz
有一个 AI 确实这么回答我了。但我不太确定。

因为是在单引号里面的,还可以解释为 Makefile 的自动变量吗?会不会应该解释为 bash 那边的用法
Acatdef
4 天前
shell 不会解析单引号里的变量
@amiwrong123
amiwrong123
4 天前
@Acatdef
OK 。所以这个$*,肯定 bash 不会做什么特殊的事情。然后感觉 makefile 也不会做什么特殊事情,因为在单引号里面。

所以是 sed ,全权来处理$*吗
Acatdef
3 天前
@amiwrong123 上面老哥提到了,$*是 makefile 的变量

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1080696

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX