Can I build a shared library by linking static libraries?

13,829

Solution 1

You can (just extract all the .o files and link them with -shared to make a .so), but whether it works, and how well it works, depends on the platform and whether the static library was compiled as position-independent code (PIC). On some platforms (e.g. x86_64), non-PIC code is not valid in shared libraries and will not work (actually I think the linker will refuse to make the .so). On other platforms, non-PIC code will work in shared libraries, but the in-memory copy of the library is not sharable between different programs using it or even different instances of the same program, so it will result in HUGE memory bloat.

Solution 2

I can't see why you couldn't just build the files of your dynamic library to .o files and link with;

gcc -shared *.o -lstaticlib1 -lstaticlib2 -o mylib.so
Share:
13,829
flyingbin
Author by

flyingbin

Updated on June 05, 2022

Comments

  • flyingbin
    flyingbin almost 2 years

    I have a bunch of static libraries (*.a), and I want to build a shared library (*.so) to link against those static libraries (*.a). How can I do so in gcc/g++?

  • Lol4t0
    Lol4t0 over 12 years
    The thing is that you can omit *.o, if you have no them
  • bdonlan
    bdonlan over 12 years
    Static libraries will only pull in their code if it's actually referenced, however, so you can't just omit all .o
  • bdonlan
    bdonlan over 12 years
    Also note that if multiple instances of the same static library are present in the same executable (loaded indirectly via shared libs containing the static libs), strange things can happen
  • bdonlan
    bdonlan over 12 years
    @nos, PIC isn't the same as relocatable. All static libs are relocatable, but may or may not be PIC. Note that on x86 Linux (but not x86-64) non-PIC shared libs do work, they just require relocations to be done at library load time, which is slow, increases memory usage, and has certain security concerns (as it requires that code pages be marked r/w while the relocation is in progress)
  • dashesy
    dashesy about 11 years
    Thanks for this very useful answer! I guess this is the best explanation for this error when doing this on x86_64: 'relocation R_X86_64_32S against `g_gGlobalVariable' can not be used when making a shared object; recompile with -fPIC'
  • Steven W. Klassen
    Steven W. Klassen over 5 years
    I have to second the comment that this doesn't work on x86_64 code on Linux. On macOS, however, you can simply include the static libraries when building the dynamic one. But I have not yet managed to accomplish it on Linux.
  • Charlie Reitzel
    Charlie Reitzel over 2 years
    I've done this in the distant past on Win32, as well. Iirr, you had to override a lot of the defaults in the IDE, but it was doable and it did work. The question comes up when building plugins. Sometimes you want to avoid collisions in shared libraries and don't have much control over the runtime deployment.