makefile 里的 sed 命令,里面的$*是 makefile 的一个自动变量吗 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
请不要在回答技术问题时复制粘贴 AI 生成的内容
amiwrong123

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

  •  
  •   amiwrong123 Oct 16, 2024 1468 views
    This topic created in 560 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
        1
    ho121  
       Oct 16, 2024
    amiwrong123
        2
    amiwrong123  
    OP
       Oct 16, 2024
    @ho121
    意思$*是$* 代表所有命令行参数呗?

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

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

    所以是 sed ,全权来处理$*吗
    Acatdef
        10
    Acatdef  
       Oct 17, 2024
    @amiwrong123 上面老哥提到了,$*是 makefile 的变量
    About     Help     Advertise     Blog     API     FAQ     Solana     5414 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 08:33 PVG 16:33 LAX 01:33 JFK 04:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86