Take a simple example:
void foo() {}
int main()
{
return 0;
}
I build it and look at the dynamic symbol table:
$ g++ test.cpp -o test
$ nm --dynamic test
0804849c R _IO_stdin_used
w __gmon_start__
U __libc_start_main
As expected, I don't see foo
. So I build to include unused symbols.
$ 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
And you can see that foo
is now in the symbol table. Now I build a static version.
$ gcc test.c -rdynamic -static -o test
$ nm --dynamic test
nm: test: No symbols
My symbols have gone, even though I explicitly specified them.
According to the GCC man page:
-static On systems that support dynamic linking, this prevents linking with the shared libraries.
My function foo()
is not a shared library.
If I expand this application, and it calls dlopen()
, and the library that I load needs to call foo()
, my symbol table no longer contains a reference to foo()
and my application will fail.
This happened to me recently.
Why does -static negate -rdynamic and how can I work around it?