V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
amiwrong123
V2EX  ›  程序员

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

  •  
  •   amiwrong123 · 5 天前 · 654 次点击

    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 条回复    2024-10-17 18:29:46 +08:00
    ho121
        1
    ho121  
       5 天前
    amiwrong123
        2
    amiwrong123  
    OP
       5 天前
    @ho121
    意思$*是$* 代表所有命令行参数呗?

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

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

    所以是 sed ,全权来处理$*吗
    Acatdef
        10
    Acatdef  
       3 天前
    @amiwrong123 上面老哥提到了,$*是 makefile 的变量
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5452 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:35 · PVG 15:35 · LAX 00:35 · JFK 03:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.