Results 1 to 20 of 20

Thread: How to link .exe to dll ?

  1. #1
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy How to link .exe to dll ?

    How to link an existing .exe to a DLL?
    I have created a project, executable called, app.exe.
    Then another project, executable called, proj.dll, together with proj.lib and few other header files.
    How do I link my first project, app.exe to the proj.dll?
    What are the step by step details and configuration settings I am required to do, so that both the projects
    can be linked successfully?
    I wish to link these 2 projects at run time.
    I'm doing programming for Windows platform, and using Qt 4.6.2 and Qt Creator 1.3.1.

    Please help.


    Newbie.
    Last edited by babygal; 21st April 2010 at 03:53.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Have a look at QLibrary

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

    babygal (28th April 2010)

  4. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to link .exe to dll ?

    The easiest way is to add the following to your app.pro file:
    Qt Code:
    1. LIBS += -L/path/to/lib -lproj
    To copy to clipboard, switch view to plain text mode 
    Then you just need to put the exe and dll in the same folder.

    Note: By doing this the exe will depend on the dll and won't run without it. If you intended to load a dll optionally at runtime You should go with QLibrary or check out the Qt plugin system.

  5. The following user says thank you to spud for this useful post:

    babygal (28th April 2010)

  6. #4
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    HI, I followed as per your advice. I'm getting some errors as shown below(in compile output).
    FYI:
    catDataPreparator::~catDataPreparator() & catDataPreparator::catDataPreparator() are functions declared in one of the header files which I included(inside ./Temp folder) in my app.exe project .The definitions are in the proj.dll project.

    Q :
    1> Are the .exe and .dll linking now?
    2>What are the errors and how do I resolve these problems?

    Qt .pro code:
    --------------------------------------------------------------------------------------------------------------------------------------------------
    TEMPLATE = app
    LIBS += -L./include -lCAT_DLL #-L/path/to/lib -lproj
    INCLUDEPATH += ./include
    INCLUDEPATH += ./lib
    INCLUDEPATH += ./Debug
    INCLUDEPATH += ./Temp

    Compile output:
    -------------------------------------------------------------------------------------------------------------------------------------------------
    g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\Cardiac.exe object_script.Cardiac.Debug -L"c:\Qt\2010.02.1\qt\lib" -lmingw32 -lqtmaind -L./include -lCAT_DLL -lQtGuid4 -lQtCored4
    ./debug\main.o: In function `~MainWindow':
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.h:36: undefined reference to `catDataPreparator::~catDataPreparator()'
    ./debug\mainwindow.o: In function `MainWindow':
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:22: undefined reference to `catDataPreparator::catDataPreparator()'
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:43: undefined reference to `catDataPreparator::~catDataPreparator()'
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:22: undefined reference to `catDataPreparator::catDataPreparator()'
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:43: undefined reference to `catDataPreparator::~catDataPreparator()'
    ./debug\moc_mainwindow.o:moc_mainwindow.cpp:(.text$_ ZN10MainWindowD0Ev[MainWindow::~MainWindow()]+0x27): undefined reference to `catDataPreparator::~catDataPreparator()'
    collect2: ld returned 1 exit status
    mingw32-make[1]: Leaving directory `C:/1Work/Cardiac_CAT DLL_WIP/Cardiac'
    mingw32-make[1]: *** [debug\Cardiac.exe] Error 1
    mingw32-make: Leaving directory `C:/1Work/Cardiac_CAT DLL_WIP/Cardiac'
    mingw32-make: *** [debug] Error 2
    Exited with code 2.
    Error while building project Cardiac
    When executing build step 'Make'
    -------------------------------------------------------------------------------------------------------------------------------------------------------EOM

  7. #5
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to link .exe to dll ?

    You may want to look at http://doc.trolltech.com/4.6/qmake-manual.html paying attention to the LIBS and INCLUDEPATH links.

    I think what you need is:

    LIBS += $$quote(C:/path to/proj.dll) ## (put in the correct path here)
    INCLUDEPATH += ./include
    INCLUDEPATH += ./Debug
    INCLUDEPATH += ./Temp

  8. The following user says thank you to JD2000 for this useful post:

    babygal (28th April 2010)

  9. #6
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by JD2000 View Post
    You may want to look at http://doc.trolltech.com/4.6/qmake-manual.html paying attention to the LIBS and INCLUDEPATH links.

    I think what you need is:

    LIBS += $$quote(C:/path to/proj.dll) ## (put in the correct path here)
    INCLUDEPATH += ./include
    INCLUDEPATH += ./Debug
    INCLUDEPATH += ./Temp
    Hi, I changed to as per below but I still get the same error.

    LIBS += ./include/proj.lib
    Last edited by babygal; 27th April 2010 at 09:53.

  10. #7
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    I have confirmed that the library and header files can be found during compiling and linking.(i confirmed this by(trial and error method)by putting a non existing path or library name,then the compile output gives error that the file not found).

    The following error still remains :
    ./debug\main.o: In function `~catVentricle':
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/header/catVentricle.h:8: undefined reference to `feMesh::~feMesh()'
    ./debug\mainwindow.o: In function `MainWindow':
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:22: undefined reference to `catDataPreparator::catDataPreparator()'
    C:\1Work\Cardiac_CAT DLL_WIP\Cardiac/mainwindow.cpp:22: undefined reference to `catDataPreparator::catDataPreparator()'
    collect2: ld returned 1 exit status


    And after this, if I remove the declaration and definition of class constructors and destructors, there will be no error in compilation and linking.
    However, we need the class constructors and destructors!
    How do I resolve my problem?

  11. #8
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to link .exe to dll ?

    You have to tell the compiler that the classes feMesh and catDataPreparator are external dependencies.
    Check out http://doc.trolltech.com/4.6/sharedlibrary.html. especially the Q_DECL_EXPORT and Q_DECL_IMPORT macros.

  12. The following user says thank you to spud for this useful post:

    babygal (30th April 2010)

  13. #9
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to link .exe to dll ?

    Which file actually contains the catDataPreparator class?

  14. The following user says thank you to JD2000 for this useful post:

    babygal (30th April 2010)

  15. #10
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by JD2000 View Post
    Which file actually contains the catDataPreparator class?
    Hi,
    catDataPreparator.h contains the catDataPreparator class. This header file is in : ./Temp . It is one of the header files along which I included from the .dll project.

    .pro :
    INCLUDEPATH += ./Temp

  16. #11
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by spud View Post
    You have to tell the compiler that the classes feMesh and catDataPreparator are external dependencies.
    Check out http://doc.trolltech.com/4.6/sharedlibrary.html. especially the Q_DECL_EXPORT and Q_DECL_IMPORT macros.
    But I think this link mentions for shared library in Qt. and not if the shared library is in MVS C++.
    Any link which explains on how to link between an app compiled with GCC C++ compiler and a dll compiled in MVS C++ ?

  17. #12
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to link .exe to dll ?

    Please post the code for
    catVentricle.h
    catVentricle.cpp

    Has the catDataPreparator class been #included in mainwindow.cpp?
    Got to keep the loonies on the path ...

  18. The following user says thank you to JD2000 for this useful post:

    babygal (20th May 2010)

  19. #13
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by babygal View Post
    But I think this link mentions for shared library in Qt. and not if the shared library is in MVS C++.
    Any link which explains on how to link between an app compiled with GCC C++ compiler and a dll compiled in MVS C++ ?
    You can not link together a class compiled in MSVC++ with a class compiled in GCC, they are not compatible. The DLL exports will have to be externally declared as C so they don't get name mangled.

  20. The following user says thank you to squidge for this useful post:

    babygal (20th May 2010)

  21. #14
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by JD2000 View Post
    Please post the code for
    catVentricle.h
    catVentricle.cpp

    Has the catDataPreparator class been #included in mainwindow.cpp?
    ? - How to include catDataPreparator class in mainwindow.cpp ?


    code in catVentricle.h:
    ====== ========== =========
    #ifndef CATVENTRICLE_H
    #define CATVENTRICLE_H

    #include "feMesh.h"

    class catVentricle
    {
    public:
    feMesh mesh;

    catVentricle(void);
    ~catVentricle(void);

    };

    #endif

    =================================================
    The catVentricle.cpp code in not included in my project(.exe) . It is in .dll project.

  22. #15
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by fatjuicymole View Post
    You can not link together a class compiled in MSVC++ with a class compiled in GCC, they are not compatible. The DLL exports will have to be externally declared as C so they don't get name mangled.
    1? So if I want to link a class compiled in MSVC++ with a class compiled in GCC , I have to use DLL export method?
    2? If yes, is there any good and clear example for me to study?

    3? If I were to use the DLL export method, the changes/implementation is required in both the .EXE and .DLL project or
    just the .EXE project alone ?!
    Last edited by babygal; 13th May 2010 at 07:39.

  23. #16
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Yes, but then you can't export a class, only functions & variables.

    If you really need to export a class, then on Windows, you can export class by changing DLL to be COM object.

    http://www.codeproject.com/KB/COM/comintro.aspx

  24. The following user says thank you to squidge for this useful post:

    babygal (20th May 2010)

  25. #17
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    Quote Originally Posted by fatjuicymole View Post
    Yes, but then you can't export a class, only functions & variables.

    If you really need to export a class, then on Windows, you can export class by changing DLL to be COM object.

    http://www.codeproject.com/KB/COM/comintro.aspx
    If I need to import functions in a class..How do I do it?

  26. #18
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    You need to either implement your own interface or use a standard one such as COM. DLLs typically do not support exporting classes.

  27. The following user says thank you to squidge for this useful post:

    babygal (20th May 2010)

  28. #19
    Join Date
    Apr 2010
    Location
    Singapore
    Posts
    156
    Thanks
    47
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    If I implement COM method in Windows, then do I need to create shared libraries using Qt macros - Q_DECL_EXPORT and Q_DECL_IMPORT in Qt project as well ?

  29. #20
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to link .exe to dll ?

    No, as you will be exporting an interface.

  30. The following user says thank you to squidge for this useful post:

    babygal (20th May 2010)

Similar Threads

  1. How to link an existing .exe to a DLL?
    By babygal in forum Qt Programming
    Replies: 23
    Last Post: 27th April 2010, 08:08
  2. Link as a dll
    By bmahf in forum Newbie
    Replies: 8
    Last Post: 15th October 2009, 19:21
  3. Link against library on Mac OS
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 22nd March 2009, 11:10
  4. How I link a dll?
    By brevleq in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2007, 02:39

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.