加载共享库的两个实例 [英] Loading two instances of a shared library

查看:153
本文介绍了加载共享库的两个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关测试我想从一个应用程序加载共享库的两个实例。库中的code提供了一个API,但它不会让我初始化库的两个(或更多)实例,因为某些功能依赖于静态变量。

For a test I'd like to load two instances of a shared library from an application. The code in the library provides an API but it does not allow me to initialize two (or more) instances of the library because some of the functions rely on static variables..

我目前正在写单元测试此lib和我想有两个实例,因为这将简化我的​​测试了很多。

I'm currently writing unit-tests for this lib, and I'd like to have two instances because that would simplify my tests a lot.

本库没有得到链接到程序。相反,我直接用调用LoadLibrary / GetProcAddress的(或的dlopen / dlsym进行在Linux上)加载它。为了区分这两个库,我可以简单地使用不同的名称为函数指针我加载...

The library doesn't get linked into the program. Instead I load it directly using LoadLibrary/GetProcAddress (or dlopen/dlsym on linux). To distinguish the two libraries I could simply use different names for the function-pointers I'm loading...

下面是问题:


  • 是否有可能加载一个库两次吗?例如。图书馆的所有载入的实例应该得到自己的数据段,并不会相互影响。

  • Is it possible to load such a library twice? E.g. All loaded instances of the library should get their own data-segment and don't influence each other.

如果这样:这是便携式Windows和Linux?

If so: Is this portable for windows and linux?

推荐答案

您可以加载一个库两次,从理论上讲,如果它编译成与位置无关的code( -fPIC )。

You can load a library twice, in theory, if it's compiled as position-independent code (-fPIC).

在一些Unix一样,可以再的dlopen 图书馆两次,如果您的装载机有一个 RTLD_PRIVATE 标记,或者通过具有相同的符号(把它放在两个不同的路径,否则将只返回的第一个文件句柄)库的两个不同的副本,并与 RTLD_LOCAL 。

On some Unices, you can then dlopen the library twice if your loader has an RTLD_PRIVATE flag, or by having two "different" copies of the library with the same symbols (put it at two different paths, otherwise it will just return the first file handle), and opening them each with RTLD_LOCAL.

我不知道有关Windows的共享库什么。它甚至可能不是可能的。

I don't know anything about Windows shared libraries. It may not even be possible.

这篇关于加载共享库的两个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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