8

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

我已经完成了(即使我知道这对上一步来说是多余的):

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

4 回答 4

9

SQLite 是一个仅限源代码的库。您将源嵌入到应用程序中,而不是与其链接。所以未定义的引用来自于你没有包含sqlite的源文件。尝试编译为

gcc -O3 sqliteTest.c sqlite3.c -o sqliteTest -lpthread -ldl
于 2013-09-13T02:22:45.750 回答
9

这有效

gcc ./sqliteTest.c -o sqliteTest -lsqlite3
于 2017-11-02T14:43:22.353 回答
5

好吧,首先您应该#include <stdlib.h>有一个适当的exit()in 范围声明,其次您应该记住您要链接的内容名为“sqlite3”,并将-lsqlite链接行中的-lsqlite3.

于 2013-09-13T02:04:54.860 回答
0

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

于 2017-02-09T04:20:11.860 回答