I wrote a sample program using QSqlDatabase object two years ago with a Qt configuration that depended on static libraries. It compiled ran as expected. Some time last year, I rebuilt the configuration using shared libraries and now I am getting the following errors.
Here is the error:
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) publ
ic: __thiscall QSqlDatabase::~QSqlDatabase(void)" (__imp_??1QSqlDatabase##QAE#XZ
) referenced in function _main
...
debug\qtsql.exe : fatal error LNK1120: 12 unresolved externals
Here are my includes and the instantiation of the object in main.cpp:
#include <Qt>
#include <QtDebug>
#include <QtSql\QSqlDatabase>
#include <QFile>
#include <QtSql\QSqlQuery>
#include <QString>
#include <QVariant>
#include <QDate>
int main()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
...
Also, here is my .pro file:
TEMPLATE = app
TARGET =
DEPENDPATH += . versions
INCLUDEPATH += .
# Input
SOURCES += main.cpp
# Libraries
QMAKE_LIB_DIR += C:\\Qt\\4.7.0\\lib
I thought that last line in the .pro file would give me a link to any library I might need in Qt. I'm pretty ignorant of what it takes to link to the Qt .dll's.
What am I missing to get the program to see the QSqlDatabase library?
Also, must I specify the library even though I have given it the header file and library directory?
CONFIG += qt
QT += sql
might be missing here (link to qmake docs)
Here is a link to the Qt sql examples. Perhaps you find inspiration/guidance there.
Related
I'm trying to install the SDL library for Qt 5.1 on Windows 7, but I'm stuck. I'm a total newbie with using external libraries so please bear with me, I figured this sort of thing would be pretty basic to set up.
I downloaded the SDL-devel-1.2.15-mingw32.tar.gz (Mingw32) file, extracted it, and in my project's PRO file I added:
INCLUDEPATH += "C:\SDL\SDL-1.2.15\include\SDL"
LIBS += "C:\SDL\SDL-1.2.15\bin\SDL.dll"
Now when I try to compile this:
#include <iostream>
#include <stdlib.h>
#include "SDL.h"
using namespace std;
int main()
{
SDL_Init( SDL_INIT_EVERYTHING );
cout << "Hello World!" << endl;
return 0;
}
I get this:
crt0_c.c:-1: error: undefined reference to `WinMain#16'
collect2.exe:-1: error: error: ld returned 1 exit status
Compiling with SDL and g++ cannot find -lSDLmain etc
Undefined reference to WinMain#16 when using SDL
I am sure one of those is also applicable to your question.
SDL should be prevented from overloading main method. To do so, add the following piece of code wherever you include SDL headers.
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#ifdef __MINGW32__
#undef main /* Prevents SDL from overriding main() */
#endif
To make it working in your case, your project.pro file should be like this:
LIBS += -LC:\SDL-devel-1.2.15-mingw32\SDL-1.2.15\lib -llibSDL
INCLUDEPATH +=C:\SDL-devel-1.2.15-mingw32\SDL-1.2.15\include
As it is eveident from my code, I am using sdl version 1.2.15, 32-bit for mingw compiler on Windows 8.
Reference:
To find a good tutorial about the SDL ffmpeg integration, you can refer to dranger.
I cannot link my project because of some issues with the qjpeg4 library.
Its DLL is located here:
PROJECT_ROOT/../Release/obj/imageformats/qjpeg4.dll
I get this linker error:
Linking CXX executable test.exe
CMakeFiles\test.dir/objects.a(entrypoint.cpp.obj):entrypoint.cpp:(.text.startup+0x2692): undefined reference to `qt_plugin_instance_qjpeg()'
collect2: ld returned 1 exit status
In the file containing main() I have: Q_IMPORT_PLUGIN(qjpeg)
I tried adding these lines, with no improvement:
QApplication app(argc, argv);
QString sDir = QCoreApplication::applicationDirPath();
app.addLibraryPath(sDir + "/plugins");
The previous error should even not depend on the fact that the DLL has not been found, since it is still in the link phase...
If in the file containing main() I remove Q_IMPORT_PLUGIN(qjpeg), the linker is successful, but the executable does absolutely nothing.
The only Qt headers I include are <QApplication> and <QtPlugin>; adding <QtGui> has been useless.
From this link, it seems to be a bug in Qt:
https://bugreports.qt-project.org/browse/QTBUG-24177
I am trying to compile this. I am using the AMD SDK. I am using the header files that come with the aforementioned SDK and they are located in:
C:\Program Files (x86)\AMD APP\include\CL
The tutorial states:
Header files
Just like any other external API used in C++, you must include a header file when using the OpenCLâ„¢ API. Usually, this is in the directory CL within the primary include directory. For the C++ bindings we have (replace the straight C API with cl.h):
I found that last bit a little confusing. I am using both .h and .hpp
#include <CL/cl.h> when this is used it will compile the checkErr function
#include <CL/cl.hpp> when this is used it gives me access to the cl namespace
When I try to compile this code it fails with:
'clUnloadCompiler': was declared deprecated
ADDITIONAL DETAILS (after removing #include <CL/c.h>)
It now gives the following list of errors:
error C4996: Error 2 error LNK2019: unresolved external symbol _clReleaseCommandQueue#4 referenced in function "public: static int __cdecl cl::detail::ReferenceHandler<struct _cl_command_queue *>::release(struct _cl_command_queue *)" (?release#?$ReferenceHandler#PAU_cl_command_queue###detail#cl##SAHPAU_cl_command_queue###Z)
error LNK2019: unresolved external symbol _clReleaseContext#4 referenced in function "public: static int __cdecl cl::detail::ReferenceHandler<struct _cl_context *>::release(struct _cl_context *)" (?release#?$ReferenceHandler#PAU_cl_context###detail#cl##SAHPAU_cl_context###Z)
In properties for my project I have:
added C:\Program Files (x86)\AMD APP\include\ as an additional include directory
added C:\Program Files (x86)\AMD APP\lib\x86_64 as an additional library directory
added OpenCL.lib as an additional dependency
The errors I listed happen regardless of whether or not I take the last two steps. That is, the last two do not seem to be helping or harming anything
Summary of answers I provided in comments:
For a C++ application, you only need to #include <CL/cl.hpp>
Make sure you are linking with the correct OpenCL.lib (32-bit vs. 64-bit).
That's because clUnloadCompiler() was deprecated in OpenCL 1.2.
Add
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
to your code before
#include <CL/cl.h>
#include <CL/cl.hpp>
I'm using QT4.7.3, VS2008 and trying to add QuaZip library.
I got QuaZip, Zlib and compile it, and then I can make "quazip.lib" file.
I added quazip.lib to "Project property->Configuration Properties->Linker->Input->Additional Dependencies" and include pathes.
I wrote the source code as below.
#include "Updater.h"
#include "quazip.h"
Updater::Updater(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QuaZip *qZip = new QuaZip();
}
but, here, I stuck in problem.
Error message is shown as below.
Updater.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: _thiscall QuaZip::QuaZip(void)"
(_imp_??0QuaZip##QAE#XZ) referenced in function "public: __thiscall
Updater::Updater(class QWidget *,class QFlags)"
(??0Updater##QAE#PAVQWidget##V?$QFlags#W4WindowType#Qt#####Z)
1>.\updater.exe : fatal error LNK1120: 1 unresolved externals
I know this error occurs when declaration exists but definition doesn't exists.
As a result of find QuaZip constructor, I found as below.
QuaZip::QuaZip():
p(new QuaZipPrivate(this))
{
}
How can I make it correct?
Thank you for any comment.
I have found the following message in Quazip's README file:
If you want to include QuaZIP sources directly into your project or if
you want to use QuaZIP compiled as a static library using
"qmake CONFIG+=statliclib", you have to define the QUAZIP_STATIC macro,
otherwise you're likely to run into problems as QuaZIP symbols will be
marked as dllimported.
Maybe that will help you.
I am using qt and developing a desktop app that will run under win xp/vista.
I have a 3rd party library UserAgentLib (static, and shared). But I am not sure how to link in qt creator.
I have opened the *.pro file and added my library and header path.
The library is called UserAgentLib and the header file is called UserAgentLib.h
TARGET = Dialer
TEMPLATE = app
LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib
INCLUDEPATH += D:\Projects\qtDialer\tools\inc
SOURCES += main.cpp\
catdialer.cpp
HEADERS += catdialer.h
FORMS += catdialer.ui
I think it does find the header file, as I get about 100 errors for declarations in the UserAgentLib.h file. However, I don't think it is linking with the library.
Many thanks for any suggestions,
======================
I have create a very simple library in VS C++ 2008. Here is the code for the header and source file.
Header:
// mathslibrary.hpp
int add_numbers(const int a, const int b);
Source:
// mathslibrary.cpp
#include "mathslibrary.hpp"
int add_numbers(const int a, const int b)
{
return a + b;
}
I have compiled this into a library. And tested by linking with a WIN32 console application in VS 2008. The library worked as expected.
Now when I try and link with qt.
#include <QtCore/QCoreApplication>
#include <iostream>
#include "mathslibrary.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "add numbers 40 + 60 = " << add_numbers(40, 60) << std::endl;
return a.exec();
}
This is my qmake file:
QT -= gui
TARGET = testlibrary
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS = D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib
INCLUDEPATH = D:\Projects\TestLibrary\mathsLibrary\
SOURCES += main.cpp
These are the errors I get when I try and build:
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c::-1: error: undefined reference to `WinMain#16'
:-1: error: collect2: ld returned 1 exit status
And these are the compile issues:
Running build steps for project testlibrary...
Creating gdb macros library...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/mingw/bin/mingw32-make.exe debug -w
mingw32-make: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
C:/Qt/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\testlibrary.exe -L"c:\Qt\qt\lib"
D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib -lQtCored4
mingw32-make[1]: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
mingw32-make: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:
(.text+0x104): undefined reference to `WinMain#16'
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\testlibrary.exe] Error 1
mingw32-make: * [debug] Error 2
Exited with code 2.
Error while building project testlibrary
When executing build step 'Make'
Many thanks for any advice,
Don't know if this changes anything, but maybe you have to define it like this:
LIBS += -LD:/Projects/qtDialer/tools/lib -lUserAgentLib
If you are getting compiler errors then your UserAgentLib.h probably didn't get included. You can test it with:
!exists( UserAgentLib.h ) {
error( "No UserAgentLib.h file found" )
}
You put the above in one of the .pro file and not the constructor.See this.
If the library didn't get linked (which is after your application has compiled well) -- then you need to tinker with your LIBS += ... line, though which appears fine on first glance.
Try this with the simple library first and then try it with the library you are actually trying to get working.
LIBS += D:\Projects\qtDialer\tools\lib\mathsLibrary.lib
In your .hpp file, add extern "C" before your function declarations:
// mathslibrary.hpp
extern "C" int add_numbers(const int a, const int b);
Rebuild the library from Visual Studio.
Now you should be able to compile your test app with Qt Creater. Then copy the corresponding dll into the directory with your new executable and give it a run.
As far as i understood, you generated a dll using MSVC and now you are trying to link it in Qt using mingw. right?
Object files and static libraries created with different compilers, or
even with significantly different releases of the same compiler, often
cannot be linked together. This issue is not specific to MinGW: many
other compilers are mutually incompatible. Build everything from
source with the same version of the same compiler if you can
chech this out : http://chadaustin.me/cppinterface.html