How to embed SQLite into a C++ project - c++

I'm trying to embed SQLite into my project. I have included the following files into a directory called lite : sqlite3.dll, sqlite3.h, and sqlite3.lib.
This is my project:
#include <stdio.h>
#include <lite/sqlite3.h>
int main(int argc, char* argv[])
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("test.db", &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}else{
fprintf(stderr, "Opened database successfully\n");
}
sqlite3_close(db);
}
I get the following errors when I run the project:
:-1: error: cannot find -lsqlite3d
collect2.exe:-1: error: error: ld returned 1 exit status
What could I be doing wrong?
I'm working in Qt. This is my .pro file:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3d
else:unix: LIBS += -L$$PWD/lite/ -lsqlite3
INCLUDEPATH += $$PWD/lite
DEPENDPATH += $$PWD/lite

Since you are already using Qt why don't you use Qt SQL module? You are going to save a lot of pain, you will bypass this kind of linkage problems just by adding
QT += sql
to you Qt project file, and adding
#include <QtSql>
to you surce files. You'll have a lot of model-view classes that facilitate the integration of the database into your application UI.
This is the recommended way to use SQL in Qt applications, unless you have very very specific needs. You can have Qt use different SQL engines under the hood (SQLite, MySQL,...), but Qt will abstract all this for you.

Despite too late, there are a couple of issues here which I think were completely left out in this question, even though the workaround is practical ..
The error is quite clear .. linker could not find the "debug" version of the lib lsqlite3d which is so configured in qmake .pro file. either get the "debug" file of the lib, or remove this line from .pro
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lite/ -lsqlite3d
This seems a common mistake when adding a library in Qt .. there is an option like :
Add "d" suffix for debug version
And this must be deselected when there is no debug version of the lib.
Second and the essence of this answer, since SQLite is written in C , the include
section is not correct and wont work; It must be corrected to be:
extern "C" {
#include <lite/sqlite3.h>
}
With these two issues corrected .. there should be no problem adding SQLite library, or, in general, any C library to Qt.
Practically with small code like SQLite, another option is to statically compile it with the project .. by just adding sqqlite3.h and sqlite3.c to the Qt project and removing the linkage to sqlite3.lib in .pro .. with the include section as updated.

Related

Undefined reference errors linking a dll in QtCreator

I have built a simple dll using g++ under mingw:
We have an include file:
#ifndef __TESTDLL_H
#define __TESTDLL_H
class sineCalculator
{
double n;
public:
sineCalculator();
sineCalculator(double);
double sine();
void setAngle(double);
};
#endif
Then an implementation cpp:
#include <testdll.h>
#include <math.h>
sineCalculator::sineCalculator()
{
n = 0;
}
sineCalculator::sineCalculator(double x)
{
n = x;
}
double sineCalculator::sine()
{
return sin(n);
}
void sineCalculator::setAngle(double x)
{
n = x;
}
This I have compiled as a dll.producing both a .dll file and .a import library with the names visaTest.dll and libvisaTest.a
I can write a small program and link to this dll successfully just using the command line - no Qt just g++. I can create objects and run the methods all fine.
However, I now want to add this library to a Qt application within QtCreator. I have followed the advice of several questions and added these lines to my .pro file:
INCLUDEPATH += C:/msys64/home/hoyla/libs/
INCLUDEPATH +=C:/msys64/home/hoyla/includes/
DEPENDPATH +=C:/msys64/home/hoyla/libs/
win32:CONFIG(release, debug|release): LIBS += -LC:/msys64/home/hoyla/libs/ -lvisaTest
else:win32:CONFIG(debug, debug|release): LIBS += -LC:/msys64/home/hoyla/libs/ -lvisaTest
else:unix: LIBS += -L$$PWD/../../../../libs/ -lvisaTest
Although I use some absolute paths here I have also tried with relative paths. However, I keep getting undefined reference errors to my dll functions when building the Qt file. I would point out that the paths point to the location of my .a import library, the .dll itself is within the system path. What am I doing wrong here?
Mea Culpa! It seems I had compiled my library under 64-bit MinGW but Qt Creator was defaulting to the 32-bit toolchain. One would have thought that the linker might have been able to determine that was the error and generate a more useful error message but there we go. Lesson learned - make sure you're using the tools you think you're using.

QtCreator LNK2019 error with external library

I have a problem when I want to link a library to my Qt project.
When I try to include an external library (libnodave.lib) in Qt Creator and try to build it, the following error occurs.
main.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_daveSetDebug referenced in function main
I'm pretty sure that I included all needed files in my project and the .pro file. I used the "Add Library" wizard to add the library.
After no success with Qt Creator, I created a minimal example with Visual Studio. When I include all the needed files to the VS project, I can build and run it without errors. So I think that there must be a problem with Qt Creator linking the library. I also tried the Qt-Visual-Studio-Add-in, but there, the same error occurs.
Here are my minimal examples with the library I want to include.
In the Visual Studio example, I added the library path, the include path, and the name of the library to the project properties. It works.
I hope you can help me with my problem.
EDIT:
I want to use the library to get some data from a S7-300 SPS device.
The following code is the minimal example from Qt Creator.
#include <QCoreApplication>
#include <QDebug>
#include <nodave.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
daveInterface *di;
daveSetDebug(daveDebugConnect); // Function of libnodave Library
qDebug() << "Hello World";
return a.exec();
}
This is the whole code from the Visual Studio minimal example.
#include "stdafx.h"
#include <nodave.h>
int _tmain(int argc, _TCHAR* argv[])
{
daveInterface *di;
daveSetDebug(daveDebugConnect);
printf("Hello World\n");
return 0;
}
The code is very small, so I don't think that there is an error inside.
That's why I think it must be a problem with the Qt linker or something like that.
EDIT:
My .pro file.
QT += core
QT -= gui
TARGET = qtminimal
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32: LIBS += -L$$PWD/../libnodave-0.8.5/win/ -llibnodave
INCLUDEPATH += $$PWD/../libnodave-0.8.5
DEPENDPATH += $$PWD/../libnodave-0.8.5
The problem was that the Qt project is 64bit and the library I want to include is only 32bit.
So I downloaded the 32bit version of Qt and now it works.
I found the mistake, when I tried to build only the minimal example with libnodave, without any 64bit Qt libraries.
By creating a new Qt project in VS2013, with this workaround and adding the libnodave library afterwards I could change whether it should be a 64bit or 32bit build. By choosing the 32bit build, the Qt library creates errors but not the libnodave lib. When I choose 64bit build, only libnodave creates the errors.
I hope it is useful for someone else.

Qt Creator - OpenCV - undefined reference/can't find libraries

Windows 7
Qt 5.4.0
OpenCV 2.4.10
Mingw 4.9.1
I'm trying to create a simple Hello World app, to test if Qt is working with OpenCV. Besides the stuff created by default, the code is
#include <opencv/cv.h>
#include <opencv/highgui.h>
(...)
cv::Mat image= cv::imread("pic.jpg");
cv::namedWindow("Test picture");
cv::imshow("Test picture", image);
cv::waitKey(1000);
(...)
However, I'm having trouble linking libraries. At first I've set the paths to *.dll.a files manually
INCLUDEPATH += C:/opencv/my_build/install/include
INCLUDEPATH += C:/opencv/my_build/install/include/opencv
INCLUDEPATH += C:/opencv/my_build/install/include/opencv2
LIBS += -LC:/opencv/my_build/install/x86/mingw/lib \
-llibopencv_core2410 \
-llibopencv_highgui2410
etc etc
But then I'd get a cannot find -llibopencv_<lib>2410 error. I've even used Qt Creator's "Add library" function (Projects -> rightclick -> add library), the result is the same. That error only goes away if I change -llibopencv_<lib>2410 to -opencv_<lib>2410. Which from my understanding is weird, as the "l" argument is missing.
Even though that error goes away, a bunch of others replace it, as I get undefined reference to cv::EVERYTHING error. I've looked around, found solutions to either problem, but solving one leads to the other and vice versa.
I'm fairly sure the OpenCV build is not at fault - I've already completed a pretty big console-ish app using it with Code::Blocks.
My current *.pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:/opencv/my_build/install/include
win32: LIBS += -L$$PWD/../../../../../../OpenCV/my_build/install/x86/mingw/lib/ -llibopencv_core2410
INCLUDEPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
DEPENDPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
win32: LIBS += -L$$PWD/../../../../../../OpenCV/my_build/install/x86/mingw/lib/ -llibopencv_highgui2410
INCLUDEPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
DEPENDPATH += $$PWD/../../../../../../OpenCV/my_build/install/x86/mingw
How to correctly link the OpenCV libraries to Qt Creator? Am I missing something?
edit: I've rebuilt the whole damn thing and it still doesn't work. God damn it.
When specifying library, you need to exclude the 'lib' part:
LIBS += -LC:/opencv/my_build/install/x86/mingw/lib \
-lopencv_core2410 \
-lopencv_highgui2410
Frankly, I have little idea of what went wrong. I've edited the environment PATH variable, twice, to include the OpenCV build. Maybe I've made a typo the first time. It now looks like this:
C:\MinGW\bin;C:\OpenCV\my_build\install\x86\mingw\bin;C:\Qt\5.4.0\5.4\mingw491_32\bin;
My *.pro file also looks different.
QT += core
QT -= gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = OpenCVTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += C:/OpenCV/qtbuild//install/include
LIBS += "C:/OpenCV/qtbuild/install/x86\mingw/lib/*.a"
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
I've change +=gui to -=gui and added CONFIG -= app_bundle. The INCLUDE and LIBS paths are also different now - I won't be changing them out of an irrational fear something will go horribly wrong again.
And lastly, I've simply removed the previous project and made a new one. Maybe something else got corrupted?
Anyway, it works now, after doing these things.

undefined reference SDL_Init with Qt

I've already gone through questions similar to mine, but none solved my problem.
So I'm trying to use SDL in a Qt Widget (for educational purpose), and I always get and undefined reference on every SDL fonction that I call.
Here is the (minimalist) code I've been using for testing :
#include "mainview.h"
#include <QApplication>
#include "SDL.h"
#undef main
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainView w;
w.show();
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindowFrom((void*)w.centralWidget->winId());
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderFillRect(render, NULL);
SDL_RenderPresent(render);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(render);
SDL_Quit();
return a.exec();
}
And here is th .pro file :
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-08T22:37:55
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MapEditor
TEMPLATE = app
SOURCES += main.cpp\
mainview.cpp
HEADERS += mainview.h
FORMS += mainview.ui
INCLUDEPATH += SDL/include/
LIBS += -L SDL/lib/x86/SDL2.lib
According to other answers i've read, it seems the problem comes from the link to the library.
I have never really used this file, or other qmake before, so I am not sure about the path I am using. I work on windows, and according to this article, I should be writing the full path, but then I get access authorization problems (damn Windows).
Furthermore, changing the path to an false one doesn't seems to upset the compiler.
However, when I try to use
LIBS += -L SDL/lib/x86/ -lSDL2.lib
the compiler cannot find the file.
The compiler I'm using is a MinGW 32bits, so I've downloaded the corresponding file on lbsdb.org, and I've tried every library on very sub-folder there was (x86_64, i686,...) but none worked.
And sorry if the way I'm writing is strange, I'm french.
When you have a .lib file, it's easiest to just add the file for linker. You have it subdirectory of your project it seems, but you are probably using a separate build directory (as you should), so the problem is probably that. To fix, use PWD qmake variable:
LIBS += $${PWD}/SDL/lib/x86/SDL2.lib
You might also want to add PWD to include path, because relative path there is relative to directory of the .c file, and you might not always have them all in the same directory.
In a comment you mention something about not being able to use absolute paths... Not sure what you mean by that, but then a way out is to not use shadow build (which is no problem, as long as you remember to do full rebuild when changing build types). If you are using Qt Creator, you can find the project's build settings for each build type under Projects view, and there you have a check box for shadow build.

SYSTEMTIME in qt

I am a bit stuck at the moment with a little sample project that I would like to run to test some cryptology that I want to use in a main project.
Basically I am using the latest Qt Creator and I have created a simple window dialog. Furthermore, I would like to test the PBKDF2 implementation through CkCrypt2
So what I have done is downloading the X64 version of the library and added it to my project folder. I then told my Qt project to use an external library, the final .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-09T18:09:44
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = crypt2test
TEMPLATE = app
SOURCES += main.cpp\
m
ainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64d
else:unix: LIBS += -L$$PWD/libs/ -lChilkatDbgDll_x64
INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include
I can successfully load the library but I cannot start the application.
My mainwindow.cpp looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "CkCrypt2.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CkCrypt2 crypt;
bool success;
success = crypt.UnlockComponent("Just some random text ");
if ( !success )
{
qDebug() << "Not successfully unlocking the library";
}
}
MainWindow::~MainWindow()
{
delete ui;
}
The error message I get in the compiler is:
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:127: error: C2061: syntax error : identifier 'SYSTEMTIME'
c:\qt\qt5.0.2\tools\qtcreator\bin\crypt2test\include\CkString.h:129: error: C2061: syntax error : identifier 'SYSTEMTIME'
C:\Qt\Qt5.0.2\Tools\QtCreator\bin\crypt2test\include\CkCrypt2.h:429: error: C2061: syntax error : identifier 'SYSTEMTIME'
Looking into the files I see:
void appendDateRfc822(SYSTEMTIME &sysTime);
void appendDateRfc822Gmt(SYSTEMTIME &sysTime);
// GETSIGNATURESIGNINGTIME_BEGIN
bool GetSignatureSigningTime(int index, SYSTEMTIME &outSysTime);
// GETSIGNATURESIGNINGTIME_END
Okay, so it is complaining about the SYSTEMTIME construct. So I look up the error C2061
Basically it says:
The compiler found an identifier where it wasn't expected. Make sure
that identifier is declared before you use it.
Which makes sense, so I look up the SYSTEMTIME and try to do :
#include <windows.h>
But that leads to many more errors in the windows.h file itself.
I use the MS Visual C++ compiler in Qt. Even though I use qmake. I am very new to this and I do not understand it all yet. Furthermore, I have no idea how to fix this, because including the windows.h does not help.
What seems to be the problem here ? Is this an issue regarding my compiler or the constulation that I use a third party app which library is compiled with MS Visual C++ and I am now trying to use this on my Windows machine in Qt with a Windows Visual C++ compiler ?
For any help I am gratefully thankful!
EDIT1:
Actually, after a clean all and qmake and build project I have now different errors and none were found in the Windows.h as previously stated. Since there were so many I have made a screenshot: http://i.imgur.com/B8EoENB.png
EDIT2:
I have adjusted the library that I include. Before this I was using the multi-threaded library of CkCrypt in the Debug mode. I have now included the single realease library. Which is located in the same directory.
When including windows.h I got the errors that things were already defined. So I removed the line again. With this result: http://i.imgur.com/z415txR.png
This shows at the bottom that MSVCRT conflichts with other library. It mentions to use NODEFAULTLIB:library but I am not to sure how to do that. Will google and keep this up to date as I process.
Many years ago, Chilkat was originally developed for the Windows platform only, and used SYSTEMTIME for this reason. In the last 5 years (approx) Chilkat is cross-platform, and SYSTEMTIME no longer makes sense. To cope with the issue, there is a "SystemTime.h" header in the same directory as the CkCrypt2.h header file. You could include this to solve the problem. (However, if WIN32 is defined, you'll probably need to edit SystemTime.h to remove the #ifdef.)
In any case, the methods using SYSTEMTIME are going to be deprecated. For any method or property that uses SYSTEMTIME, there should be a newer alternative method/property that instead uses CkDateTime.
Finally, Chilkat will test with Qt so that for the next version, (hopefully) Qt out-of-the-box will compile without any pitfalls.