Export C++ class from Qt Library on Windows
-
I have created a Qt shared library project using Qt Creator 2.1 (vc 2008 compiler) and Qt Library 4.7.3 on Windows.
in QtSharedLib_global.h:
@#if defined(QTSHAREDLIB_LIBRARY)
define QTSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
define QTSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif@
in QtSharedLib.h:
@#include "QtSharedLib_global.h"
class QTSHAREDLIBSHARED_EXPORT QtSharedLib {
public:
QtSharedLib();
int Sub(int a, int b);
};@in QtSharedLib.h:
@include "QtSharedLib.h"
QtSharedLib::QtSharedLib()
{
}
int QtSharedLib::Sub(int a, int b)
{
return (a-b);
}@I can build it successfully.
In my widget application. in MainWindow.cpp funtion - on_pushButton_clicked():
@ QtSharedLib *lib = new QtSharedLib();
int c = lib->Sub(4, 3);@in the .pro:
@QT += core gui
TARGET = Qt_WidgetApp
TEMPLATE = app
SOURCES += main.cpp MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
INCLUDEPATH += ../QtSharedLibLIBS += C:/Qt_Test/QtSharedLib-build-desktop/debug/QtSharedLib.lib@
I can build it successfully. But when I run it, I got popup dialog say exception triggered: The inferior stopped because it triggered an exception. Stopped in thread 0...
What is the problem?
I can import / export functions. But do not know how to inport / export class.
-
OK. I figure out something.
I forget to mention something. In the library project, I also have: FunctionsLib.h/cpp
in FunctionsLib.h
@#include "QtSharedLib_global.h"
QTSHAREDLIBSHARED_EXPORT int Add(int a, int b);@in FunctionsLib.cpp
@#include "FunctionsLib.h"
QTSHAREDLIBSHARED_EXPORT int Add(int a, int b)
{
return (a+b);
}@If I remove the FunctionsLib.h/cpp from library. Then build both dll and widget application project OK and Run application no error.
So question is:
Can I include multiple h/cpp files in the library? If yes, how to do it? if no, then how to export multiple classes? I like one file for one class.
-
Yes, you can have severeal .h/.cpp files in one library and you can export many symbols, too.
Try giving "Add" another name or putting it into a namespace. Maybe some other library is already exporting a "Add"-symbol and you get a conflict... which will be resolved one way or the other with only one of the implementations being used. That can trigger all kinds of strange effects:)
In general you only need to export/import a symbol in the header file, too, so consider to remove the QTSHAREDLIBSHARED_EXPORT from the .cpp file.
[EDIT: fixed strike-through markup, Volker]
-
I have tried:
Change 'Add' to 'MyAdd'
Remove QTSHAREDLIBSHARED_EXPORT from the .cpp file.
But same error.
-
Finally I got it!
the dll library project folder:
C:/Qt_Test/QtSharedLibthe build folder:
C:/Qt_Test/QtSharedLib-build-desktopthe widget application project folder:
C:/Qt_Test/Qt_WidgetAppthe widget application build folder:
C:/Qt_Test/Qt_WidgetApp-build-desktopIn the widget application .pro file:
LIBS += C:/Qt_Test/QtSharedLib-build-desktop/debug/QtSharedLib.libIf I copy the dll file to the folder:
C:/Qt_Test/Qt_WidgetApp-build-desktop/debugSo question is: how to set dll path in .pro file?
-
It seems no way to set dll library path inside .pro. Anyway, I could set PATH in windows environment so that exe will use to find the dll path.