热插拔 C++图书馆可以吗?
我正在寻找“热插拔”C++ 代码库。我有兴趣让这项技术在 Linux/Mac/Windows 之间跨平台工作。基本上我想要主程序#include“StateMachine.h”定义所有可调用接口。然后在运行时和执行期间加载和卸载 StateMachineLibrary.a 以使我的应用程序使用不同的状态机。
我的一个想法是,也许可以做一些事情,比如编写一个包装器,将编译后的代码加载到我自己的 malloc 内存中,并在该内存中创建函数指针?
动机是我的项目的状态机部分将经常更改并且需要重新编译,也将允许主应用程序在加载不同状态机的情况下继续运行。出于某些考虑,我希望使用“热插拔”库来代替 Lua 脚本之类的库,因此考虑将其作为替代方案。
I'm looking to "hot plug" a library of C++ code. I'm interested in having this technique work cross platform between Linux/Mac/Windows. Basically I want to have the main program #include "StateMachine.h" which defines all callable interfaces. Then at runtime and DURING EXECUTION load and unload StateMachineLibrary.a to have my application use different state machines.
One thought I have is maybe do something like write a wrapper that loads this compiled code in to my own malloc'd memory and creates function pointers in to that memory?
Motivation is that the State Machine portions of my project will be frequently changing and need recompilation, also would allow the main app to continue running with different State Machines being loaded. I'm hoping to use a "hot-pluggable" library INSTEAD OF something like Lua scripts due to some concerns, so considering that as an alternative has already been explored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
定义一个基本接口并从中派生您的实现。将它们放入动态库(DLL/SO)并在运行时加载它们。该库只需要一个静态工厂函数来向您提供其实现的实例。
注意:这只是一个草图。它有一些缺陷,例如我忽略了所有权语义,并且如果我们刚刚加载的 DLL 与我们二进制兼容,则不会进行实际检查。想一想,或者寻找现有的实现(其他回复中提到了一些)。
Define a base interface and derive your implementations from it. Put these into dynamic libraries (DLL/SO) and load them at runtime. The library will just need a static factory function to deliver an instance of its implementation to you.
Note: this is just a sketch. It has some flaws, for example I'm ignoring ownership semantics, and no actual checks are done if the DLL we just loaded is binary compatible with us. Think about it a bit, or look for existing implementations (some are mentioned in other responses).
这是可能的。对于跨平台工作(至少仅重新编译),您可能需要查看一些执行此操作的现有框架。
OpenSceneGraph 包括一个功能齐全的“热插拔”实现,用于加载和卸载插件。
Qt 也有一个插件框架。
“技巧”是为您的插件提供一个干净的界面,并且仅使用可以加载和卸载的动态库。几乎每个平台(所有主要平台)都支持动态加载和卸载库,因此没有什么可以阻止它的工作。
This is possible. For cross-platform work (at least recompile only), you might want to look at some existing frameworks that do this.
OpenSceneGraph includes a full featured, "hot-pluggable" implementation for loading and unloading plugins.
Qt has a plugin framework, as well.
The "trick" is having a clean interface for your plugins, and just using dynamic libraries that can be loaded and unloaded. Nearly every platform (all major ones) support dynamic loading and unloading of libraries, so there is nothing that prevents this from working.
是的——这当然有可能。在之前开发 3D 图形 API 和应用程序的过程中,我们让用户“即时”选择显示驱动程序。必须重新创建视图,但应用程序本身不需要关闭。
Yes - it's certainly possible. In a previous role where we developed an 3D graphics API and applications we let the user choose the display driver "on the fly". The view had to be recreated, but the application itself didn't need to shut down.
尽管它的许多部分都相当过时,高级 C++ 编程风格和惯用语(James Coplien)有一个关于如何做这些事情的部分,可能对阅读有用(尽管我不确定我是否这样做) 只是为此买一本)。
Though many parts of it are quite dated, Advanced C++ Programming Styles and Idioms (James Coplien) has a section on how to do such things that might be useful to read through (though I'm not sure I'd buy a copy just for this).
查看 Boost.Reflection 和 Boost.Extension - 它们旨在解决尝试此类操作时涉及的各种问题。我很确定它仍然不允许您跨编译器或版本工作,但它可能对您有帮助。
Check out Boost.Reflection and Boost.Extension - they were designed to address the various issues involved with attempting such things. I'm pretty sure it still doesn't let you work across compilers or versions, but it might be of aid to you.
我最初编写 v3c-dcom 只是为了看看我是否可以做到 - 你可以从 Sourceforge 下载它。
目前它基本上只是一个插件系统。
它依赖于其他三个 SourceForge 项目,因此您必须首先下载并安装它们。
转到 SourceForge http://sourceforge.net/ 并下载以下项目:
* v3c
* 树数据库
* 元树数据库
* v3c-dcom
v3c
包含构建系统和通用实用程序库。treedb
包含核心“持久内存”功能。meta-treedb
将treedb内联实现包装在一个毯子中,缩短编译时间和代码膨胀。v3c-dcom
包含一些示例,包括在程序内部创建插件存储库、向存储库添加库、调用CoCreateInstance()
创建对象以及调用方法在那些物体上。我设计的构建系统是用户友好的,即使它是基于 automake ;)
只需执行
make &&依次在每个项目的解压目录下sudo make install
。如果您偏执或没有“sudo”权限,请阅读 v3c 的自述文件和“试用”脚本,了解如何在您拥有的目录下解压/构建/安装软件包。
make check
将按其速度运行每个库,对于 v3c-dcom 它将运行我上面提到的演示。希望这有帮助。
I wrote v3c-dcom initially just to see if I could do it - you can download it from Sourceforge.
it's basically just a plug-in system at the moment.
It depends on three other SourceForge projects, so you will have to download and install them first.
Go to SourceForge http://sourceforge.net/ and download the following projects:
* v3c
* treedb
* meta-treedb
* v3c-dcom
v3c
contains a build system and a general utility library.treedb
contains the core "persistent memory" functionality.meta-treedb
wraps treedb inline implementations in a blanket, shortening compile times and code bloat.v3c-dcom
contains some examples, including creating a plug-in repository inside the program, adding a library to the repository, callingCoCreateInstance()
to create objects, and calling methods on those objects.I designed the build system to be user friendly, even though it's automake based ;)
Just do
make && sudo make install
in the unpacked directories of each project in turn.If you're paranoid or don't have "sudo" privileges read v3c's README and the "tryout" script on how to unpack/build/install the packages under a directory you own.
make check
will run each library through its paces and for v3c-dcom it will run the demo I mentioned above.Hope this helps.
并且不要忘记 XPCOM。它被设计为一个跨平台的 COM。
And don't forget XPCOM. It's designed to be a cross-platform COM.