Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems linking to sqlite3.h with gcc

Tags:

c

sqlite

gcc

linker

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?

like image 732
AppFzx Avatar asked Sep 13 '13 01:09

AppFzx


3 Answers

This works

gcc ./sqliteTest.c -o sqliteTest -lsqlite3
like image 178
Abhay Nayak Avatar answered Oct 23 '22 05:10

Abhay Nayak


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
like image 33
Siyuan Ren Avatar answered Oct 23 '22 04:10

Siyuan Ren


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.

like image 44
This isn't my real name Avatar answered Oct 23 '22 04:10

This isn't my real name