SQLITE3在tiny6410上的移植

时间:2021-04-13 12:40:15

下载sqlite最新版本3.7.9 http://www.sqlite.org/download.html 

源码包: sqlite-autoconf-3071400.tar.gz 

解压源码包到/opt目录下:

      #tar xvzf sqlite-autoconf-3071400.tar.gz –C /opt/ 

建立make install目录:

     # mkdir /opt/build   

进入解压出得文件夹中:

     # cd /opt/sqlite-autoconf-3070900 

执行configure命令:生成Makefile文件:

     # ./configure –-host=arm-linux –prefix=/opt/build    

生成Makefile文件后,执行make命令::
     # make #make install 

    

    完成之后可以发现,之前所建目录/opt/build下生成bin include lib share四个目录。主要用到的文件有./bin/sqlite3  ./include/sqlite3.h以及  ./lib/下的库文件。 

    bin文件夹下的sqlite3是sqlite可执行应用程序下载到板子Linux系统下的/bin目录或者/usr/bin目录下并添加文件可执行权限。在板子Linux系统命令行下执行:# chmod +x sqlite3 

    进入sqlite命令行,可以实现对数据库的管理。

 [root@FriendlyARM plg]# sqlite3 

SQLite version 3.7.9 2011-11-01 00:52:41 

Enter ".help" for instructions 

Enter SQL statements terminated with a ";" 

sqlite> 

 ./include/sqlite3.h定义了sqlite的API接口会在有关sqlite的c文件交叉编译时用到

 ./lib/文件夹下是有关sqlite的静态链接库和动态链接库  

     # ls lib/ libsqlite3.a  libsqlite3.la  libsqlite3.so  libsqlite3.so.0  libsqlite3.so.0.8.6  pkgconfig  

其中libsqlite3.so和libsqlite3.so.0都是libsqlite3.so.0.8.6的软链接文件。真正需要下载到板子目录/lib下的动态库是libsqlite3.so.0.8.6。下载到板子后还需对它建立软链接文件因为可执行程序寻找的动态链接库的名字为libsqlite3.so.0。

    # ln -s /lib/libsqlite3.so.0.8.6  /lib/libsqlite3.so.0  

到此,sqlite的移植工作已经完成,下面进行测试。

 测试程序test_sqlite.c如下:

1. #include <stdio.h> 

2. #include <sqlite3.h> 

3.   

4. static int callback(void *NotUsed, int argc, char **argv, char **azColName) 

5. { 

6.     int i; 

7.     for(i=0; i<argc; i++) 

8.     { 

9.     printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 

10.     } 

11.     printf("\n"); 

12.     return 0; 

13. } 

14.   

15. int main(int argc, char **argv) 

16. { 

17.     sqlite3 *db; 

18.     char *zErrMsg = 0; 

19.     int rc; 

20.     if( argc!=3 ) 

21.     { 

22.     fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); 

23.     } 

24.     rc = sqlite3_open(argv[1], &db); 

25.     if( rc ) 

26.     { 

27.    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 

28.         sqlite3_close(db); 

29.     } 

30.     rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); 

31.     if( rc!=SQLITE_OK ) 

32.     { 

33.         fprintf(stderr, "SQL error: %s\n", zErrMsg); 

34.     } 

35.     sqlite3_close(db); 

36.     return 0; 

37. }

动态编译: # arm-linux-gcc -o test test_sqlite.c -I /opt/build/include/ -L /opt/build/lib/ -lsqlite3 

注:-I制定优先搜索头文件路径:

       -L制定编译的时候,优先搜索库的路径。 

下载到板子上执行:

    [root@FriendlyARM plg]#./test xyz.db "create table tbl0(name varchar(10), number smallint);" 

    [root@FriendlyARM plg]#./test xyz.db "insert into tbl0 values('cyc',1);" 

    [root@FriendlyARM plg]#./test xyz.db "insert into tbl0 values('dzy',2);" 

    [root@FriendlyARM plg]#./test xyz.db "select * from tbl0;" 

    name = cyc 

    number = 1 

    name = dzy 

    number = 2 

    测试成功!

    静态编译:

    # arm-linux-gcc -o test test_sqlite.c -I /opt/build/include/ -L  /opt/build/lib/ -static -lsqlite3 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlSym': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:29926: undefined reference to `dlsym' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexLeave': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17807: undefined reference to `pthread_mutex_unlock' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexTry': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17769: undefined reference to `pthread_mutex_trylock' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexEnter': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17723: undefined reference to `pthread_mutex_lock' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexFree': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17680: undefined reference to `pthread_mutex_destroy' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `pthreadMutexAlloc': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17654: undefined reference to `pthread_mutex_init' 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17637: undefined reference to `pthread_mutexattr_init' 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17638: undefined reference to `pthread_mutexattr_settype' 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17639: undefined reference to `pthread_mutex_init' 

    /opt/sqlite-autoconf-3070900/sqlite3.c:17640: undefined reference to `pthread_mutexattr_destroy' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlClose': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:29930: undefined reference to `dlclose' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlError': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:29899: undefined reference to `dlerror' 

    /opt/build/lib//libsqlite3.a(sqlite3.o): In function `unixDlOpen': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:29885: undefined reference to `dlopen' collect2: ld returned 1 exit status  

错误信息中提示有线程接口函数,需要线程的动态链接库,要加入-lpthread选项。在网上查找按照网友提供的资料dlopen(),dlclose(),dlerror(),dlsym()函数在头文件#include <dlfcn.h>中,同样需要添加编译选项-ldl。 

    # arm-linux-gcc -o test test.c -I /opt/build/include/ -L /opt/build/lib  -static -lsqlite3 -lpthread –ldl  

编译成功:但有一个warning,没有解决,暂时没有发现这个警告对程序影响。 

    /opt/build/lib/libsqlite3.a(sqlite3.o): In function `unixDlOpen': 

    /opt/sqlite-autoconf-3070900/sqlite3.c:29885: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking 

静态编译与动态编译的差别在于:可执行文件较大,即使板子上没有移植动态链接库libsqlite3.so.0,程序也同样可以执行。  

原文网址: http://blog.chinaunix.net/space.php?uid=26310563&do=blog&id=3023475