c++ - How to go from linker error to line of code in the sources? -
the linker produces kind of output
/var/tmp/ccitb4j2.o: in function `main': /var/tmp/ccitb4j2.o(.text+0x4): undefined reference `myfunction(void)'
how can find out line of source code corresponding instruction @ .text+0x4 function invoked?
first, other answer question wrong: on linux do file , line number 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 `myfunction()' collect2: ld returned 1 exit status
above output gcc (ubuntu/linaro 4.6.3-1ubuntu5) 4.6.3
, linker gnu ld (gnu binutils ubuntu) 2.22
, has been true older versions of gcc , ld well.
the reason not getting file/line must that
- you didn't use
-g
flag, or - you have really old
ld
, or - you have configured
ld
without support debugging (i not sure possible).
however, if ld
refusing tell file , line, not lost. can compile source object, use objdump -rds foo.o
obtain 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
in above output, can see source line caused reference _z10myfunctionv
(which c++
mangled name myfunction(void)
) emitted in object file.
Comments
Post a Comment