将 .dll 导入 Qt

2021-12-25 00:00:00 dll qt c++

我想将 .dll 依赖项引入我的 Qt 项目.

I want to bring a .dll dependency into my Qt project.

所以我将它添加到我的 .pro 文件中:

So I added this to my .pro file:

win32 {
LIBS += C:libdependency.lib
LIBS += C:libdependency.dll
}

然后(我不知道这是否是正确的语法)

And then (I don't know if this is the right syntax or not)

#include <windows.h>
Q_DECL_IMPORT int WINAPI DoSomething();

顺便说一句,.dll 看起来像这样:

btw the .dll looks something like this:

#include <windows.h>
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, 
                                        LPVOID lpReserved)
{
    return TRUE;
}

extern "C"
{
int WINAPI DoSomething() { return -1; }
};

出现错误:未解析的符号?

Getting error: unresolved symbol?

注意:在 .NET 的 ez pz 程序集架构之外,我对 .dll 没有经验,绝对是 n00b.

Note: I'm not experienced with .dll's outside of .NET's ez pz assembly architechture, definitely a n00b.

推荐答案

您的LIBS +="语法错误.试试这个:

Your "LIBS +=" syntax is wrong. Try this:

win32 {
    LIBS += -LC:/lib/ -ldependency
}

我也不确定在 .pro 文件中使用带驱动器号的绝对路径是否是个好主意 - 我通常将依赖项保留在项目树中的某处并使用相对路径.

I'm also not sure if having absolute paths with drive letter in your .pro file is a good idea - I usually keep the dependencies somewhere in the project tree and use relative path.

我想你的 dll 有问题,即符号没有正确导出.我总是使用 QtCreator 提供的模板:

I suppose that something is wrong in your dll, i.e. the symbols are not exported correctly. I always use template provided by QtCreator:

  1. 在 dll 项目中有 mydll_global.h 头文件,代码如下:

  1. Inside dll project there is mydll_global.h header with code like that:

#ifdef MYDLL_LIB
    #define MYDLL_EXPORT Q_DECL_EXPORT
#else
    #define MYDLL_EXPORT Q_DECL_IMPORT
#endif

  • Dll 项目的 pro 文件中有 DEFINES += MYDLL_LIB.

  • Dll project has DEFINES += MYDLL_LIB inside it's pro file.

    导出的类(或仅选定的方法)和自由函数在头文件中用 MYDLL_EXPORT 标记,即

    Exported class (or only selected methods) and free functions are marked with MYDLL_EXPORT inside header files, i.e.

    class MYDLL_EXPORT MyClass {
    
    // ...
    
    };
    

  • 相关文章