Program-Me-Rev
Program-Me-Rev

Reputation: 6634

How to embed SQLite into a C++ project

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

Upvotes: 3

Views: 2478

Answers (2)

Mohammad Kanan
Mohammad Kanan

Reputation: 4602

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.

Upvotes: 1

Emerald Weapon
Emerald Weapon

Reputation: 2540

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.

Upvotes: 5

Related Questions