PDA

View Full Version : How to link .exe to dll ?



babygal
21st April 2010, 03:19
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.

squidge
21st April 2010, 08:05
Have a look at QLibrary

spud
21st April 2010, 08:26
The easiest way is to add the following to your app.pro file:

LIBS += -L/path/to/lib -lproj
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 (http://doc.qt.nokia.com/latest/plugins-howto.html).

babygal
22nd April 2010, 08:35
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

JD2000
22nd April 2010, 20:50
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

babygal
27th April 2010, 03:54
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

babygal
27th April 2010, 11:04
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?

spud
27th April 2010, 12:00
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.

JD2000
27th April 2010, 12:01
Which file actually contains the catDataPreparator class?

babygal
28th April 2010, 03:26
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

babygal
12th May 2010, 09:21
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++ ?

JD2000
12th May 2010, 18:08
Please post the code for
catVentricle.h
catVentricle.cpp

Has the catDataPreparator class been #included in mainwindow.cpp?

squidge
12th May 2010, 18:35
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.

babygal
13th May 2010, 07:01
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.

babygal
13th May 2010, 07:03
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 ?!

squidge
13th May 2010, 08:00
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

babygal
17th May 2010, 04:28
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?

squidge
17th May 2010, 15:55
You need to either implement your own interface or use a standard one such as COM. DLLs typically do not support exporting classes.

babygal
19th May 2010, 04:04
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 ?

squidge
19th May 2010, 09:30
No, as you will be exporting an interface.