简体   繁体   中英

Clang++ 3.5.0 -rdynamic

I'm compiling c++ code and I'm trying to add in the -rdynamic option so I can print out a meaningful stack trace for debugging my c++ program, but clang throws back a warning saying "argument unused during compilation: '-rdynamic'".

As a test, on my system I've tried writing a simple c++ program and compiling it with -rdynamic and it worked no problem, but with this project it doesn't seem to go.

Any advice is much appricated

You are likely using the -rdynamic flag when you are just compiling the source code, not linking it. It's a flag for the linker, so you only need it when linking. Some versions of clang might not recognize it, in which case you can just instruct clang to pass the proper option to the linker, which commonly is:

 -Wl,--export-dynamic

So, eg

clang++ -rdynamic test.cpp

or

clang++ --Wl,--export-dynamic test.cpp

But if you are compiling and linking separately, only use it at the linking stage:

clang++ -c test.cpp
clang++ --Wl,--export-dynamic test.o 

(or as the last step: clang++ -rdynamic test.o )

The nos's answer is right ,and help me a lot.
One little tip, --Wl,--export-dynamic should be -Wl,--export-dynamic

And there ars some ways to make sure -rdynamic worked.
Use readelf -s to see the ELF symbos:
eg

$ cat t.c

#include <stdio.h>
void bar() {}
void baz() {}
void foo() {}
int main() { foo(); printf("test"); return 0; }
$ clang  -O0  -o  test  t.c
$ readelf -s test >test.elf

Symbol table '.dynsym' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.17 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND abort@GLIBC_2.17 (2)
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.17 (2)
$ clang -rdynamic -O0  -o  test1  t.c
$ readelf -s test1 >test1.elf

Symbol table '.dynsym' contains 24 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTab
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.17 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     4: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND abort@GLIBC_2.17 (2)
     5: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable
     6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.17 (2)
     7: 0000000000420038     0 NOTYPE  GLOBAL DEFAULT   25 _bss_end__
     8: 00000000004009a0    68 FUNC    GLOBAL DEFAULT   14 main
     9: 0000000000420030     0 NOTYPE  GLOBAL DEFAULT   25 __bss_start__
    10: 0000000000420030     0 NOTYPE  GLOBAL DEFAULT   25 __bss_start
    11: 0000000000400994     4 FUNC    GLOBAL DEFAULT   14 bar
    12: 0000000000400a7c     4 OBJECT  GLOBAL DEFAULT   16 _IO_stdin_used
    13: 0000000000420038     0 NOTYPE  GLOBAL DEFAULT   25 _end
    14: 0000000000420038     0 NOTYPE  GLOBAL DEFAULT   25 __end__
    15: 0000000000420020     0 NOTYPE  GLOBAL DEFAULT   24 __data_start
    16: 0000000000420030     0 NOTYPE  GLOBAL DEFAULT   24 _edata
    17: 0000000000400a68     4 FUNC    GLOBAL DEFAULT   14 __libc_csu_fini
    18: 000000000040099c     4 FUNC    GLOBAL DEFAULT   14 foo
    19: 00000000004009e8   128 FUNC    GLOBAL DEFAULT   14 __libc_csu_init
    20: 00000000004008a0     0 FUNC    GLOBAL DEFAULT   14 _start
    21: 0000000000420020     0 NOTYPE  WEAK   DEFAULT   24 data_start
    22: 0000000000400998     4 FUNC    GLOBAL DEFAULT   14 baz
    23: 0000000000420038     0 NOTYPE  GLOBAL DEFAULT   25 __bss_end__

You will see all symbols in .dynsym ,not only used ones.
And there are some interesting test about strip influence in -rdynamic flag in:
gcc debug symbols (-g flag) vs linker's -rdynamic option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM