If I do
>ar rc foobar.a foobar.o
>g++ -shared -fPIC foobar.a -o foobar.so
the resulting foobar.so
is much smaller and has missing symbols compared to
>g++ -shared -fPIC foobar.o -o foobar.so
where foobar.so
has all the symbols from foobar.o
Why?
SSCCE as requested by Alec Teal:
> cat foobar.cpp
#include "foobar.h"
Foobar::Foobar() {}
> cat foobar.h
class Foobar {
public:
Foobar();
};
> g++ -fPIC -c foobar.cpp -o foobar.o
> ar rc foobar.a foobar.o
> nm foobar.a | grep Foobar
0000000000000000 T _ZN6FoobarC1Ev
0000000000000000 T _ZN6FoobarC2Ev
> g++ -fPIC -shared foobar.a -o foobar.so
> nm foobar.so | grep Foobar
> g++ -fPIC -shared foobar.o -o foobar.so
> nm foobar.so | grep Foobar
0000000000000680 T _ZN6FoobarC1Ev
0000000000000680 T _ZN6FoobarC2Ev
tristan's answer points to the usage of --whole-archive
option, does not work for me:
> g++ -fPIC -shared -Wl,--whole-archive foobar.a -o foobar.so
/usr/lib64/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0xd): undefined reference to `__init_array_end'
/usr/bin/ld: /usr/lib64/libc_nonshared.a(elf-init.oS): relocation R_X86_64_PC32 against undefined hidden symbol `__init_array_end' can not be used when making a shared object
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status