无法引用另一个插件中的方法

Unable to refer to methods in another plugin

我在 classA 中有一个方法存在于 PluginA 中,我能够在同一插件内的所有 类 中编译和访问该方法。 当我尝试从另一个 pluginB 访问该方法时,出现以下错误。尽管我可以从 pluginB.

引用和打印 pluginA 中的枚举
\plugins\pluginB\mocks\classB.cpp:61: error: undefined reference to namespaceA::classA::methodA(int)

collect2.exe:-1: error: error: ld returned 1 exit status

非常感谢任何指导。

如果插件是独立的,则不能直接跨插件调用函数。 在这种情况下,如果您确实需要跨插件调用函数,则需要使用 GetProcAddress 来检索特定函数的地址。但是,这仅适用于使用 extern "C":

声明的自由函数
// Somewher in pluginA
extern "C" void functionA() {}

// Somewhere in pluginB
using MyFunc = void(void);
MyFunc *pointer = GetProcAddress(module,TEXT("functionA"));
if (pointer)
    pointer(); // call "functionA()";
else
    qWarning("functionA() not found, pluginA not loaded");

请注意,您可能希望使用 EnumProcessModulesEx() 搜索所有可能加载的 module

如果 pluginB 在编译时链接到 pluginA,这意味着您应该在 pluginB 的 .pro 文件中包含 LIBS += -lpluginA。 还要确保在 classA 声明中使用 __declspec( dllexport )__declspec( dllimport )

如果您使用 Qt Creator 向导生成您的 pluginA 项目,您的代码中应该已经有这样的东西:

#ifdef _MSC_VER

    #if defined(LIBRARY_A)
        #define LIBRARY_A_EXPORT __declspec(dllexport)
    #else
        #define LIBRARY_A_EXPORT __declspec(dllimport)
    #endif

#else

    #define LIBRARY_A_EXPORT

#endif

只需确保 classA 定义如下所示:class LIBRARY_A_EXPORT classA;