Link one shared static library to my shared library

I'm struggling a bit with some options for linking to the project I'm currently working on:

I am trying to create a shared library linked to two other libraries. (Lets call them libfoo.soand libbar.so)
My output library should be a shared library, and I want a static link libfoo.soto the resulting library, but libbar.soshould be linked as a dynamic library. (It libbar.soshould be available on every computer where it is libfoo.sonot available, and I do not want the user to install / send it with my binaries.)

How can I archive this?

My current build instruction looks 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 defend myself: I'm not a c / C ++ specialist, so sorry if this question seems silly.

+3
source share
1 answer

There are two types of Linux C / C ++ libraries.

  • Static libraries ( *.a) are object code archives that are associated with part of the application and become part of the application. They are created using and can be controlled using a command ar(1)(i.e. it ar -t libfoo.awill display files in a library / archive).

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

    • Libraries of shared objects can be dynamically linked at runtime, but statically known. Libraries should be available during the compilation / link phase. Shared objects are not included in the executable, but are tied to execution.
    • / .

libfoo.so , , libfoo.a. , .

- :

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 
+10

Source: https://habr.com/ru/post/1746014/


All Articles