Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: undefined reference to `sqlite3_open'

Tags:

c++

sqlite

I'm trying to get started with the C++ API for SQLite.

#include <iostream>
#include <sqlite3.h>

using namespace std;

int main()
{
    sqlite3 *db;
    if (sqlite3_open("ex1.db", &db) == SQLITE_OK)
        cout << "Opened db successfully\n";
    else
        cout << "Failed to open db\n";

    return 0;
}   

Compiling this using the command "g++ main.cpp" gives the following error:

/tmp/ccu8sv4b.o: In function `main':
main.cpp:(.text+0x64): undefined reference to `sqlite3_open'
collect2: ld returned 1 exit status

What could have gone wrong? Hasn't sqlite3 properly installed in the server I'm compiling this in?

like image 431
thameera Avatar asked Feb 22 '12 05:02

thameera


2 Answers

You need to link the sqlite3 library along with your program:

g++ main.cpp -lsqlite3
like image 142
casablanca Avatar answered Nov 06 '22 21:11

casablanca


You need to adjust your linker flags to link in the sqlite3 library. Libraries are usually installed in /usr/lib or /usr/lib64

Alternatively, you can copy the sqlite3.c file to your project directory and compile it as part of the g++ command:

g++ main.cpp sqlite3.c 

as per: http://sqlite.org/cvstrac/wiki?p=HowToCompile

like image 5
fileoffset Avatar answered Nov 06 '22 20:11

fileoffset