From 82758c7890251bf70227b193732a53309f22038b Mon Sep 17 00:00:00 2001 From: Mia Date: Sun, 28 Nov 2021 08:18:40 +0000 Subject: [PATCH 1/2] Docs on how to compile and link SQLite --- README.md | 4 ++ compile_and_link_sqlite.md | 84 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 compile_and_link_sqlite.md diff --git a/README.md b/README.md index eeef632..c5fc66a 100644 --- a/README.md +++ b/README.md @@ -48,3 +48,7 @@ end * `Time` is implemented as `TEXT` column using `SQLite3::DATE_FORMAT_SUBSECOND` format (or `SQLite3::DATE_FORMAT_SECOND` if the text does not contain a dot). * `Bool` is implemented as `INT` column mapping `0`/`1` values. + +## Guides + +- [Compile and link SQLite](compile_and_link_sqlite.md) diff --git a/compile_and_link_sqlite.md b/compile_and_link_sqlite.md new file mode 100644 index 0000000..c1df671 --- /dev/null +++ b/compile_and_link_sqlite.md @@ -0,0 +1,84 @@ +# How to Compile And Link SQLite + + +## Install Prerequisites (Ubuntu) + +On Ubuntu you will need build-essential installed at a minimum. + +```sh +sudo apt update +sudo apt install build-essential +``` + + +## Download And Extract The Source Code + +Source code for the latest release can be downloaded from the [SQLite Download Page](https://sqlite.org/download.html). +Look for "C source code as an amalgamation", It should be the first one on the page. + +```sh +wget https://sqlite.org/2021/sqlite-amalgamation-3370000.zip +unzip sqlite-amalgamation-3370000.zip +cd sqlite-amalgamation-3370000 +``` + + +## Compile SQLite + +Compile the sqlite command. + +```sh +gcc shell.c sqlite3.c -lpthread -ldl -o sqlite3 +./sqlite3 --version +``` + +Compile libsqlite. + +```sh +gcc -lpthread -ldl -shared -o libsqlite3.so.0 -fPIC sqlite3.c +``` + +## Using The New Version of SQLite + +The path to libsqlite can be specified at runtime with "LD_LIBRARY_PATH". + +```sh +# Crystal run +LD_LIBRARY_PATH=../sqlite-amalgamation-3370000 crystal run src/app.cr + +# This way will allow specifying the library location at runtime if it is different from the system default. +crystal build --release --link-flags -L"$(realpath ../sqlite-amalgamation-3370000/libsqlite3.so.0)" src/app.cr +LD_LIBRARY_PATH=../sqlite-amalgamation-3370000 ./app + +# ldd can be used to see which libsqlite is being linked +LD_LIBRARY_PATH=../sqlite-amalgamation-3370000 ldd ./app +``` + +Or the absolute path to libsqlite can be specified at compile time. + +```sh +crystal run --link-flags "$(realpath ../sqlite-amalgamation-3370000/libsqlite3.so.0)" src/app.cr + +# This will create a version that only works if libsqlite in the excact same location as when it was compiled. +crystal build --release --link-flags "$(realpath ../sqlite-amalgamation-3370000/libsqlite3.so.0)" src/app.cr +./app + +# Use ldd to see which libsqlite is being linked +ldd ./app +``` + + +## Check SQLite Version From Crystal + +To check which version of SQLite is being used from Crystal. + +```crystal +# src/app.cr + +DB_URI = "sqlite3://:memory:" + +DB.open DB_URI do |db| + db_version = db.scalar "select sqlite_version();" + puts "SQLite #{db_version}" +end +``` From d36980242f27b1916725753ff37b519b82ba1627 Mon Sep 17 00:00:00 2001 From: Mia Bennett Date: Fri, 3 Dec 2021 00:56:19 +0000 Subject: [PATCH 2/2] Add reasons for compiling SQLite from source. --- compile_and_link_sqlite.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compile_and_link_sqlite.md b/compile_and_link_sqlite.md index c1df671..106d64b 100644 --- a/compile_and_link_sqlite.md +++ b/compile_and_link_sqlite.md @@ -1,5 +1,12 @@ # How to Compile And Link SQLite +There are two main reasons to compile SQLite from source and they are both about getting features that are otherwise unavailable. + +- You may need a feature from a release that haven't made it to your distro yet or you want to use the latest code from development. +- Perhaps you want some compile time features enabled that are not commonly enabled by default. + +This guide assumes the first reason and goes through how to compile the latest release. + ## Install Prerequisites (Ubuntu) @@ -43,6 +50,9 @@ gcc -lpthread -ldl -shared -o libsqlite3.so.0 -fPIC sqlite3.c The path to libsqlite can be specified at runtime with "LD_LIBRARY_PATH". ```sh +# directory of your crystal app +cd ../app + # Crystal run LD_LIBRARY_PATH=../sqlite-amalgamation-3370000 crystal run src/app.cr