PDA

View Full Version : dll using qt



sepp
26th November 2008, 20:20
hi there,

i'm not really a qt newbie - but in this field here i am :o

i have to develop a dll (and a so, later, too) which uses qtcore. in particular i need container classes, threads and mutexes as well as signals and slots. i don't need any gui stuff.

my first question: i have qt 4.3.3 installed, as well as the vs 2005 integration. now, when i choose 'new project' and specify 'qt library', i obviously get a dll project and i can build it - but - where is DllMain() :confused:

do i have to add the code?

my second question: do i have to have a QCoreApplication? What if the caller of my dll runs it's own event loop?

thanks for any advice,

sepp

spirit
27th November 2008, 06:04
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

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
{
....
};

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.

sepp
27th November 2008, 08:16
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

and then build this project.
PS. attach contsins complete example.
ok, thanks. so i probally don't need a DllMain()... i'll find out.

however, the application which will use my library will not be a qt project. therefore, again: do i need a QCoreApplication in my dll to use signals/slots, qt threads/mutexes and qt container classes? may this conflict with the applications own event loop?

thanks,

sepp