举个简单的例子:
void foo() {}
int main()
{
return 0;
}
我构建它并查看动态符号表:
$ g++ test.cpp -o test
$ nm --dynamic test
0804849c R _IO_stdin_used
w __gmon_start__
U __libc_start_main
正如预期的那样,我没有看到foo
。所以我构建以包含未使用的符号。
$ gcc test.c -rdynamic -o test
$ nm --dynamic test
0804863c R _IO_stdin_used
w _Jv_RegisterClasses
0804a010 A __bss_start
0804a008 D __data_start
w __gmon_start__
080485e0 T __libc_csu_fini
08048570 T __libc_csu_init
U __libc_start_main
0804a010 A _edata
0804a018 A _end
0804861c T _fini
08048638 R _fp_hw
08048438 T _init
080484a0 T _start
0804a008 W data_start
08048554 T foo
08048559 T main
你可以看到它foo
现在在符号表中。现在我建立一个静态版本。
$ gcc test.c -rdynamic -static -o test
$ nm --dynamic test
nm: test: No symbols
我的符号已经消失了,即使我明确指定了它们。
根据 GCC 手册页:
-static 在支持动态链接的系统上,这会阻止与共享库的链接。
我的函数foo()
不是共享库。
如果我扩展这个应用程序,它调用dlopen()
了,我加载的库需要调用foo()
,我的符号表不再包含对的引用foo()
,我的应用程序将失败。
这件事最近发生在我身上。
为什么 -static 否定 -rdynamic 以及如何解决它?