如何从链接器错误到源代码行? [英] How to go from linker error to line of code in the sources?

查看:110
本文介绍了如何从链接器错误到源代码行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接器产生这种输出

/var/tmp/ccITB4j2.o: In function `main':
/var/tmp/ccITB4j2.o(.text+0x4): undefined reference to `myFunction(void)'

我如何找到对应于.text + 0x4的指令所在的源代码行?

How can I find out the line of source code corresponding to the instruction at .text+0x4 where the function is actually invoked?

推荐答案

首先,您的问题的另一个答案是错误的:在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.

这篇关于如何从链接器错误到源代码行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆