1、首先从SQLite Download Page网站下载最新sqlite3源码,
2、选择sqlite3.c和sqlite3.h,sqlite3ext.h加载到工程中。
3、编写测试main.c函数:
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
int main()
{
sqlite3 *db;
int nResult = sqlite3_open("test.db", &db);
if(nResult == SQLITE_OK)
{
printf("open success\n");
}
else
{
printf("open fail\n");
}
return 0;
}
4、编写命令行命令:gcc -o main main.c sqlite3.c -lpthread -ldl -lm
如果不加上-lpthread -ldl -lm,则会报错,报错信息如下:
/usr/bin/ld: sqlite3.o: in function `pthreadMutexAlloc':
sqlite3.c:(.text+0x436e): undefined reference to `pthread_mutexattr_init'
/usr/bin/ld: sqlite3.c:(.text+0x437f): undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: sqlite3.c:(.text+0x439e): undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: sqlite3.o: in function `pthreadMutexTry':
sqlite3.c:(.text+0x4473): undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: sqlite3.o: in function `sqlite3ThreadCreate':
sqlite3.c:(.text+0x93ec): undefined reference to `pthread_create'
/usr/bin/ld: sqlite3.o: in function `sqlite3ThreadJoin':
sqlite3.c:(.text+0x9482): undefined reference to `pthread_join'
/usr/bin/ld: sqlite3.o: in function `unixDlOpen':
sqlite3.c:(.text+0x125a9): undefined reference to `dlopen'
/usr/bin/ld: sqlite3.o: in function `unixDlError':
sqlite3.c:(.text+0x125cc): undefined reference to `dlerror'
/usr/bin/ld: sqlite3.o: in function `unixDlSym':
sqlite3.c:(.text+0x1261f): undefined reference to `dlsym'
/usr/bin/ld: sqlite3.o: in function `unixDlClose':
sqlite3.c:(.text+0x12659): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: main] Error 1