心血来潮下载了SQLite源代码(版本 3.7.12.1),解压后只有4个文件:sqlite3.h,sqlite3ext.h,shell.c,sqlite3.c,简单粗暴我喜欢。
按照文档介绍,写了个最简单的demo程序如下:
// The name of a database is given by the first argument and // the second argument is one or more SQL statements to execute against the database. #include <stdio.h> #include "sqlite3.h" static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } int main(int argc, char **argv){ sqlite3 *db; char *zErrMsg = 0; int rc; if( argc!=3 ){ fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]); return(1); } rc = sqlite3_open(argv[1], &db); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return(1); } rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); return 0; }
想在VC2010中调试下sqlite3.c,就在调用sqlite3_open()处打了个断点,结果发现跟进去的位置好古怪,完全不是正确的地方。
搜到liigo的一篇文章“VC6/VC2005均不支持行数超过65536的C/C++源代码文件”,而当前sqlite3.c文件共136692行,看来VC2010仍然不支持过长的文件。
为了将sqlite3.c的行数压缩到65535行以内,有如下2个方案:
方案1:删除注释行、空行、#if 0 ... #endif
仅将注释行删除是不够的,即使将空行都删除也不够,再加上删除#if 0 ... #endif,勉强挤到了66675行。
懂正则表达式的话,上述操作其实不难。
虽然仍然超出65535一些,但是只要调试的代码行不要太靠后,大体上还是满足调试需求的。
方案2:拆分源文件为几部分,再通过#include的方式将其嵌入
源文件sqlite3.c文件共136692行,因为136692/65535=2.086,所以可以将此文件拆为3部分,主文件sqlite3.c,及两个子文件 sqlite3_1.h 和 sqlite3_1.h,调用方法为主文件sqlite3.c在最后#include另外两个子文件,如下:
... #include "sqlite3_1.h" #include "sqlite3_2.h"
为什么sqlite3.c文件这么大?
根据官网的说法,该文件是将多个文件(Sqlite核心文件及FTS3 和 RTREE 扩展)整合(amalgamation)为一个单独的文件的,据说这样可以让编译器达成更充分的优化,从而提高5%-10%的效率。
代码下载
为方便下载,我已经将Sqlite源代码(sqlite-amalgamation-3071201) + 方案1(sqlite3_no_comment) + 方案2(sqlite3_split)的代码打包上传在这里了。