views:

151

answers:

1

I am struggeling a little bit with some options for linking on a project I am currently working on:

I am trying to create a shared library which is linked against 2 other libraries. (Lets call them libfoo.so and libbar.so)
My output library has to be a shared library and I want to static link libfoo.so to the resulting library, but libbar.so should linked as a dynamic library. (libbar.so should be available on every machine, where libfoo.so is not available and I do not want the user install it / ship it with my binaries.)

How could I archive this?

My current build instruction look like this:

c++ -Wall -shared -c -o src/lib.o src/lib.cpp
c++ -Wall -shared -o lib.ndll src/lib.o -lfoo -lbar

I my defense: I am not a c/c++ expert, so sorry if this question seems to be stupid.

+5  A: 

There are two Linux C/C++ library types.

  • Static libraries (*.a) are archives of object code which are linked with and becomes part of the application. They are created with and can be manipulated using the ar(1) command (i.e. ar -t libfoo.a will list the files in the library/archive).

  • Dynamically linked shared object libraries (*.so) can be used in two ways.

    1. The shared object libraries can be dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the binary executable but are tied to the execution.
    2. The shared object libraries can be dynamically loaded/unloaded and linked during execution using the dynamic linking loader system functions.

In order to statically link libfoo.so into your binary, you will need a corresponding static library which is typically called libfoo.a. You can use a static library by invoking it as part of the compilation and linking process when creating a program executable.

The result would be changing your build commands to something like the following:

g++ -Wall -fPIC -c -o src/lib.o src/lib.cpp
g++ -shared -Wl,-soname,mylib.so.1 -o mylib.so.1 src/lib.o -L/path/to/library-directory -lbar libfoo.a 
jschmier