-->

Error: undefined reference to `sqlite3_open'

2019-01-22 13:27发布

问题:

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?

回答1:

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

g++ main.cpp -lsqlite3


回答2:

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