且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从链接器错误到源代码行?

更新时间:2023-11-10 17:41:04

首先,您的问题的另一个答案是错误的:在Linux上,您可以从链接器获取文件和行号:

First, the other answer to your question is wrong: on Linux you do get file and line number from the linker:

$ cat foo.cc
extern int myFunction(void);

int main()
{
  return myFunction();
}
$ g++ -g foo.cc
/tmp/cc3twlhL.o: In function `main':
/tmp/foo.cc:5: undefined reference to `myFunction()'
collect2: ld returned 1 exit status

c $ c> gcc(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3 和链接器 GNU ld(GNU Binutils for Ubuntu)2.22 但是对于更旧版本的GCC和ld也是如此。

Above output is from gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 and linker GNU ld (GNU Binutils for Ubuntu) 2.22, but this has been true for much older versions of GCC and ld as well.

无法获取文件/行的原因必须是

The reason you are not getting the file/line must be that


  • 您未使用 -g 标志或

  • 您拥有真的 ld

  • 您已配置 ld 不支持调试(我不确定这是否可能)。

  • you didn't use -g flag, or
  • you have a really old ld, or
  • you have configured your ld without support for debugging (I am not sure this is even possible).

ld 拒绝告诉您文件和行,不是所有都丢失。您可以将源代码编译成对象,然后使用 objdump -rdS foo.o 获得相同的信息:

However, even if your ld is refusing to tell you the file and line, not all is lost. You can compile your source into object, then use objdump -rdS foo.o to obtain the same info:

g++ -g -c foo.cc
objdump -rdS foo.o

Disassembly of section .text:

0000000000000000 <main>:
extern int myFunction(void);

int main()
{
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
  return myFunction();
   4:   e8 00 00 00 00          callq  9 <main+0x9>
            5: R_X86_64_PC32    _Z10myFunctionv-0x4
}
   9:   5d                      pop    %rbp
   a:   c3                      retq

在上面的输出中,您可以清楚地看到哪条源代码行引用了 _Z10myFunctionv (这是 C ++ myFunction(void)的错误名称将在对象文件中发出。

In above output, you can clearly see which source line caused reference to _Z10myFunctionv (which is the C++ mangled name for myFunction(void)) to be emitted in the object file.