在Visual Studio 下编译 sqlite3
首先,sqlite 官方已经有预编译好的DLL下载,如果你不需要特别优化的版本,直接下载官网上的DLL即可。你可以在下载 页面的
“Precompiled Binaries For Windows” 可以找到。 不过你想编译出自己的版本,比如添加优化参数后编译,debug版,或进行一些定制(例
如官网这里 有一些预编译宏,允许你定制自己的sqlite),那么你就需要自己编译了。 首先在下载源代码,在页面的第一行就是,如
sqlite-amalgamation-3_6_12.zip ,接着下载编译好的DLL,我们主要是需要这里面的一个 sqlite3.def 文件,因为源代码里没有。一般下拉
到 “Precompiled Binaries For Windows” 就可以看到,例如 sqlitedll-3_6_12.zip 。有了这两个压缩包就可以了。 首 先解压
sqlite-amalgamation-3_6_12.zip 到文件夹 sqlite3/,会得到 sqlite3.c,sqlite3.h,sqlite3ext.h 三个文件,然后解压 sqlitedll-
3_6_12.zip ,把 sqlite3.def 移动到 sqlite3/ 下面。 打开 visual studio,新建一个空的dll工程,工程名为sqlite3,这样是为了能直接
生成名为sqlite3.dll 的dll。然后sqlite3/ 下的4个文件文件夹移动到此工程目录下,即与 sqlite3.vcproj 在同一级。 将 sqlite3.c 添
加到工程,具体做法是右击“源文件”,选择“添加->现有项”,浏览到sqlite3.c即可。
这样就完成了第一步,但是这个时侯如果你编译的话,会发现有好几个错误和 N 个警告。首先在项目解决方案里移除以下几个文件
tclsqlite.c , shell.c , rtree.c , fts3.c , fts3_hash.c , fts3_icu.c , fts3_porter.c , fts3_tokenizer1.c ,
fts3_tokenizer.c 。
其中 tclsqlite.c 用于生成基于 TCL 的 API ,如果要编译,这需要另外下载 tcl.h ; shell.c 用于生成命令行模式的 sqlite.exe ;
rTree.c 是建立数据库 R 树索引的模块, fts3*.c 是全文索引模块,这两个扩展模块直接编译的时候会提示以下错误,
1>fts3.obj : error LNK2005: _sqlite3_extension_init 已经在 rtree.obj 中定义
1>fts3.obj : error LNK2005: _sqlite3_api 已经在 rtree.obj 中定义
1>fts3_tokenizer.obj : error LNK2005: _sqlite3_api 已经在 rtree.obj 中定义
至于原因,一时半会儿也没找出来,就先不管它了,从项目中移除上面几个文件再编译就 Ok 了(移除 Rtree.c 是因为发现用其编译出来的库
时会出错)。
(呵呵,初次测试的目的是基本应用,所以具体错误的原因就没深究,有兴趣的可以共同探讨一下,主要是 Rtree 和全文检索模块的应用)
然后将sqlite3.def 添加到工程,然后输入给连接器。具体做法是右击工程sqlite3,选择“属性”,在弹出的对话框中选择“连接器->输入”
,在“模块定义文件”中输入sqlite3.def。
好,可以开始编译了,右击sqlite3,选择“生成”,结果链接出错, ------ 已启动生成: 项目: sqlite3, 配置: Release Win32 ------
正在编译...
sqlite3.c
正在链接...
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_database_name
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_database_name16
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_origin_name
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_origin_name16
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_table_name
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_column_table_name16
sqlite3.def : error LNK2001: 无法解析的外部符号 sqlite3_table_column_metadata 看来sqlite3.def 中定义的这几个导出函数找不到定
义,什么原因呢,经过一番google,找到这篇文章 ,需要增加一个预定义宏 SQLITE_ENABLE_COLUMN_METADATA,这个宏是什么意思呢,到官网
去查一下, SQLITE_ENABLE_COLUMN_METADATA
When this C-preprocessor macro is defined, SQLite includes some additional APIs that provide convenient access to meta-data
about tables and queries. The APIs that are enabled by this option are:
* sqlite3_column_database_name()
* sqlite3_column_database_name16()
* sqlite3_column_table_name()
* sqlite3_column_table_name16()
* sqlite3_column_origin_name()
* sqlite3_column_origin_name16()
* sqlite3_table_column_metadata() 原来是只有当启用了这个宏后,c代码中才会后这几个函数的定义,OK,在预处理器中添加
SQLITE_ENABLE_COLUMN_METADATA。
再来编译,搞定!