问题
Suppose you're developing a shared library libshared.so
.
And you have a static library libstatic.a
with some internal classes and functionality you need. You'd like to link it to your .so
like this:
g++ -o libshared.so -shared myObj.o -lstatic
Also you have an executable.sh
which will use your .so
and dynamically open it in the runtime
dlopen("libshared.so", RTLD_NOW)
You know this executable was as well statically linked against libstatic.a
(but you're not sure the version of the library is exactly the same as yours).
So the question is:
Is it safe and correct to statically link your libshared.so
against libstatic.a
when you know the same library is already used in executable.sh
?
回答1:
You should avoid linking a static library into a shared one.
Because a shared library should have position independent code (otherwise, the dynamic linker has to do too much relocation, and you lose the benefits of shared libraries), but a static library usually does not have PIC.
Read Drepper's paper: How to write a shared library
You build your library with
g++ -Wall -O -fPIC mySrc.cc -c -o myObj.pic.o
g++ -o libshared.so -shared myObj.pic.o -lotherlib
来源:https://stackoverflow.com/questions/23133387/an-executable-and-a-shared-library-dependent-on-a-same-statically-linked-library