creating library
creating dlls with Qt is very simple, you need to add export keywords to export needed entities, but there are several rules:
first of all you need to create some kind of global h-file for your library-project with import/export variables, it should look like this example
#ifndef MYWIDGET_GLOBAL_H
#define MYWIDGET_GLOBAL_H
#include <QtGlobal>
#ifdef MYWIDGET_STATICLIB
# undef MYWIDGET_SHAREDLIB
# define MYWIDGET_EXPORT
#else
# ifdef MYWIDGET_MAKEDLL
# define MYWIDGET_EXPORT Q_DECL_EXPORT
# else
# define MYWIDGET_EXPORT Q_DECL_IMPORT
# endif
#endif
#endif//MYWIDGET_GLOBAL_H
#ifndef MYWIDGET_GLOBAL_H
#define MYWIDGET_GLOBAL_H
#include <QtGlobal>
#ifdef MYWIDGET_STATICLIB
# undef MYWIDGET_SHAREDLIB
# define MYWIDGET_EXPORT
#else
# ifdef MYWIDGET_MAKEDLL
# define MYWIDGET_EXPORT Q_DECL_EXPORT
# else
# define MYWIDGET_EXPORT Q_DECL_IMPORT
# endif
#endif
#endif//MYWIDGET_GLOBAL_H
To copy to clipboard, switch view to plain text mode
then as I said earlier you need to add export keyword before class declaration, e.g.
....
#include "mywidget_global.h"
...
class MYWIDGET_EXPORT MyWidget
: public QWidget {
....
};
....
#include "mywidget_global.h"
...
class MYWIDGET_EXPORT MyWidget: public QWidget
{
....
};
To copy to clipboard, switch view to plain text mode
that's all what you need to add in source file.
then you need to prepare correct pro and pri files.
* pri-file should contain the next lines
MYWIDGET_LIB = -llibmywidget
INCLUDEPATH += $$TOP/libmywidget/include
DEPENDPATH += $$TOP/libmywidget/src
where $$TOP is start path.
* pro-file should contain the next lines
CONFIG += dll
TEMPLATE = lib
DESTDIR = lib
unix:CLEAN_FILES += $(DESTDIR)lib$(QMAKE_TARGET).*
win32:CLEAN_FILES += $(DESTDIR)$(QMAKE_TARGET).*
TOP = ../..
TARGET = libmywidget
include(libmywidget.pri)
contains(CONFIG, staticlib) {
DEFINES += MYWIDGET_STATICLIB
} else {
DEFINES += MYWIDGET_SHAREDLIB
}
win32 {
DEFINES += MYWIDGET_MAKEDLL
}
and that's all! now, when you call qmake/(n/mingw32-)make you'll get shared library.
how to attach this library to another project?
you just need to add following lines in pro-file of needed project which will use created library
...
TOP = ..
include($$TOP/libmywidget/libmywidget.pri)
LIBS += -L$$TOP/libmywidget/lib/ $$MYWIDGET_LIB
...
and then build this project.
PS. attach contsins complete example.
Bookmarks