Importing .dll into Qt

后端 未结 3 1052
情话喂你
情话喂你 2020-12-15 02:26

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

So I added this to my .pro file:

win32 {
LIBS += C:\\lib\\dependency.lib
LIBS += C:\\lib\\depe         


        
3条回答
  •  失恋的感觉
    2020-12-15 02:38

    This is possible if the DLL contains files with a "C" linkage (i.e. no C++ class decorations) and if you have a header file and a .def file for the DLL. If you do not have a .def file you can create one easily by downloading the dependency walker tool from http://www.dependencywalker.com/ to get the list of exported symbols; you can save the output of this tool as text, then extract the names. You then create a text file called mylibname.def that holds:

    LIBRARY mylibname
    
    EXPORTS
        FirstExportedFunctionName
        SecondExportedFunctionName
        ...
        LastExpertedFunctionName
    

    Then you run dlltool (in MinGW\bin):

    dlltool -d mylibname.def -l mylibname.a
    

    This will generate mylibname.a, which you add into your .pro file:

    win32:LIBS += mylibname.a
    

    You have to provide paths to all the files, or copy them to the right folders, of course.

    You must also modify the header file to your third party program so that all the symbols in the DLL that you need to link to are marked for import with Q_DECL_IMPORT. I do this by declaring all functions in the .h file as:

    extern "C" {
    MYLIBAPI(retType) FirstFunctionName(arg list...);
    MYLIBAPI(retType) SecondFunctionName(arg list...);
    ...
    MYLIBAPI(retType) LastFunctionName(arg list...);
    }
    

    where MYLIBAPI is

    #define MYLIBAPI(retType) Q_DECL_IMPORT retType
    

    We use the MYLIBAPI(retType) format as this allows us to adjust the header file for use in both import and when creating DLLs and it also allows us to work with a wide variety of different compilers and systems.

    Doing this, I managed to link QT in MinGW to a DLL that I generate using VS 2005. The routines in VS were exported as __stdcall. You should look at the dlltool documentation for adding underlines or other prefixes to the names in the library.

提交回复
热议问题