Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c++ - Importing .dll into Qt

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

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();

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

EDIT:

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. 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
    
  2. Dll project has DEFINES += MYDLL_LIB inside it's pro file.

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

    class MYDLL_EXPORT MyClass {
    
    // ...
    
    };
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...