Search code examples
shared-librariesstatic-librariesstatic-linkingdynamic-linking

How generate static library (.a) from shared for dynamic linking


I have a shared library librun.so without source code, but have an SDK to work with it.

How generate static library (librun.a) with only export functions from librun.so for dynamic linking my library libapp.so with librun.so?

On windows it's done like this, but on Linux how?

  1. dumpbin /exports run.dll
  2. Make run.def with export functions
  3. lib /def:run.def /out:run.lib /machine:x86

Solution

  • On Linux you normally don't need explicit static library and just link against shared library directly:

    $ gcc ... -o libapp.so -Lpath/to/librun.so -lrun
    

    If you really need an static library, you can generate it via Implib.so:

    # Generate wrappers
    $ implib-gen.py librun.so
    Generating librun.so.tramp.S...
    Generating librun.so.init.c...
    # Create static library
    $ gcc -c -fPIC librun.so.tramp.S librun.so.init.c
    $ ar rcs librun.a librun.so.*.o
    

    Note that resulting static library librun.a will be a wrapper which internally loads original librun.so and forwards calls to it (this is same as Windows).