37

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?

2
  • What compiler platform are you using ? Make sure you build sqlite.c and include it with your program. +Don't forget to close your connection...
    – ixe013
    Commented Feb 22, 2012 at 5:07
  • undefined reference to `sqlite3_open'
    – CS QGB
    Commented Nov 1, 2022 at 12:53

5 Answers 5

57

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

g++ main.cpp -lsqlite3
3
  • In an Android environment, this caused me to see: fatal error: 'jni.h' file not found
    – Charlie
    Commented Jul 17, 2017 at 9:48
  • /usr/bin/ld: cannot find -lsqlite3
    – CS QGB
    Commented Nov 1, 2022 at 8:16
  • @CSQGB have u installed libsqlite3-dev?
    – user5819540
    Commented Mar 26, 2023 at 10:51
7

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

2
1

First step: Install all library sqlite3 with the command:

        sudo apt-get install libsqlite3-dev

With that you can use #include <sqlite3.h> in a programm of C or C++.

Second step: To compile the program by console:

C++:

        g++ program.cpp -o executable -lsqlite3

        ./executable

C:

        gcc program.c -o executable -lsqlite3

        ./executable
2
  • /usr/bin/c++ -lsqlite3 qgb.cpp undefined reference to `sqlite3_close'
    – CS QGB
    Commented Nov 1, 2022 at 8:20
  • already apt install libsqlite3-dev
    – CS QGB
    Commented Nov 1, 2022 at 8:21
0

Either link your program to lib g++ yourProgram.c -lsqlite3 in command line or in Open IDE -> project -> properties -> locate lib file for sqlite3 .

0

Compile using Devcpp

1. add sqlite3.dll file in the project folder.

2. go to Compiler option in Tools >>

3. write sqlite3.dll next to >> Add the following commands when calling compiler

Screen shot

Compile using command line

NOTE : install MinGW (compiler)

g++ file.cpp -o output.exe sqlite3.dll

Compile using VS

define sqlite3.dll in linker in project properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.