49

Say I have a.so and b.so. Can I produce c.so as a single shared library with all the functions exported by a and b, of course resolving all intra-dependencies (i.e. all functions of b.so called by a.so and the other way around)?

I tried

gcc -shared -Wl,soname,c.so -o c.so a.so b.so

but it doesn't work.

Same goes if I archive a.o and b.o in a.a and b.a (which shouldn't modify a.o and b.o), and do

gcc -shared -Wl,soname,c.so -o c.so a.a b.a

Thanks

Metiu
  • 1,677
  • 2
  • 16
  • 24
  • In the end I resorted to use a library script with a GROUP which allows me to show all the small libraries as a single one, but one software could just link to the right "sub"-library. – Metiu May 28 '09 at 20:07

2 Answers2

57

Merging multiple shared libraries into one is indeed practically impossible on all UNIXen, except AIX: the linker considers the .so a "final" product.

But merging archives into .so should not be a problem:

gcc -shared -o c.so -Wl,--whole-archive a.a b.a -Wl,--no-whole-archive
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thank you for the useful flag: whole-archive! – Denis Gorodetskiy Oct 14 '11 at 01:25
  • Does the order of the archives matter here? If I have a bunch of .a files, can I use *.a? – Raj Sep 12 '12 at 21:52
  • 1
    @Raj No, the order doesn't matter -- they all will be included in their entirety. If you don't have any symbol redefinitions in your `*.a`, then `*.a` will just work (TM). – Employed Russian Sep 13 '12 at 03:04
  • 1
    @EmployedRussian: I have tried the above command with g++, i,e g++ -shared -o c.so -Wl,--whole-archive a.a b.a -Wl,--no-whole-archive I got the following error: /usr/bin/ld: unrecognized option '-plugin' /usr/bin/ld: use the --help option for usage information collect2: error: ld returned 1 exit status What changes do I need to do? – Vinay Jul 11 '16 at 19:03
11

In practice it is not possible.

From linker point of view, a SO library is a final product that does not contain relocation information required for linking.

If you have access to either source or object files for both libraries, it is straightforward to compile/link a combined SO from them.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • 5
    "if you have access to either source or object files for both libraries, it is straightforward to compile/link a combined SO from them." --- How do I do this ? Specifically, on Android platform. – shaktiman_droid Aug 04 '14 at 20:22