【发布时间】:2015-08-07 02:05:05
【问题描述】:
我正在使用 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 的简洁实例功能。
【问题讨论】: