从动态加载的库(QPluginLoader)调用主程序函数
Calling main program functions from a dynamically loaded library (QPluginLoader)
我在运行时使用Qt QPluginLoader类动态加载插件(dll)。
到目前为止,我已经成功地加载了从主程序调用函数的插件。现在,插件需要调用主程序中的其他函数。我已经在插件项目中包含了相关的头文件,并且它的编译没有错误。当我试图从主程序调用以下插件函数时:
// main program calling a function in a dll that has been dynamically
// loaded into the program:
PluginInterface* plugin = qobject_cast<PluginInterface*>(QPluginLoader(path)).instance();
plugin->DoSomething(); // works, writes a message to the console
plugin->callMainProgramFunction(); // not working
// test method in the plugin project (dll) that writes to console:
void TestDLL::DoSomething();
{
std::cout << "Hello, this messages comes from TestDLL! Have a nice day"; // works
}
// test method in the plugin project (dll) that tries to
// call a method in the main program:
void TestDLL::callMainProgramFunction()
{
Angle test; // angle.h is included, and offers geometric functions
std::cout << test.sine() << "n"; // does not work, program stops
}
程序停止。我相信这是因为插件不知道在哪里找到符号(从angle.cpp链接并编译到主程序中的代码),因为它随后被动态链接到程序中。
许多插件将使用相同的函数,因此我认为编译每个插件的所有实现是一个坏主意。
是否有解决方案,使用QPluginLoader?我怎么能告诉动态加载的插件在哪里找到主程序中的符号?我知道QLibrary提供了一种更手动的方式来导出/导入函数和"解析符号",但缺乏QPluginLoader的简洁实例功能。
您可以创建一个包含插件和主程序之间共享的公共函数的dll,并将其链接到主程序和插件中。
QPluginLoader解决了我所描述的问题:通过QPluginLoader动态加载到主程序中的库将能够调用主程序中的函数。
我的问题的原因是一个微妙的错误在我使用的插件界面。
这是可能的,但不要忘记在Linux上将主程序与-rdynamic
标志链接起来,以便从插件中可以看到主程序的符号。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 输出 = 0 尝试计算标头中的函数并在主程序中调用它们时
- 如何在主程序流中不调用全局变量的情况下填充全局变量
- 从运行时加载的Dll中调用主程序的方法
- 从动态加载的库(QPluginLoader)调用主程序函数