如何将静态库嵌入共享库? [英] How to embed a static library into a shared library?

查看:83
本文介绍了如何将静态库嵌入共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在linux上,我试图创建一个共享库libbar.so,该库嵌入了一个商业静态库(许可就可以了).商业库具有4个版本:libfoo-seq.a,libfoo-mt.a,libfoo-seq.so和libfoo-mt.so(它们都提供相同的符号,只是代码是顺序/多线程的,并且该库是静态/共享的).在这四个文件中,我希望我的代码始终使用顺序的foo库,因此在创建libbar.so时,我将目标文件和libfoo-seq.a链接在一起.

On linux I am trying to create a shared library, libbar.so, that embeds a commercial static library (licensing is fine). The commercial library has 4 versions: libfoo-seq.a, libfoo-mt.a, libfoo-seq.so, and libfoo-mt.so (they all provide the same symbols, just the code is sequential/multi-threaded, and the lib is static/shared). Of these four I want my code always to use the sequential foo library, so when I create libbar.so I link together my object files and libfoo-seq.a.

问题是我的库用户可能在他们拉进我的libbar.so时就已经拉进了libfoo-mt.so,因此,在读入libbar.so时,libfoo中的所有符号都已经存在,因此我对foo中的函数的调用被解析为多线程版本.

The problem is that the users of my library may have already pulled in libfoo-mt.so by the time they pull in my libbar.so, thus all symbols from libfoo are already present by the time libbar.so is read in, so my calls to the functions in foo are resolved to the multithreaded version.

我想知道如何解决此问题?在编译以创建目标文件以及将目标文件与libfoo-seq.a链接以创建libbar.so时,我需要使用哪种魔术标记?

I wonder how can I resolve this issue? What kind of magic flags do I need to use when I compile to create my object files and when I link my object files with libfoo-seq.a to create libbar.so?

推荐答案

您可以通过版本脚本在libbar中隐藏libfoo的符号:

You can hide libfoo's symbols in libbar via version script:

$ cat libbar.map
{
  global: libbar_*;
  local: libfoo_*;
};
$ gcc ... -o libbar.so -Wl,--version-script=libbar.map

这篇关于如何将静态库嵌入共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆