PDA

View Full Version : How to develop platform independ "DLL"



alxwang
7th March 2009, 15:44
In windows if I use dllexport or DEF file I can compile a DLL and supply programmer a LIB and H file so he can use my DLL very easy.
In QT how can I do same thing? What I want is:
I develop a project, it can be compile to DLL in window, .so in Linux and I also got the LIB file.
I also need to export not only C function but also C++ class in this DLL.

What I should do?

Thanks a lot!

fullmetalcoder
7th March 2009, 16:02
qmake offers you a simple way to create shared libs (DLL) :
TEMPLATE = lib
CONFIG *= shared dll
This will build your project as a shared lib (as opposed to a static one) but it will also create (on systems that require it) a static lib (or the equivalent) required to link against the shared lib (.lib files with msvc, .a files with mingw, ...)

Note that the second line is optional, on most installs shared libs are the default.

Now, there is an extra step if you want your DLL to be fully cross platform. You need to rely on an "export macro" :


// global header file defining the export macro
// could be named something like mylib-config.h

// the Q_DECL_* macros are defined here
#include <qtglobal.h>

#ifdef _MYLIB_BUILD_
#define MYLIB_EXPORT Q_DECL_EXPORT
#else
#define MYLIB_EXPORT Q_DECL_IMPORT
#endif

Then you have to manually specify classes/methods you wan to export :



#include "mylib-config.h"

class MYLIB_EXPORT ExportedClass
{
// this class will be available to apps linking against the lib on ALL platforms
};

class SomewhatPrivateClass
{
// this class will NOT be available to apps linking against the app on SOME platforms (e.g windows) but will be on others (e.g linux)
};

// see above (name self-explanatory)
MYLIB_EXPORT void exportedFunction();

// see above
void somewhatPrivateFunction();


Last, but not least, you need to add a define to your project when building the lib :

DEFINES += _MYLIB_BUILD_

Note that you could tweak the macro definition to allow direct embedding of the lib into an app or compilation as a static lib.

P.S : this topic has already been covered in the forum if I remember well. Using the search features of the forum is highly recommended...