问题
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
回答1:
I see a few possible scenarios:
- LibraryA.dll is missing (not built). Probably not the case.
- 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
- LibraryA project doesn't have
LIBRARY_A
macro defined, and theLIBRARY_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
回答2:
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.
来源:https://stackoverflow.com/questions/34663527/how-do-i-export-and-link-to-a-dll-using-qt-creator-on-windows