• 请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123
V2EX  ›  程序员

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

  •  
  •   amiwrong123 · Oct 16, 2024 · 1957 views
    This topic created in 756 days ago, the information mentioned may be changed or developed.

    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:脚本方面比较弱,正在学习中,大佬们帮忙解答下。

    10 replies  •  2024-10-17 18:29:46 +08:00
    ho121
    ho121
    Oct 16, 2024
    1
    amiwrong123
    amiwrong123
    OP
    Oct 16, 2024
    2
    @ho121
    意思$*是$* 代表所有命令行参数呗?

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

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

    所以是 sed ,全权来处理$*吗
    Acatdef
    Acatdef
    Oct 17, 2024
    10
    @amiwrong123 上面老哥提到了,$*是 makefile 的变量
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2049 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:58 · PVG 22:58 · LAX 06:58 · JFK 09:58
    ♥ Do have faith in what you're doing.