C++ – Sqlite undefined reference to `sqlite3_open’ error in Netbeans C++ on Ubuntu, Integrating SQLite into Netbeans C++ Ubuntu

c++netbeanssqliteubuntu

I am trying to use Sqlite in my Netbeans C++ and following the tutorial here
http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/

When i build it,its giving me undefined reference to `sqlite3_open' and the other sqlite3 functions. I included the sqlite3.h too. What am i missing here?

When i added the linker to static library libsqlite3.a in project properties, my undefined reference errors are gone but now its giving me

/usr/lib/libsqlite3.a(sqlite3.o): In function pthreadMutexTry':
/usr/lib/libsqlite3.a(sqlite3.o): In function
pthreadMutexAlloc':
/usr/lib/libsqlite3.a(sqlite3.o): In function pthreadMutexAlloc':
/usr/lib/libsqlite3.a(sqlite3.o): In function
pthreadMutexAlloc':
/usr/lib/libsqlite3.a(sqlite3.o): In function unixDlError':
/usr/lib/libsqlite3.a(sqlite3.o): In function
findLockInfo':
/usr/lib/libsqlite3.a(sqlite3.o): In function findLockInfo':
/usr/lib/libsqlite3.a(sqlite3.o): In function
unixDlSym':
/usr/lib/libsqlite3.a(sqlite3.o): In function unixDlClose':
/usr/lib/libsqlite3.a(sqlite3.o): In function
unixDlOpen':
collect2: ld returned 1 exit status
make[2]: * [dist/Debug/GNU-Linux-x86/cddb] Error 1
make[1]: *
[.build-conf] Error 2
make: *** [.build-impl] Error 2

And if i include the sqlite3ext.h, I get

main.cpp:20: error: ‘sqlite3_api’ was not declared in this scope

Best Solution

(please see EDIT below)

Not sure how C++ works in NetBeans, but your adding libsqlite3.a looks pretty good. Now if you have a Makefile you could edit it and define (or edit) the LDFLAGS variable, and pass it as an option to the linker...

SQLite in its default configuration needs to link with libdl and libpthread, that is why you might need to add -ldl -lpthread to your linking options.

For instance (if this is possible in NetBeans) add this to your Makefile :

LDFLAGS= -ldl -lpthread

In my projects I use it like so :

target: $(OBJ)
    gcc $(LDFLAGS)  $(OBJ) -o $@

EDIT :

Actually it is also possible to add linker options in the GUI, without editing the Makefile by hand :

In submenu Configuration Properties -> Linker -> Command Line, just add -ldl -lpthread in "Additional options" and recompile your project.

Related Question