linux - 如何将扩展编译成sqlite?

标签 linux sqlite compilation

我想将一个扩展编译成 sqlite 以便在运行时加载。

我正在使用的文件是扩展名 - functions.c 来自 https://www.sqlite.org/contrib

我已经能够编译成可加载模块,但我需要静态链接它以便在运行时加载(使用 shell.c 在运行时创建接口(interface))

我已经阅读了关于链接的手册,但老实说,这有点超出我的理解范围!

有人可以告诉我编译需要做什么吗?

最佳答案

我找到了一种通过 extension_functions.c 提供的附加函数从源代码编译 sqlite3 的方法。

Note:

At this time I show the quite dirty and easy way to compile sqlite with additional features because I haven't succeed to do that in right way.

But please remember that it would perhaps be much better to prepare a brand new part of amalgamation for adding custom features as @ngreen says above.
That's the designed way of sqlite itself.

1。下载sqlite源码

https://www.sqlite.org/download.html

选择合并,最好使用autoconf版本。
例如,这里是3.33.0版本的下载链接。

https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz

curl -O https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz
tar -xzvf sqlite-autoconf-3330000.tar.gz
cd sqlite-autoconf-3330000

2。下载 extension_functions.c

在此网址列出。

https://sqlite.org/contrib

实际网址:

https://sqlite.org/contrib/download/extension-functions.c?get=25

curl -o extension_functions.c https://sqlite.org/contrib/download/extension-functions.c?get=25

3。配置编译

我们可以指定 --prefix 选项来确定构建内容的目的地。

./configure --prefix=/usr/local/sqlite/3.33.0

此时可以将其他配置时间选项指定为环境变量。
检查https://www.sqlite.org/draft/compile.html了解更多详情。

这是启用 JSON 和 RTree 索引功能的示例。

CPPFLAGS="-DSQLITE_ENABLE_JSON1=1 -DSQLITE_ENABLE_RTREE=1" ./configure --prefix=/usr/local/sqlite/3.33.0

也可以指定 autoconf 选项。

CPPFLAGS="-DSQLITE_ENABLE_JSON1=1 -DSQLITE_ENABLE_RTREE=1" ./configure --prefix=/usr/local/sqlite/3.33.0 --enable-dynamic-extensions

我在官方网站上找不到关于这些选项的任何文档,但在配置脚本本身中找到了一些内容。

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-silent-rules   less verbose build output (undo: "make V=1")
  --disable-silent-rules  verbose build output (undo: "make V=0")
  --disable-largefile     omit support for large files
  --enable-dependency-tracking
                          do not reject slow dependency extractors
  --disable-dependency-tracking
                          speeds up one-time build
  --enable-shared[=PKGS]  build shared libraries [default=yes]
  --enable-static[=PKGS]  build static libraries [default=yes]
  --enable-fast-install[=PKGS]
                          optimize for fast installation [default=yes]
  --disable-libtool-lock  avoid locking (might break parallel builds)
  --enable-editline       use BSD libedit
  --enable-readline       use readline
  --enable-threadsafe     build a thread-safe library [default=yes]
  --enable-dynamic-extensions
                          support loadable extensions [default=yes]
  --enable-fts4           include fts4 support [default=yes]
  --enable-fts3           include fts3 support [default=no]
  --enable-fts5           include fts5 support [default=yes]
  --enable-json1          include json1 support [default=yes]
  --enable-rtree          include rtree support [default=yes]
  --enable-session        enable the session extension [default=no]
  --enable-debug          build with debugging features enabled [default=no]
  --enable-static-shell   statically link libsqlite3 into shell tool
                          [default=yes]

仅供引用,这是 Homebrew 中使用的默认安装脚本。也许确定应该指定哪个选项会很有用。

def install
  ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_COLUMN_METADATA=1"
  # Default value of MAX_VARIABLE_NUMBER is 999 which is too low for many
  # applications. Set to 250000 (Same value used in Debian and Ubuntu).
  ENV.append "CPPFLAGS", "-DSQLITE_MAX_VARIABLE_NUMBER=250000"
  ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_RTREE=1"
  ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS3_PARENTHESIS=1"
  ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_JSON1=1"

  args = %W[
    --prefix=#{prefix}
    --disable-dependency-tracking
    --enable-dynamic-extensions
    --enable-readline
    --disable-editline
    --enable-session
  ]

  system "./configure", *args
  system "make", "install"
end

4。消除冲突

现在我们必须修改extension_functions.c,以避免在编译它们之前与sqlite的源代码发生冲突。

打开 extension_functions.c 并将第 123 ~ 128 行替换为单行 SQLITE_EXTENSION_INIT1

#ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#else
#include "sqlite3.h"
#endif

SQLITE_EXTENSION_INIT1

5。启用扩展功能

我们需要在 shell.c 中插入一些行来导入和启用扩展功能。

打开 shell.c,搜索 static void open_db 并在上面的行中插入 #include "extension_functions.c"

#include "extension_functions.c"

static void open_db(ShellState *p, int openFlags){

然后搜索sqlite3_shathree_init(p->db, 0, 0);并在init的底部插入sqlite3_extension_init(p->db, 0, 0);功能。

#endif
    sqlite3_fileio_init(p->db, 0, 0);
    sqlite3_shathree_init(p->db, 0, 0);
    sqlite3_completion_init(p->db, 0, 0);
    sqlite3_uint_init(p->db, 0, 0);
    sqlite3_decimal_init(p->db, 0, 0);
    sqlite3_ieee_init(p->db, 0, 0);
    sqlite3_extension_init(p->db, 0, 0);

6。编译

终于可以编译包含扩展函数的sqlite了。

make install

这需要一段时间,一旦完成,分发文件将在配置时通过 --prefix 选项指定的目的地生成。

# Now we can use extension_functions without loading it manually.
$ /usr/local/sqlite/3.33.0/bin/sqlite3
sqlite> select cos(10);
-0.839071529076452

关于linux - 如何将扩展编译成sqlite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898113/

相关文章:

linux - rsync 使用包含选项仅复制某些类型的文件

php - Apache 服务器压缩和利用浏览缓存问题

mysql - TideSDK:MySQL 代替 SQLite

android - Kotlin 安卓编译

compilation - ARM Thumb 模式 : Code Size not decreasing

linux - 管道 `find` 到 'tail`

linux - PPC460GT 上的 USB 功能

java - Makefile 同时编译 C 和 Java 程序

python - 在 sqlalchemy 中替代 sqlite 中的 rowid

sqlite - 从sqflite按值获取数据