gcc obj文件为空,带有某些编译器标志

xsuvu9jc  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(184)

我正在尝试为我的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
...
bbuxkriu

bbuxkriu1#

我也遇到了同样的问题。最后我发现原因是-M-MM会隐含-E。请参阅GNU文档。
而选项-E除了预处理之外什么也不做,情况就是这样。
如果你想同时生成依赖输出文件和编译源文件,你可以使用-MD-MMD代替,但它们有一些区别,详见文档。

相关问题