crystal-lang / crystal-sqlite3

SQLite3 bindings for Crystal
MIT License
139 stars 30 forks source link

How to use a different version of SQLite? #57

Closed samueleaton closed 4 years ago

samueleaton commented 4 years ago

If I install a newer version of SQLite (e.g. in /usr/local/) how do I get crystal to use the newer binaries?

chillfox commented 4 years ago

I am trying to figure this out as well (specifically to use the pre-release version).

chillfox commented 4 years ago

ok, I finally figured it out. Please let me know if there is a better way.

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 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

Specify library path at runtime

LD_LIBRARY_PATH=./ ./db_test

And you can see the version crystal is using like this

DB_URI = "sqlite3://./db_test.db"
DB.open DB_URI do |db|
  db_version = db.scalar "select sqlite_version();"
  puts "SQLite #{db_version}"
end
bcardiff commented 4 years ago

pkg-config can be used to instruct crystal where to find the libraries.

Basically crystal does $ pkg-config --libs sqlite3 to determine de link flags.

If PKG_CONFIG_PATH is changed to include some custom locations you can tweak the link flags as pleased

$ PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig" pkg-config --libs sqlite3`
-L/usr/local/Cellar/sqlite/3.33.0/lib -lsqlite3

So, set up somehow the PKG_CONFIG_PATH and compile as usual.

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