加载共享库两次 [英] Loading shared library twice

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

问题描述

我试图在 C 中两次加载共享库:

I'm trying to load a shared library in C twice:

lib1 = dlopen("mylib.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
lib2 = dlopen("mylib.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);

我想要的是 lib1 和 lib2 具有单独的地址空间,以便它们可以做不同的事情.目前,我可以实现这一点的唯一方法是复制 mylib 使代码如下所示:

What I want is that lib1 and lib2 have separate address spaces so that they can do different things. Currently, the only way I can achieve this is by copying mylib so that the code looks like this:

lib1 = dlopen("mylib.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
lib2 = dlopen("mylib2.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);

在有限的范围内,这对我来说很好用.但是,我有一个应用程序使用库的次数一般,这使得复制库变得很麻烦.

In a limited scope this works fine for me. However, I have an application which uses the library a generic number of times which makes copying the library cumbersome.

有没有更好的方法来为每次加载库时拥有一个单独的地址空间?

Is there a better way to have a separate address space for each time the library is loaded?

我想多次加载库,因为我的应用程序正在处理一种消息队列.消息队列中的项引用共享库的名称(例如 mylib),并包含一组应由库处理的数据.我想在多线程环境中处理 MQ,在自己的线程中运行对库方法的每次调用.只要 MQ 只包含一次对库的调用,一切都会按预期进行.然而,当我有两个项目使用同一个库时,事情开始变得奇怪.

I want to load the library multiple times as my application is processing a kind of message queue. The items in the message queue refer to the name of a shared library (e.g. mylib) and contain a set of data that shall be processed by the library. I want to process the MQ in a multithreading environment, running each call to the library's method in its own thread. As long as the MQ contains the call to a library only once, everything is working as expected. However, when I have two items that use the same library, things start to get weird.

推荐答案

您需要使用 dlmopen 来实现这种隔离:

You need to use dlmopen to achieve this sort of isolation:

// No need for RTLD_LOCAL, not sure about RTLD_DEEPBIND
lib1 = dlmopen (LM_ID_NEWLM, "mylib.so", RTLD_LAZY | RTLD_DEEPBIND);

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

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