Results 1 to 5 of 5

Thread: Problem with linking with homemade dll

  1. #1
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Problem with linking with homemade dll

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with linking with homemade dll

    You don't need a tutorial, just add a LIBS+=-lnameofthelibrary to your qmake project and rerun qmake.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with linking with homemade dll

    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:
    Qt Code:
    1. TARGET = mydll
    2. TEMPLATE = lib
    3.  
    4. CONFIG += dll
    5.  
    6. win32 {
    7. DEFINES += MYDLL_EXPORTS _MBCS _USRDLL
    8. # would be better to have an rc file to add some descriptions shown in the explorer
    9. # but without win, I do not have an example :p
    10. # RC_FILE = MYDLL.rc
    11. }
    12. SOURCES += mydll.cpp
    13. HEADERS += mydll.h
    To copy to clipboard, switch view to plain text mode 

    mydll.h:
    Qt Code:
    1. #ifndef MYDLL_H
    2. #define MYDLL_H
    3. // to know the OS...
    4. #include <QString>
    5. #if( defined Q_OS_WIN32 )
    6. #ifdef MYDLL_EXPORTS
    7. #define MYDLL_API __declspec(dllexport)
    8. #else
    9. #define MYDLL_API __declspec(dllimport)
    10. #endif
    11. #else
    12. #define MYDLL_API
    13. #endif
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    mydll.cpp:
    Qt Code:
    1. #include "mydll.h"
    2. #ifdef Q_OS_WIN32
    3. #include <windows.h>
    4. BOOL APIENTRY DllMain( HANDLE hModule,
    5. DWORD ul_reason_for_call,
    6. LPVOID lpReserved
    7. )
    8. {
    9. switch (ul_reason_for_call)
    10. {
    11. case DLL_PROCESS_ATTACH:
    12. case DLL_THREAD_ATTACH:
    13. case DLL_THREAD_DETACH:
    14. case DLL_PROCESS_DETACH:
    15. break;
    16. }
    17. return TRUE;
    18. }
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    Any other header class:
    Qt Code:
    1. #ifndef EXTENDER_H
    2. #define EXTENDER_H
    3. #include "mydll.h"
    4. class MYDLL_API TMyDllExtender
    5. {
    6. };
    7. #endif
    To copy to clipboard, switch view to plain text mode 

    Program which uses this class:
    Qt Code:
    1. #include "extender.h"
    2.  
    3. void main()
    4. {
    5. TMyDllExtender ext;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    pro file of this program:
    Qt Code:
    1. LIBS += -lmydll
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to auba for this useful post:

    Synk (17th May 2009)

  5. #4
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problem with linking with homemade dll

    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:
    Qt Code:
    1. DEFINES += MYDLL_EXPORTS _MBCS _USRDLL
    To copy to clipboard, switch view to plain text mode 
    I know what MYDLL_EXPORTS mean, but I don't know what _MBCS and _USRDLL mean?

    Could you also explain code from mydll.cpp?
    Qt Code:
    1. #ifdef Q_OS_WIN32
    2. #include <windows.h>
    3. BOOL APIENTRY DllMain( HANDLE hModule,
    4. DWORD ul_reason_for_call,
    5. LPVOID lpReserved
    6. )
    7. {
    8. switch (ul_reason_for_call)
    9. {
    10. case DLL_PROCESS_ATTACH:
    11. case DLL_THREAD_ATTACH:
    12. case DLL_THREAD_DETACH:
    13. case DLL_PROCESS_DETACH:
    14. break;
    15. }
    16. return TRUE;
    17. }
    18. #endif
    To copy to clipboard, switch view to plain text mode 

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

    Thank you for help.
    Paweł
    Attached Files Attached Files

  6. #5
    Join Date
    May 2009
    Posts
    61
    Thanks
    5
    Thanked 6 Times in 6 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with linking with homemade dll

    Quote Originally Posted by Synk View Post
    I don't know what _MBCS and _USRDLL mean?
    Me either 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

    Quote Originally Posted by Synk View Post
    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
    Qt Code:
    1. extern MyClass myclass;
    To copy to clipboard, switch view to plain text mode 
    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?

    Resume: avoid those externals

Similar Threads

  1. linking problem (ffmpeg)
    By kralyk in forum Qt Programming
    Replies: 2
    Last Post: 13th April 2009, 07:35
  2. Qt linking problem
    By LovesTha in forum Newbie
    Replies: 3
    Last Post: 30th March 2009, 23:31
  3. problem with order of libs during linking
    By minimax in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 10:32
  4. Linking problem on Mac os x 10.4
    By maverick_pol in forum General Programming
    Replies: 0
    Last Post: 6th January 2008, 14:09
  5. why linking problem with QGLWidget???
    By Shuchi Agrawal in forum Newbie
    Replies: 17
    Last Post: 16th March 2007, 10:45

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.