Create and use dll in Qt Creator on Windows
-
I would like to create a dll and use the dll in Qt Creator on Windows XP. My Qt Creator use the VC 2008 compiler.
First, I use Creator to create a shared library –
I have create 4 files: FunctionsLib.h / cpp and QtSharedLib.h / cpp
========
FunctionsLib.h@
#ifndef FUNCTIONSLIB_H
#define FUNCTIONSLIB_H
#endif#include “QtSharedLib_global.h”
QTSHAREDLIBSHARED_EXPORT int Add(int a, int b);
@========
FunctionsLib.cpp@
#include “FunctionsLib.h”QTSHAREDLIBSHARED_EXPORT int Add(int a, int b)
{ return (a+b);
}
@========
QtSharedLib.h@
#ifndef QTSHAREDLIB_H
#define QTSHAREDLIB_H
#include “QtSharedLib_global.h”class QTSHAREDLIBSHARED_EXPORT QtSharedLib {
public:
QtSharedLib();
int Add(int a, int b);
};
#endif // QTSHAREDLIB_H
@========
QtSharedLib.cpp@
#include “QtSharedLib.h”QtSharedLib::QtSharedLib()
{
}
int QtSharedLib::Add(int a, int b)
{ return (a+b);
}
@It builds succefully.
Then I create a Qt Widget application, add a button. I change the .pro file:
@
QT += core gui
TARGET = Qt_WidgetApp
TEMPLATE = app
SOURCES += main.cpp MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.uiINCLUDEPATH += ../QtSharedLib
LIBS += ./debug/QtSharedLib.lib
LIBS += ./debug/QtSharedLib.dll
@in the button click handling routine:
@
void MainWindow::on_pushButton_clicked()
{ int c = Add(1, 2); c = c+ 1;
}
@When I try to build the application, I got:
@
C:\Qt Test\Qt_WidgetApp-build-desktop\debug\QtSharedLib.dll:-1: error: LNK1107: invalid or corrupt file: cannot read at 0×2C8
@What is the problem?
[EDIT: code formatting, Volker]
-
I don't know if that's the problem, but this line is not necessary:
@
LIBS += ./debug/QtSharedLib.dll
@ -
Even I comment it out, I still can not build it successfully.
-
My first guess would be that if in your "whatever_global.h"
@#if defined(TEST_LIBRARY)define whateverSHARED_EXPORT Q_DECL_EXPORT
#else
define whateverSHARED_EXPORT Q_DECL_IMPORT
#endif@
looks like this, so your whateverSHARED_EXPORT macro isn't good for you, use Q_DECL_EXPORT only. (If not, then it's OK.)
The more important is, that you have to add to your .pro file the
@LIBS -Ldebug -lQtSharedLib@
instead of the other lines (if your dll is in your debug folder and your dll name is QtSharedLib.dll, if not, write the path of the folder which contains your dll file after -L and write the dll name without the .dll extension afterl -l). -
In QtSharedLib_global.h
@#if defined(QTSHAREDLIB_LIBRARY)
define QTSHAREDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
define QTSHAREDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif@
By the way,
LIBS -Ldebug -lQtSharedLib
is not for win32, is that right? I try it and give error.
-
It is for win, my bad, what I wrote is wrong :)
@LIBS += -Ldebug -lQtSharedLib @
Don't forget the += symbol like I did. -
I tried to use:
@LIBS += -Ldebug -lQtSharedLib @
But failed. The error:
:-1: error: LNK1104: cannot open file 'lQtSharedLib.obj'
But if I try to use:
@LIBS += -Ldebug -QtSharedLib @
It seems work. My question is - Why I need the obj file? What happens if I only have dll file?
-
Also, When I set a break point inside on_pushButton_clicked(), when I tried to step through it. I always got into Disassembler, not the c++ source code. Anyone know why?
-
The COFF file message is due to a compiler bug. See https://qt-project.org/wiki/Category:Tools::msvc