【问题标题】:obj files empty with certain compiler flags带有某些编译器标志的 obj 文件为空
【发布时间】:2022-09-22 21:21:47
【问题描述】:

我正在尝试为我的 c 文件创建依赖文件。

[INFO] CMD: gcc -Wall -std=c11 -MM -MT ./stuff/cat.c -MF ./deps/cat.c.d -fPIC -o ./obj/stuff/cat.o -c ./stuff/cat.c
[INFO] CMD: gcc -Wall -std=c11 -MM -MT ./stuff/add.c -MF ./deps/add.c.d -fPIC -o ./obj/stuff/add.o -c ./stuff/add.c
[INFO] CMD: gcc -Wall -std=c11 -MM -MT ./stuff/rot13.c -MF ./deps/rot13.c.d -fPIC -o ./obj/stuff/rot13.o -c ./stuff/rot13.c
[INFO] CMD: ld -r -o ./obj/stuff.o ./obj/stuff/cat.o ./obj/stuff/add.o ./obj/stuff/rot13.o
[INFO] CMD: gcc -shared -o ./target/libstuff.so ./obj/stuff.o
[INFO] CMD: ar -rc ./target/libstuff.a ./obj/stuff.o
[INFO] CMD: gcc -Wall -std=c11 -o ./target/stuff ./obj/stuff.o ./tests/stuff.c
/usr/bin/ld: /tmp/ccF2LsaB.o: in function `test_add_2\':
stuff.c:(.text+0x2106): undefined reference to `add_2\'

objdump -d 给我:

christopher$ objdump -d ./obj/stuff.o    

./obj/stuff.o:     file format elf64-x86-64

在没有依赖项的情况下编译时,一切都按预期运行!

[INFO] CMD: gcc -Wall -std=c11 -fPIC -o ./obj/stuff/cat.o -c ./stuff/cat.c
[INFO] CMD: gcc -Wall -std=c11 -fPIC -o ./obj/stuff/add.o -c ./stuff/add.c
[INFO] CMD: gcc -Wall -std=c11 -fPIC -o ./obj/stuff/rot13.o -c ./stuff/rot13.c
[INFO] CMD: ld -r -o ./obj/stuff.o ./obj/stuff/cat.o ./obj/stuff/add.o ./obj/stuff/rot13.o
[INFO] CMD: gcc -shared -o ./target/libstuff.so ./obj/stuff.o
[INFO] CMD: ar -rc ./target/libstuff.a ./obj/stuff.o
[INFO] CMD: gcc -Wall -std=c11 -o ./target/stuff ./obj/stuff.o ./tests/stuff.c
[INFO] CMD: ./target/stuff
[INFO] DESCRIBE: ./tests/stuff.c => stuff
      [RUN!] It should... add 2 to input
      [..OK] Passed
      [RUN!] It should... add 2 to input with failure
      [FAIL] file: ./tests/stuff.c => line: 12

和 ofc objdump -d

christopher$ objdump -d ./obj/stuff.o
                                                           
./obj/stuff.o:     file format elf64-x86-64                                                                            
                                                           
                                                           
Disassembly of section .text:                   
                                                                                                                       
0000000000000000 <cat_file>:                             
   0:   f3 0f 1e fa             endbr64                
   4:   55                      push   %rbp           
   5:   48 89 e5                mov    %rsp,%rbp         
   8:   48 83 ec 20             sub    $0x20,%rsp
...

    标签: gcc


    【解决方案1】:

    我只是有同样的问题。最后我发现原因是-M-MM 会暗示-E。见the GNU doc

    而选项-E 除了预处理之外什么都不做。就是这样。

    如果要同时生成依赖输出文件和comilpe源文件,可以改用-MD-MMD。但它们有一些区别,详情请参阅doc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      相关资源
      最近更新 更多