Results 1 to 2 of 2

Thread: How to develop platform independ "DLL"

  1. #1
    Join Date
    Feb 2009
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to develop platform independ "DLL"

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to develop platform independ "DLL"

    qmake offers you a simple way to create shared libs (DLL) :
    qmake Code:
    1. TEMPLATE = lib
    2. CONFIG *= shared dll
    To copy to clipboard, switch view to plain text mode 
    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" :
    Qt Code:
    1. // global header file defining the export macro
    2. // could be named something like mylib-config.h
    3.  
    4. // the Q_DECL_* macros are defined here
    5. #include <qtglobal.h>
    6.  
    7. #ifdef _MYLIB_BUILD_
    8. #define MYLIB_EXPORT Q_DECL_EXPORT
    9. #else
    10. #define MYLIB_EXPORT Q_DECL_IMPORT
    11. #endif
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. #include "mylib-config.h"
    2.  
    3. class MYLIB_EXPORT ExportedClass
    4. {
    5. // this class will be available to apps linking against the lib on ALL platforms
    6. };
    7.  
    8. class SomewhatPrivateClass
    9. {
    10. // 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)
    11. };
    12.  
    13. // see above (name self-explanatory)
    14. MYLIB_EXPORT void exportedFunction();
    15.  
    16. // see above
    17. void somewhatPrivateFunction();
    To copy to clipboard, switch view to plain text mode 

    Last, but not least, you need to add a define to your project when building the lib :
    Qt Code:
    1. DEFINES += _MYLIB_BUILD_
    To copy to clipboard, switch view to plain text mode 

    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...
    Last edited by wysota; 7th March 2009 at 17:53.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    alxwang (10th March 2009)

Similar Threads

  1. Crash handler on Win32
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 19:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.