sqlite嵌入式数据库在arm-linux下的编译全攻略

来源:互联网 发布:网络黑客技术 编辑:程序博客网 时间:2024/06/05 05:26
导读:
  sqlite嵌入式数据库在arm-linux下的编译全攻略 [原创] 2004-06-02
  作者:余涛(yut616_at_sohu.com)
  第一步 sqlite在arm-linux下的编译
  1、 下载sqlite:请到http://www.sqlite.org/download.html,将下载的代码包解开,将生成sqlite目录,另外新建一个build目录,如sqlite-arm-linux,应该是和sqlite目录平行的同级目录。
  2、 请先确定你的PATH中已经包含交叉编译工具arm-linux-gcc。可用“echo $PATH”命令查看。如我的是“/opt/toolchain/gcc 3.2/toolchain/bin/”
  3、 为了在arm-linux下能正常运行sqlite,我们需要修改一处代码,否则在arm板上运行sqlite时会出现下面的东东:
  ===============================
  在文件btree.c中抛出断言,
  assert( sizeof(ptr)==sizeof(char*) );
  ===============================
  此断言是为了保证btree(B树)有正确的变量大小,如“ptr”和“char*”。 在不同的体系结构的linux,如x86和arm,会有些差别。刚好让我们在arm-linux下遇到了:-)。那么我们可以做一定的修改。
  请修改sqlite/src/sqliteInt.h,找到如下部分:
  #ifndef INTPTR_TYPE
  # if SQLITE_PTR_SZ==4
  # define INTPTR_TYPE int
  # else
  # define INTPTR_TYPE long long
  # endif
  在上面的代码前加上一句:
  #define SQLITE_PTR_SZ 4
  这样后面的“typedef INTPTR_TYPE ptr;”就是定义的“int”类型,而不是“long long”。
  4、 准备使用configure进行一些配置。请在sqlite目录下的configure中找到如下4处,并将他们注释掉,这样可以让configure不 去检查你的交叉编译环境。在此提示一下:请你自己确定自己的“arm-linux-”系列命令在你的PATH环境变量中。如:你可以输入“arm- linux-”再按“TAB”键,看其是否自动完成命令行。
  #if test "$cross_compiling" = "yes"; then
  # { { echo "$as_me:12710: error: unable to find a compiler for building build tools" >&5
  #echo "$as_me: error: unable to find a compiler for building build tools" >&2;}
  # { (exit 1); exit 1; }; }
  #fi
  . . .
  #else
  # test "$cross_compiling" = yes &&
  # { { echo "$as_me:13264: error: cannot check for file existence when cross compiling" >&5
  #echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
  # { (exit 1); exit 1; }; }
  . . .
  #else
  # test "$cross_compiling" = yes &&
  # { { echo "$as_me:13464: error: cannot check for file existence when cross compiling" >&5
  #echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
  # { (exit 1); exit 1; }; }
  . . .
  #else
  # test "$cross_compiling" = yes &&
  # { { echo "$as_me:13490: error: cannot check for file existence when cross compiling" >&5
  #echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
  # { (exit 1); exit 1; }; }
  注释掉后,就可以执行configure了。在sqlite-arm-linux目录下,输入如下命令:
  ../sqlite/configure --host=arm-linux
  这样在你的build目录中就将生成Makefile和一个libtool脚本,这些将在make时用到。
  5、 修改Makefile文件
  请修改Makefile文件,将下面的这行
  BCC = arm-linux-gcc -g -O2
  改掉,改成:
  BCC = gcc -g -O2
  一般地,我们都是将sqlite放到arm-linux的硬件板子上运行,所以我们一般将其编译成静态链接的形式。如果是共享so库的话,比较麻烦。
  所以继续修改Makefile,找到如下地方:
  sqlite:
  将有其后的“libsqlite.la”改成
  “.libs/libsqlite.a”
  大功告成,现在可以make了。
  应该不会出错,生成sqlite,libsqlite.a,libsqlite.so。你可以使用命令:find -name "sqlite";find -name "*.a";find -name "*.so"查
  看文件所在的目录。
  此时生成的sqlite文件是还未strip过的,你可以使用命令“file sqlite”查看文件信息。用strip处理过后,将去掉其中的调试信息,执行文
  件大小也将小很多。命令如下:
  arm-linux-strip sqlite
  第二步 在arm板上运行sqlite
  将sqlite拷贝到你的arm板上,方法很多,你需要根据自己的情况来选择。如ftp,cmdftp,wget等。
  我的方法是使用wget将sqlite下载到arm板的/tmp目录,此目录是可写的。命令如下:
  busybox wget ftp://192.168.0.100/sqlite
  上面的命令,你可以看到我的板子上是已经有busybox来支持wget命令了的。:-)
  好,开始运行
  chmod +wx sqlite
  ./sqlite test.sqlite
  会出现
  sqlite>
  提示符号,打个“.help”来看看命令先:-)
  sqlite>.help
  好了。现在sqlite已经在arm-linux下跑了起来。如何,感觉不错吧,在arm板子上玩玩“select * from”语句蛮爽吧:-)
  友情提示:
  如果sqlite在处理数据库过程中出现“The database disk image is malformed”,如:你在delete from sometable时,可能遇到这个问题。
  那么就是你的arm板上的空间不够(如在/tmp下),请删除掉一些文件。我遇到的情况是空间还剩1-2M都会出现这个提示。删除后空余4M,就正
  常了(如delete from sometable)。我是开的8M的ramdisk做开发玩的:-)

本文转自
http://hi.baidu.com/shlongli/blog/item/a302117abfa42dec2e73b348.html