链接到sqlite3.h与gcc的问题

时间:2013-09-13 01:26:07

标签: c gcc sqlite linker

我正在使用Linux Mint 15.我已经从http://www.sqlite.org/download.html下载了sqlite-amalgamation-3080002.zip(并将文件放在我的项目目录中)

我已经完成了(尽管我知道它对上一步是多余的):

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

sqlite3在命令行工作正常,我可以创建/编辑数据库。

我创建了一个测试文件:

#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);
}

并跑了:

gcc ./sqliteTest.c -o sqliteTest -lsqlite

并收到以下错误:

./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

我试过了:

gcc -Wall sqliteTest.c -o sqliteTest -lsqlite

得到了:

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

我将<sqlite3.h>更改为"sqlite3.h"并执行了第一个编译命令并获得了:

./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

我很难过......接下来我该尝试什么?

4 个答案:

答案 0 :(得分:8)

SQLite是一个仅限源的库。您将源嵌入到应用程序中,而不是与其链接。因此,未定义的引用来自于您未能包含sqlite的源文件这一事实。尝试编译为

gcc -O3 sqliteTest.c sqlite3.c -o sqliteTest -lpthread -ldl

答案 1 :(得分:4)

好吧,首先你要#include <stdlib.h>在范围内有适当的exit()声明,其次你应该记住你想要链接的内容被命名为“sqlite3”,并取代您与-lsqlite的链接行中的-lsqlite3

答案 2 :(得分:4)

这有效

gcc ./sqliteTest.c -o sqliteTest -lsqlite3

答案 3 :(得分:0)

CodeLite: 右键单击项目&gt;设置&gt;链接器:   1.库搜索路径:/ usr / include(路径所在的sqlite3.h);   2.库:sqlite3   3.按OK 和F7重建项目