How to statically link SQLite extension?

Here are my notes from the last time I needed a SQLite feature that hadn’t landed in a release yet. I don’t know what you need to do to enable modules, but hopefully this will be helpfull in getting started.

On Ubuntu you will need build-essential installed at a minimum.

sudo apt update
sudo apt install build-essential

You can get the SQLite source code from SQLite Download Page


Build SQLite

Download and build SQLite

(this creates the sqlite command)


wget https://sqlite.org/snapshot/sqlite-snapshot-202010201440.tar.gz

tar xvfz sqlite-snapshot-202010201440.tar.gz

cd sqlite-snapshot-202010201440

./configure

make

Build sqlite command


gcc shell.c sqlite3.c -lpthread -ldl

Build libsqlite


gcc -lpthread -ldl -shared -o libsqlite3.so.0 -fPIC sqlite3.c

Link crystal program agains specific libsqlite version


ls libsqlite3.so.0

crystal build --link-flags -L./ --link-flags -lsqlite3 src/db_test.cr

List links


ldd db_test

Specify library path at runtime


LD_LIBRARY_PATH=./ ./db_test

See sqlite version used


DB_URI = "sqlite3://./db_test.db"

DB.open DB_URI do |db|

db_version = db.scalar "select sqlite_version();"

puts "SQLite #{db_version}"

end

Better way?


# Crystal runs this to determine lib path

pkg-config --libs sqlite3

# Alter pkg-config path to include custom path

PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig" pkg-config --libs sqlite3`

-L/usr/local/Cellar/sqlite/3.33.0/lib -lsqlite3

PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig" crystal build src/db_test.cr

2 Likes