假设你已经正确编译和安装了Sqlite,写个测试程序来测试:
#include <stdlib.h>
#include <stdio.h>
#include "sqlite3.h"
int main(void)
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc=sqlite3_open("test1.db",&db);
if(rc)
{
fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("open mydata successfully!\n");
sqlite3_close(db);
return 0;
}
用GCC来编译的时候总是会出现错误,编译的命令如下
gcc -static -o hello -lsqlite3 -L /usr/local/lib -I/usr/local/include hello.c
错误信息如下
/tmp/ccKeKpX9.o(.text+0x37): In function `main':
: undefined reference to `sqlite3_open'
/tmp/ccKeKpX9.o(.text+0x51): In function `main':
: undefined reference to `sqlite3_errmsg'
/tmp/ccKeKpX9.o(.text+0x73): In function `main':
: undefined reference to `sqlite3_close'
/tmp/ccKeKpX9.o(.text+0x9b): In function `main':
: undefined reference to `sqlite3_close'
collect2: ld returned 1 exit status
那么,恭喜你中招了。错误根本不在SQLITE也不在你的程序,而在GCC。Gcc的编译参数是有顺序的。正确的编译命令是:
gcc -o hello -L /usr/local/lib -I/usr/local/include -static hello.c -lsqlite3
说实话,这么的一个小问题困扰了我一天的时间!真是菜啊~~~~~~