1

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
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • Need more info, but everything will work, if GCC has dropped symbols it has done so legitimately, fear not. If you want to know why rather than just trusting, please add details. – Alec Teal Oct 09 '13 at 04:08
  • http://stackoverflow.com/questions/12601302/creating-shared-object-from-static-library-whose-object-files-were-linked-with – tristan Oct 09 '13 at 05:11

0 Answers0