I am new to C++ ad trying to link to a sqlite database. I am following a tutorial and I am getting a sqlite3_open error Undefined Reference. What am I doing wrong?
#include "database.h"
#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>
database::database()
{
}
int database::test()
{
int retval;
// A prepered statement for fetching tables
sqlite3_stmt *stmt;
// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;
// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
retval = sqlite3_open("CC.sqlite",&handle);
// If connection failed, handle returns NULL
if(retval)
{
printf("Database connection failed\n");
return -1;
}
printf("Connection successful\n");
return 1;
}
.pro File
#-------------------------------------------------
#
# Project created by QtCreator 2013-05-31T09:22:09
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CC_Herd_Manager
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
animal.cpp \
email.cpp \
owner.cpp \
phone.cpp \
equipment.cpp \
../CC_Cpp/cc_number.cpp \
../CC_Cpp/cc_date.cpp \
../CC_Cpp/cc_timestamp.cpp \
../CC_Cpp/cc_address.cpp \
database.cpp
HEADERS += mainwindow.h \
animal.h \
email.h \
owner.h \
phone.h \
equipment.h\
../CC_Cpp/cc_number.h \
../Cpp/CC_Cpp/cc_date.h \
../Cpp/CC_Cpp/cc_timestamp.h \
../Cpp/CC_Cpp/cc_address.h \
database.h
FORMS += mainwindow.ui
OTHER_FILES += \
DB_Install
Error
/home/mongo/Cpp/CC_Herd_Manager/database.o:-1: In function `database::test()':
/home/mongo/Cpp/CC_Herd_Manager/database.cpp:23: error: undefined reference to `sqlite3_open'
:-1: error: collect2: error: ld returned 1 exit status
If you want to link agains your system's sqlite version (which is probably best), you have to add the following line to your .pro-file:
LIBS += -lsqlite3
and that should be it.
You could also download the sqlite sources (the "amalgamation", as they call it) and add the .c file to your project. If you do that, you should use your downloaded Header file as well. But because you use your system's header file in your code, I assume that you want to use the system's library version, which I recommend.