Problems linking to sqlite3.h with gcc
Solution 1
SQLite is a source only library. You embed the source into the application, not linking with it. So the undefined reference comes from the fact that you fail to include the source file of sqlite. Try compiling as
gcc -O3 sqliteTest.c sqlite3.c -o sqliteTest -lpthread -ldl
Solution 2
This works
gcc ./sqliteTest.c -o sqliteTest -lsqlite3
Solution 3
Well, first you should #include <stdlib.h>
to have an appropriate declaration of exit()
in scope, and second you should remember that what you're trying to link with is named "sqlite3", and replace the -lsqlite
in your link line with -lsqlite3
.
AppFzx
Physics-engineer trapped (by student loans) in a software-engineer's body.
Updated on November 03, 2020Comments
-
AppFzx over 3 years
I'm working on Linux Mint 15. I've downloaded
sqlite-amalgamation-3080002.zip
from http://www.sqlite.org/download.html (and put the files in my project directory)I've done (even though I know it's redundant to the previous step):
sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev
sqlite3
works at command line just fine and I can create/edit databases.I created a test file:
#include <stdio.h> #include <sqlite3.h> int main(int argc, char* argv[]){ sqlite3 *db; char *zErrMsg = 0; int rc; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "Opened database successfully\n"); } sqlite3_close(db); }
and ran:
gcc ./sqliteTest.c -o sqliteTest -lsqlite
and got the following error:
./sqliteTest.c: In function ‘main’: ./sqliteTest.c:14:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] /usr/bin/ld: cannot find -lsqlite collect2: error: ld returned 1 exit status
I tried:
gcc -Wall sqliteTest.c -o sqliteTest -lsqlite
and got:
sqliteTest.c: In function ‘main’: sqliteTest.c:14:7: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration] sqliteTest.c:14:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] sqliteTest.c:7:10: warning: unused variable ‘zErrMsg’ [-Wunused-variable] sqliteTest.c:19:1: warning: control reaches end of non-void function [-Wreturn-type] /usr/bin/ld: cannot find -lsqlite collect2: error: ld returned 1 exit status
I changed the
<sqlite3.h>
to"sqlite3.h"
and did the first compile command and got:./sqliteTest.c: In function ‘main’: ./sqliteTest.c:14:7: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default] /tmp/ccvdOOv2.o: In function `main': sqliteTest.c:(.text+0x24): undefined reference to `sqlite3_open' sqliteTest.c:(.text+0x39): undefined reference to `sqlite3_errmsg' sqliteTest.c:(.text+0x89): undefined reference to `sqlite3_close' collect2: error: ld returned 1 exit status
I'm stumped... What do I try next?
-
This isn't my real name over 10 yearsSQLite is a source-only library from the producer. The OS vendor, however, provides a precompiled version. The package named "libsqlite3-dev" puts
sqlite3.h
into /usr/include, and putslibqslite3.so
into a directory where GCC will find it. Since the questioner does#include <sqlite3.h>
in his code, not#include "sqlite3.h"
, he's trying to use the system version, and the fact that he also downloaded the source code is an ignorable red herring. Alternatively, he should do it the way you recommend, but then he should also#include "sqlite3.h"
instead of#include <sqlite3.h>
. -
Siyuan Ren over 10 years@ElchononEdelson: The questioner tried changing to
#include "sqlite3.h"
in the last part of his question. But it is interesting to know that Mint provides a precompiled version. -
This isn't my real name over 10 yearsAh, I missed that. Good point. Yes, as long as he's doing
"sqlite.h"
, he should be adding sqlite3.c to his compilation and not linking with-lsqlite3
, just as you said. Also, I think pretty much every Linux provides precompiles of this library (and tons of others). In fact, major distributions will prefer to provide their own builds of everything even if the maker of the product provides builds, because that way they can customize it to align with their OS layout and philosophy. (And their own bugfixes and things like that...) -
AppFzx over 10 yearsAnd this is why we don't code while falling asleep. Good catch. For curiosity sake though: Why didn't it work when I included via
#include "sqlite3.h"
? -
AppFzx over 10 years@ElchononEdelson Actually, the order I attempted was first to download
sqlite-amalgamation-3080002.zip
as this usually gives a better performance. That didn't work and compiling with a typo (sqlite
vssqlite3
) didn't work and sqlite3 at command prompt didn't work so I assumed Mint does NOT in fact come with sqlite. Hence I did apt-get. -
AppFzx over 10 yearsGah, wish I could accept both these answers. Missing
3
was an obvious dumb mistake. <stdlib.h> is useful as well.