PDA

View Full Version : Problem with linking with homemade dll



Synk
17th May 2009, 00:56
Hi

I'm trying to create dll and link with simple Qt application.
It looks that I have created dll in good way.
I has extern "C" and definition for import export of classes and functions in dll.
Is very any good tutorial how to make dll in qt and use with new Qt application?
I have found some examples in Internet but I would like to see good example of let's say dll with one object and application which uses this object.
Do you know any good tutorials about it?

Regards,
Pawel

wysota
17th May 2009, 10:44
You don't need a tutorial, just add a LIBS+=-lnameofthelibrary to your qmake project and rerun qmake.

auba
17th May 2009, 10:52
This is windows related stuff, as you don't need these cryptic export definitions on linux (unix?) systems. There you even can overwrite the malloc function to debug memory allocation...

.pro file:


TARGET = mydll
TEMPLATE = lib

CONFIG += dll

win32 {
DEFINES += MYDLL_EXPORTS _MBCS _USRDLL
# would be better to have an rc file to add some descriptions shown in the explorer
# but without win, I do not have an example :p
# RC_FILE = MYDLL.rc
}
SOURCES += mydll.cpp
HEADERS += mydll.h


mydll.h:


#ifndef MYDLL_H
#define MYDLL_H
// to know the OS...
#include <QString>
#if( defined Q_OS_WIN32 )
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
#else
#define MYDLL_API
#endif
#endif


mydll.cpp:


#include "mydll.h"
#ifdef Q_OS_WIN32
#include <windows.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif


Any other header class:


#ifndef EXTENDER_H
#define EXTENDER_H
#include "mydll.h"
class MYDLL_API TMyDllExtender
{
};
#endif


Program which uses this class:


#include "extender.h"

void main()
{
TMyDllExtender ext;
...
}


pro file of this program:


LIBS += -lmydll

Synk
17th May 2009, 19:11
Hi,

Thank you for useful reply. I finally compiled example successfully.
I have mad some modifications, but finally I have working dll.

I have question on defines in project file:


DEFINES += MYDLL_EXPORTS _MBCS _USRDLL

I know what MYDLL_EXPORTS mean, but I don't know what _MBCS and _USRDLL mean?

Could you also explain code from mydll.cpp?


#ifdef Q_OS_WIN32
#include <windows.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#endif


I know that it is windows specific code, but I dont know the meaning.

Thank you for help.
Paweł

auba
17th May 2009, 21:08
I don't know what _MBCS and _USRDLL mean?

Me either :D I think these both defines where once set in a Visual Project File, and I needed them somehow. Maybe you can cut or just ignore them



Could you also explain code from mydll.cpp?


Once again, this is a copy of a Visual Studio Generated DLL project. When I dont have it, I get (or got) unresolved externals. You can add some qDebugs and see, when your dll is loaded or when a process attaches to/detaches from it. Has something to do with the loading mechanism. Maybe you can even trigger how many times it is loaded when used by two program (I think I needed it to find some problems when using a static library with

extern MyClass myclass;
which was linked against the main program and such a dll. Quiz: How many instances of MyClass are generated? If more than one, which one uses the main app and which the dll? :p

Resume: avoid those externals :)