使用g ++有没有办法在不修改源的情况下在指定的命名空间下加载动态链接库?(With g++ Is there way for a dynamically linked library to be l

编程入门 行业动态 更新时间:2024-10-10 14:22:21
使用g ++有没有办法在不修改源的情况下在指定的命名空间下加载动态链接库?(With g++ Is there way for a dynamically linked library to be loaded under a specified namespace without modifying source?)

使用g ++有一种链接库的方法,以便所有符号都在新的命名空间下吗?

例如,我想在我的程序中使用一个库,它与我现有的代码有冲突的符号。 有没有办法通过链接器将链接库封装在新的名称空间下,而无需修改源代码?

编辑

这里有一些更详细的信息:

我正在开发一个Qt应用程序,它作为嵌入式设备的“应用程序管理器”。 此应用程序链接Qt模块“webkitwidgets”,它是一个库。

由此应用程序管理器管理的应用程序构建为共享库,当这个管理器程序启动它们时,一个包装程序编程(Launcher)被分叉,启动程序使用QLibrary加载共享库:

bool ProcessInitImpl::loadLibrary(const QString& libraryPath) { qTrace() << libraryPath; QLibrary lib(libraryPath); lib.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint); if (lib.load()) { qDebug() << libraryPath << " is loaded successfully!"; typedef void (*RegistrationMethod)(); RegistrationMethod registrationMethodInTheSharedLibrary = (RegistrationMethod) lib.resolve("registerMetaTypes"); if (registrationMethodInTheSharedLibrary) { registrationMethodInTheSharedLibrary(); return true; } } qCritical() << libraryPath << " failed to load!" << lib.errorString(); return false; }

问题是该启动器被假设与管理器分离,使用IPC在启动器实例和管理器之间进行通信。 但是,启动器链接到管理器,而管理器又链接到webkitwidgets。 有一个应用程序在webkitwidgets中包含冲突的符号。

考虑到上述所有情况,我想到的唯一事情就是修改启动程序包装器,以便它不依赖于管理器,因此不会链接webkitwidgets。 我只是想看看是否有另一种方法来解决这个问题,然后再走这条路。

With g++ is there a way to link a library so that all symbols are under a new namespace?

For example, I'd like to use a library in my program which has conflicting symbols with my existing code. Is there a way to encapsulate the linked library under a new name space through the linker, without the need to modify source code?

Edit

Here is some more detail:

I am working on a Qt application, which functions as an "Application Manager" for an embedded device. This application links the Qt module "webkitwidgets", which is a library.

the applications managed by this application manager are built as shared libraries, and when they are launched by this manager program a wrapper programed (Launcher) is forked, the launcher uses QLibrary to load the shared library:

bool ProcessInitImpl::loadLibrary(const QString& libraryPath) { qTrace() << libraryPath; QLibrary lib(libraryPath); lib.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint); if (lib.load()) { qDebug() << libraryPath << " is loaded successfully!"; typedef void (*RegistrationMethod)(); RegistrationMethod registrationMethodInTheSharedLibrary = (RegistrationMethod) lib.resolve("registerMetaTypes"); if (registrationMethodInTheSharedLibrary) { registrationMethodInTheSharedLibrary(); return true; } } qCritical() << libraryPath << " failed to load!" << lib.errorString(); return false; }

the problem is that this launcher is suppose to be decoupled from the manager, using IPC to communicate between the launcher instances and the manager. However, the launcher is linked to the manager, which in turn is linked to webkitwidgets. There is an application which contains conflicting symbols within webkitwidgets.

Given all of that above, the only thing that came to mind for me was modifying the launcher wrapper so that it had no dependencies on the manager and thus didn't link webkitwidgets. I just wanted to see if there was another way to address this before going down that path.

最满意答案

使用g ++有一种链接库的方法,以便所有符号都在新的命名空间下吗?

可能不是(命名空间是C ++的东西,并且是通过名称修改来实现的),并且您没有指定动态链接库的确切加载对您意味着什么,并且您没有告诉您有关实际冲突的任何信息。 实际上,它非常复杂。 阅读Levine的链接器和加载器以及Drepper的如何编写共享库以获取详细信息,以及binutils ld , ld-linux(8)等文档。另请阅读dlmopen(3) (我从未使用过)。

例如,我想在我的程序中使用一个库,它与我现有的代码有冲突的符号。

请勿触摸磁带库或加载磁带库的方式,而是触摸您的程序。 它会更简单(你可以使用工具,或许可以编写你自己的GCC插件或使用GCC MELT )。 也许它可以像添加一些namespace一样简单,也可以在您自己的C ++源代码中使用。 因人而异。

实际上,如何处理问题在很大程度上取决于您观察到的实际冲突以及您拥有多少...

With g++ is there a way to link a library so that all symbols are under a new namespace?

Probably not (namespaces are a C++ thing and are implemented by name mangling), and you don't specify what exactly loading a dynamically linked library means to you and you don't tell anything about the actual conflicts you've got. In reality, it is quite complex. Read Levine's Linkers and Loaders and Drepper's How to Write Shared Libraries for details, and also the documentation of binutils ld, ld-linux(8) etc. Read also about dlmopen(3) (which I never used).

For example, I'd like to use a library in my program which has conflicting symbols with my existing code.

Don't touch the library, or the way you are loading it, but touch your program. It would be much simpler (and you could use tools for that, perhaps code your own GCC plugin or use GCC MELT for that). Perhaps it could be as simple as adding a few namespace and or using in your own C++ source code. YMMV.

Practically speaking, how to approach the problem depends a lot on the actual conflicts you are observing and how much you have ...

更多推荐

本文发布于:2023-08-02 13:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   情况下   动态链接库   加载   空间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!