如何在 Delphi 中静态链接 SQLite

搞了我几个小时,终于成功在 Delphi 中静态链接了 SQLite (v3.5.4),下一步就是研究加密了,呵呵
中间其实遇到很多问题,今天累了,就不说了,改天补上



下载测试工程

下面说说方法

1.当然是下载 SQLite源代码啦,呵呵,不过记得要是 all in one 的 amalgamation 版本哦
  (修正: amalgamation 并非 all in one, 只是 core code all in one, 源代码里的其他文件也是不能少的!)
2.解压缩,得到3个文件 sqlite3.c sqlite3.h sqlite3ext.h
  然后把 sqlite3.c 编译成 obj 以便在 Delphi 中使用
  要注意的是不要用 VC 编译,要用 Borland 的 C++ 编译器,比如 Delphi 自带的 bcc
  这主要是因为 VC 编译的 obj 是 COFF 格式的,而 Borland 用的 obj 是 OMF 格式
  bcc 编译的命令行: bcc32 -pc -RT- -O -w- -6 -I(bcc32)\include -c sqlite3.c
3.光有 sqlite3.obj 还不够哦,呵呵,因为 sqlite3.c 有链接其他的库
  这里提供所有要用到的 obj 文件 下载
4.现在所有的 obj 文件都准备好了,不过别高兴的太早了,现在只完成了一小部分而已...
  要在 Delphi 中使用这些 obj 中的函数,必须要先声明一下
  先新建个 Unit, 比如 sqlite3.pas, 然后指定链接的 obj 文件,如
    {$L 'OBJ\sqlite3_5_4.obj'}
    {$L 'OBJ\streams.obj'} //duplicato
    {$L 'OBJ\_ftoul.obj'}
    {$L 'OBJ\files.obj'}
  注意顺序哦,呵呵
  然后添加函数声明
  比如要用到 sqlite3_open 方法,在 sqlite 的源代码里声明是这样的
    SQLITE_API int sqlite3_open(
      const char *filename,   /* Database filename (UTF-8) */
      sqlite3 **ppDb          /* OUT: SQLite db handle */
    );
  在 Delphi 中相应的声明为:
    function _sqlite3_open(dbname: PChar; var db: Pointer): Integer; cdecl; external;
  注意调用方式为 cdecl, 函数名要以 _ 开头,否则会找不到
  只是 sqlite3 函数好多哦,呵呵,所以我才说只完成了部分工作嘛...
5.OK,完成了函数声明才算是全部完成
  现在可以正式使用了~

常见问题:
  1.编译时报 Unsatisfied forward or external declaration
    出现这个错误的原因是声明的函数的找不到
    一般来说是因为链接的 obj 文件不全,或者顺序不对
    还有就是声明的函数名称不对,找不到
  2.编译时报 Internal Error: L3576
    声明的函数参数不匹配

enjoy~
   



看清这世界的美丽与残酷



NAILY Soft
Sephil on CNBlogs

 

标签: Delphi, SQLite
posted @ 2007-12-19 05:59 Sephil 阅读(3723) 评论(11) 编辑 收藏

 回复 引用   
#1楼 2008-04-16 14:43 yce[未注册用户]
呵呵,试了一下,不错,谢谢!
 回复 引用   
#2楼 2008-05-01 19:25 passengerA[未注册用户]
确实很不错,
非常感谢,
另外请问那些其他的obj文件是如何得到的?
引用的顺序是怎么确定的?

 回复 引用   
#3楼 2008-10-26 13:49 loveddd[未注册用户]
D:\Program Files\borlamd c++\Bin>bcc32 -pc -RT- -O -w- -6 -I(bcc32)\include -c s
qlite3.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
sqlite3.c:
Error E2209 sqlite3.c 494: Unable to open include file 'stdarg.h'
Error E2303 sqlite3.c 2437: Type name expected
Error E2209 sqlite3.c 7237: Unable to open include file 'stdio.h'
Error E2209 sqlite3.c 7238: Unable to open include file 'stdlib.h'
Error E2209 sqlite3.c 7239: Unable to open include file 'string.h'
Error E2209 sqlite3.c 7240: Unable to open include file 'assert.h'
Error E2209 sqlite3.c 7241: Unable to open include file 'stddef.h'
Error E2209 sqlite3.c 8555: Unable to open include file 'windows.h'
Error E2303 sqlite3.c 10373: Type name expected
Error E2303 sqlite3.c 10375: Type name expected
Error E2084 sqlite3.c 10718: Parameter names are used only with a function body
Error E2209 sqlite3.c 11101: Unable to open include file 'ctype.h'
Error E2209 sqlite3.c 11102: Unable to open include file 'time.h'
Error E2451 sqlite3.c 11154: Undefined symbol 'va_list' in function getDigits
Error E2379 sqlite3.c 11154: Statement missing ; in function getDigits
Error E2140 sqlite3.c 11155: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11156: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11157: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11158: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11159: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11160: Declaration is not allowed here in function getDigi
ts
Error E2140 sqlite3.c 11161: Declaration is not allowed here in function getDigi
ts
Error E2451 sqlite3.c 11162: Undefined symbol 'ap' in function getDigits
Error E2188 sqlite3.c 11164: Expression syntax in function getDigits
Error E2188 sqlite3.c 11165: Expression syntax in function getDigits
Error E2228 sqlite3.c 11165: Too many error or warning messages in function getD
igits
*** 26 errors in Compile ***
---
是怎么回事情?请指教

 回复 引用   
#4楼 2008-11-16 22:53 左眼[未注册用户]
深受此文启发,顺便写个了根据符号名从c的lib文件中导出obj文件的工具,以后delphi引用c的东西就方便多了
 回复 引用   
#5楼 2009-04-02 00:33 ledo88[未注册用户]
你好,我用Borland C++2007编译3.5.4和最新的3.6.12均成功通过。
用delphi 2007,在sqlite3.pas文件中引用sqlite3_5_4.obj,编译成功。
但引用sqlite3_6_12.obj时出现以下错误信息


[Pascal Hint] sqlite3.pas(24): H2164 Variable '__dll_table' is declared but never used in 'sqlite3'
[Pascal Hint] sqlite3.pas(52): H2164 Variable '__exe_table' is declared but never used in 'sqlite3'
[Pascal Error] sqlite3.pas(349): E2065 Unsatisfied forward or external declaration: '_strlen'

 回复 引用   
#6楼 2009-08-16 22:31 account[未注册用户]
请问obj文件的顺序是怎么确定的?


 回复 引用   
#7楼 2009-09-03 10:44 oadwcv[未注册用户]
请问“所有要用到的 obj 文件”,这个是怎么得到的?比如strlen?急啊,盼回复谢谢!!!
 回复 引用 查看   
#8楼[楼主] 2009-09-13 00:04 Sephil      
上面有下载啊
 回复 引用 查看   
#9楼 2009-09-21 00:53 Henry Read      
这就是C语言标准运行是库的obj,应该是从C++ Builder里提取出来的吧。
 回复 引用 查看   
#10楼 2010-05-31 08:36 yangvb      
请问obj文件的顺序是怎么确定的?
 回复 引用 查看   
#11楼 2011-05-12 08:44 s_guang      
在用DELPHI编译SQLITE V3.7.6.2时出现以下问题:
1、[DCC Error] sqlite3.pas(314): E2065 Unsatisfied forward or external declaration: '_strlen'
2、[DCC Error] sqlite3.pas(314): E2065 Unsatisfied forward or external declaration: '_atol'
3、[DCC Error] sqlite3.pas(314): E2065 Unsatisfied forward or external declaration: '__ltoupper'
请问obj文件的顺序是怎么确定的?谢谢!