简体   繁体   English

依赖于同一静态链接库的可执行文件和共享库

[英]An executable and a shared library dependent on a same statically linked library

Suppose you're developing a shared library libshared.so . 假设您正在开发一个共享库libshared.so

And you have a static library libstatic.a with some internal classes and functionality you need. 您将拥有一个静态库libstatic.a ,其中包含一些所需的内部类和功能。 You'd like to link it to your .so like this: 您想将其链接到您的.so如下所示:

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 另外,您还有一个executable.sh ,它将使用您的.so并在运行时动态打开它。

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). 您知道此可执行文件也已与libstatic.a静态链接(但是您不确定该库的版本与您的库完全相同)。

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 ? 当您知道executable.sh libstatic.a中已使用同一个库时,将libshared.solibstatic.a静态链接是否安全正确?

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. 因为共享库应该具有与位置无关的代码 (否则,动态链接程序必须进行过多的重定位,您会失去共享库的好处),但是静态库通常没有PIC。

Read Drepper's paper: How to write a shared library 阅读Drepper的论文:如何编写共享库

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM