我正在编译c++代码,并且我试图添加-rdynamic选项,以便输出一个有意义的堆栈跟踪来调试我的c++程序,但是clang返回一个警告:“编译期间未使用的参数:'-rdynamic'”。
作为一种测试,在我的系统中,我尝试过编写一个简单的c++程序并用-rdynamic编译它,它没有问题,但是这个项目似乎没有问题。
任何建议都是见多识广的。
发布于 2014-12-10 14:54:06
您可能在编译源代码时使用-rdynamic标志,而不是链接它。它是链接器的标志,所以您只需要在链接时使用它。有些版本的clang可能不认识它,在这种情况下,您只需指示clang将正确的选项传递给链接器,通常如下:
-Wl,--export-dynamic因此,例如。
clang++ -rdynamic test.cpp或
clang++ --Wl,--export-dynamic test.cpp但是,如果您单独编译和链接,只在链接阶段使用它:
clang++ -c test.cpp
clang++ --Wl,--export-dynamic test.o (或者作为最后一步:clang++ -rdynamic test.o)
发布于 2020-08-30 07:59:48
nos's的答案是正确的,并帮助我很多。
一个小提示,--Wl,--export-dynamic应该是-Wl,--export-dynamic
还有一些确保-rdynamic工作正常的方法。
使用readelf -s查看ELF交响乐:
例如:
$ 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__您将在.dynsym中看到所有符号,而不仅仅是已使用的符号。
对于strip在-rdynamic标志中的影响有一些有趣的测试:
https://stackoverflow.com/questions/27403371
复制相似问题