Problems with sqlite3.h with gcc

I am working on Linux Mint 15. I downloaded sqlite-amalgamation-3080002.zip from http://www.sqlite.org/download.html (and put the files in the project directory)

I did (although I know that it is redundant in the previous step):

 sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev 

sqlite3 works fine on the command line, 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 received:

 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 <sqlite3.h> to "sqlite3.h" and "sqlite3.h" first compilation 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 at a dead end ... What should I do next?

+6
source share
4 answers

SQLite is just the source library. You embed the source in the application, rather than contacting it. Thus, the undefined link assumes that you are not including the sqlite source file. Try compiling as

 gcc -O3 sqliteTest.c sqlite3.c -o sqliteTest -lpthread -ldl 
+7
source

Well, first you must #include <stdlib.h> have the corresponding exit() declaration in scope, and secondly, you must remember that what you are trying to link is called "sqlite3" and replaces -lsqlite in your line links with -lsqlite3 .

+4
source

It works

 gcc ./sqliteTest.c -o sqliteTest -lsqlite3 
+1
source

CodeLite: right-click on the project> settings> linker: 1. Library search path: / usr / include (path where sqlite3.h); 2. Libraries: sqlite3 3. click OK. and F7 to restore the project

0
source

Source: https://habr.com/ru/post/953744/


All Articles