How do I export and link to a dll using Qt Creator

2019-08-14 14:14发布

I have got a few Qt projects building on my Mac and I am currently trying to get the same code to build on Windows. In among these projects there is one dll (call it LibraryA) that exports a class (call it ClassA) that is linked to by a second dll (call it LibraryB).

I have the following code that defines the dll export code to be used based on the compiler being used.

#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

I then incorporate this into my class definitions.

class LIBRARY_A_EXPORT ClassA

However, on Windows I get unresolved symbol __declspec(dllimport) in the LibraryB.

What have I done wrong?

EDIT I created a couple of standalone projects to try and get this working (called LibraryA and LibraryB again). LibraryA successfully builds and in the LibraryB project I used the "Add Library" dialog, selecting External Library. This gives me the following .pro file entries which is still not working

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../build-LibraryA-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug/release/ -lLibraryA
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../build-LibraryA-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug/debug/ -lLibraryA

INCLUDEPATH += $$PWD/../build-LibraryA-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug/debug
DEPENDPATH += $$PWD/../build-LibraryA-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug/debug

2条回答
闹够了就滚
2楼-- · 2019-08-14 14:58

I see a few possible scenarios:

  1. LibraryA.dll is missing (not built). Probably not the case.
  2. LibraryB is not able to find LibraryA. Make sure you have the proper library path in LibraryB 'pro' file: LIBS += -L$$PWD/Path_To_LibraryA_Dll/ -lLibraryA
  3. LibraryA project doesn't have LIBRARY_A macro defined, and the LIBRARY_A_EXPORT doesn't get defined as __declspec(dllexport), in which case, LibraryA won't export anything for external usage. Make sure you have the define in LibraryA 'pro' file: DEFINES += LIBRARY_A
查看更多
叼着烟拽天下
3楼-- · 2019-08-14 14:58

There is a good Qt wiki on that: How to create a library with Qt and use it in an application. Mind using Q_DECL_EXPORT and Q_DECL_IMPORT in the declarations depending on the project, whether it consumes or provides the dynamic library.

查看更多
登录 后发表回答