这篇虽然很老了,但是后面的调试很详细,也许对你很有帮助:
虽然网上有很多高手对 sqlite 数据库的ARM-Linux移置进行过详细的介绍,但最近发现很多朋友还在问这个问题。
虽然我对 sqlite 的了解说不上透彻,但是写点东西让初学者能够很快上手,特别是能够让刚接触sqlite的朋友也能轻松完成移置工作,
也算是我的心愿之一。也许很多高手会觉得我这是班门弄斧,但是我还是想写点关于 sqlite 的基本知识,这样也许能给予某些人一些帮助,希望如此。
1.1 sqlite简介
(跳过)
1.2 下载代码
(跳过)
1.3 交叉编译sqlite库
准备工作:
1. 取得一个 arm-linux-gcc 的编译器。
2. sqlite的源码 sqlite-3.0.8.tar.gz (本文以)
好了,我们可以开始了。
这里设 arm-linux-gcc 在 /usr/local/arm-linux/bin/ 目录下
解压sqlite-3.0.8.tar.gz 到/usr/local/arm-linux/sqlite
# tar zxvf sqlite-3.0.8.tar.gz -C /usr/local/arm-linux/sqlite
# cd /usr/local/arm-linux/sqlite
将目录下的 configure 文件的
19206行 { (exit 1); exit 1; }; }改为 { (echo 1); echo 1; }; }
20107行 { (exit 1); exit 1; }; }改为 { (echo 1); echo 1; }; }
20514行 { (exit 1); exit 1; }; }改为 { (echo 1); echo 1; }; }
20540行 { (exit 1); exit 1; }; }改为 { (echo 1); echo 1; }; }
# mkdir /usr/local/arm-linux/sqlite-arm-linux
# cd /usr/local/arm-linux/sqlite-arm-linux
设置交叉编译环境
# export PATH=/usr/local/arm-linux/bin:$PATH
配置:
# ../sqlite/configure --host=arm-linux --prefix=/usr/local/arm-linux/sqlite-arm-linux
注意:这里不用写成了 "../sqlite/configure --host=arm-linux --prefix=/usr/local/arm-linux/sqlite-arm-linux/"
这最后一个斜杠"/"不要带上了。
如果一切顺利的话,会在/usr/local/arm-linux/sqlite-arm-linux目录下生成一些相关文件:
config.log config.status libtool Makefile sqlite3.pc
改/usr/local/arm-linux/sqlite-arm-linux/Makefile文件的 BCC = arm-linux-gcc -g -O2 为 BCC = gcc -g -O2
编译安装:
# make
# make install
/usr/bin/install -c -d /usr/local/arm-linux/sqlite-arm-linux/lib
./libtool --mode=install /usr/bin/install -c libsqlite3.la /usr/local/arm-linux/sqlite-arm-linux/lib
/usr/bin/install -c .libs/libsqlite3.so.0.8.6 /usr/local/arm-linux/sqlite-arm-linux/lib/libsqlite3.so.0.8.6
(cd /usr/local/arm-linux/sqlite-arm-linux/lib && rm -f libsqlite3.so.0 && ln -s libsqlite3.so.0.8.6 libsqlite3.so.0)
(cd /usr/local/arm-linux/sqlite-arm-linux/lib && rm -f libsqlite3.so && ln -s libsqlite3.so.0.8.6 libsqlite3.so)
/usr/bin/install -c .libs/libsqlite3.lai /usr/local/arm-linux/sqlite-arm-linux/lib/libsqlite3.la
/usr/bin/install -c .libs/libsqlite3.a /usr/local/arm-linux/sqlite-arm-linux/lib/libsqlite3.a
arm-linux-ranlib /usr/local/arm-linux/sqlite-arm-linux/lib/libsqlite3.a
chmod 644 /usr/local/arm-linux/sqlite-arm-linux/lib/libsqlite3.a
PATH="$PATH:/sbin" ldconfig -n /usr/local/arm-linux/sqlite-arm-linux/lib
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/arm-linux/sqlite-arm-linux/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
2 sqlite3嵌入式数据库的ARM-Linux移置详解
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
/usr/bin/install -c -d /usr/local/arm-linux/sqlite-arm-linux/bin
./libtool --mode=install /usr/bin/install -c sqlite3 /usr/local/arm-linux/sqlite-arm-linux/bin
/usr/bin/install -c .libs/sqlite3 /usr/local/arm-linux/sqlite-arm-linux/bin/sqlite3
/usr/bin/install -c -d /usr/local/arm-linux/sqlite-arm-linux/include
/usr/bin/install -c -m 0644 sqlite3.h /usr/local/arm-linux/sqlite-arm-linux/include
/usr/bin/install -c -d /usr/local/arm-linux/sqlite-arm-linux/lib/pkgconfig;
/usr/bin/install -c -m 0644 sqlite3.pc /usr/local/arm-linux/sqlite-arm-linux/lib/pkgconfig;
注明: "# make install" 这一步将会在 /usr/local/arm-linux/sqlite-arm-linux/lib 生成库文件
# cd lib
# file libsqlite3.so.0.8.6
libsqlite3.so.0.8.6: ELF 32-bit LSB shared object, ARM, version 1 (ARM), not stripped
此时生成的sqlite文件是还未strip过的,你可以使用命令“file sqlite”查看文件信息。用strip处理过后,将去掉其中的调试信息,执行文
件大小也将小很多。命令如下:
# arm-linux-strip libsqlite3.so.0.8.6
另外可以生成帮助文档,以便以后查询。
# make doc
好了,这样我们已经编译出了
1.4 在ARM板上运行sqlite
将/usr/local/arm-linux/sqlite-arm-linux/bin/目录下的 sqlite3 文件下载到你的arm板上,
方法很多,你需要根据自己的情况来选择。如ftp,nfs,串口等。
好,开始运行
chmod +wx sqlite
[root@51Board var]# ./sqlite3 zieckey.db
./sqlite3: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file
or directory
这里是因为刚刚编译时编译的是动态连接库形式的,所有我们还的将库文件下载到ARM板上。
将 /usr/local/arm-linux/sqlite-arm-linux/lib 目录下所有文件下到ARM板上。
再次运行,
[root@51Board var]# ./sqlite3 zieckey.db
./sqlite3: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file
or directory
还是出错,哦,我们没有设置环境变量,
假设我们下在库文件在ARM板上的 /usr/qpe/lib/ 目录下,这里设置环境就像下面:
[root@51Board var]# export LD_LIBRARY_PATH=/usr/qpe/lib:$LD_LIBRARY_PATH
好了这样就可以运行了:
[root@51Board var]# ./sqlite3 zieckey.db
SQLite version 3.0.8
Enter ".help" for instructions
sqlite>
看见
sqlite>
提示符号没有?成功了。哈哈!!
打个“.help”来看看命令先:-)
sqlite>.help
好了。现在sqlite已经在arm-linux下跑了起来。如何,感觉很high吧。
1.5 编写一个程序到ARM板上运行
现在我们来写个C/C++程序,调用 sqlite 的 API 接口函数查询数据库中表的内容。并在ARM板上运行。
下面是一个C程序的例子,显示怎么使用 sqlite 的 C/C++ 接口. 这个函数调用sqlite3_open() 在 22 行打开数据库, sqlite3_get_table 在 40 行执行 SQL 命令, 并且sqlite3_close() 在 47 行关闭数据库连接。
代码:
// name: test_sqlite.c
// This file is used to test C/C++ API for sqlite
3 sqlite3嵌入式数据库的ARM-Linux移置详解
// Author : zieckey
// 2006/06/28
#include <stdio.h>
#include <sqlite3.h>
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("open zieckey.db successfully!\n");
sqlite3_close(db); //关闭数据库
return 0;
}
编译:# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out
也许会碰到类似这样的问题:
# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out
test_sqlite.c:7:21: sqlite3.h: No such file or directory
test_sqlite.c: In function `main':
test_sqlite.c:11: error: `sqlite3' undeclared (first use in this function)
test_sqlite.c:11: error: (Each undeclared identifier is reported only once
test_sqlite.c:11: error: for each function it appears in.)
test_sqlite.c:11: error: `db' undeclared (first use in this function)
这是个没有找到库文件的问题。
由于用到了用户自己的库文件,所用应该指明所用到的库,我们可以这样编译:
# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out -lsqlite3
我用用 -lsqlite3 选项就可以了(前面我们生成的库文件是 libsqlite3.so.0.8.6 等,
去掉前面的lib和后面的版本标志,就剩下 sqlite3 了所以是 -lsqlite3 )。
不过似乎还是可能出现问题,例如下面的这样:
]# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out -lsqlite3
test_sqlite.c:7:21: sqlite3.h: No such file or directory
test_sqlite.c: In function `main':
test_sqlite.c:11: error: `sqlite3' undeclared (first use in this function)
test_sqlite.c:11: error: (Each undeclared identifier is reported only once
test_sqlite.c:11: error: for each function it appears in.)
test_sqlite.c:11: error: `db' undeclared (first use in this function)
我们刚才在编译安装的时候,选择了安装路径是 /usr/local/arm-linux/sqlite-arm-linux
这样编译安装时,sqlite的库文件将会生成在 /usr/local/arm-linux/sqlite-ix86-linux/lib 目录下
这时编译还要指定库文件路径,因为系统默认的路径没有包含 /usr/local/arm-linux/sqlite-arm-linux/lib
# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out -lsqlite3 -lsqlite3 -L/usr/local/arm-linux/sqlite-arm-linux/lib
如果还不行的话,可能还需要指定头文件 sqlite3.h 的路径,如下:
# arm-linux-gcc test_sqlite.c -o test_sqlite_for_arm.out -lsqlite3 -L/usr/local/arm-linux/sqlite-arm-linux/lib -I/usr/local/arm-linux/sqlite-arm-linux/include
现在可以看看生成的文件格式:
# file test_sqlite_for_arm.out
test_sqlite_for_arm.out: ELF 32-bit LSB executable, ARM, version 1 (ARM), for GNU/Linux 2.0.0, dynamically linked (uses shared libs), not stripped
这说明已经初步成功了。
下面就把 test_sqlite_for_arm.out 文件下载到ARM板上运行了(下载过程不用说了,如串口、ftp、nfs等待都可以)
运行:
[root@51Board var]# ./test_sqlite_for_arm.out
./test_sqlite_for_arm.out: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file or directory
运行是也许会出现类似上面的错误。
这个问题是由于运行沿着系统默认路径没有找到库文件引起的。这时需要指定 libsqlite3.so.0 库文件的路径。
在shell下输入:
[root@51Board var]# export LD_LIBRARY_PATH=/usr/qpe/lib:$LD_LIBRARY_PATH
再运行
# test_sqlite_for_arm.out
open zieckey.db successfully!
总结:这里,我们了解了sqlite的移置基本过程,怎么在C/C++中查询数据库中表的内容。
本文一定存在很多不妥或者错误之处,还劳烦各位不吝赐教指正。
