C project: building with dynamically compiled SQLCipher


#1

Hi everybody,

I was surprised that official documentation does not include an example how to compile C projects that use SQLCipher.

I have managed to do $make and $make install of sqlcipher with dynamic linking of libcrypto under ArchLinux.

I want to build a simple SQL code in C, but I’m not sure how to compile it.
When only sqlite3 was available, I was doing it by:
$gcc SQLite_example.c -l sqlite3

where SQLite_example.c had #include <\sqlite3.h> (ignore the backslash)

Now I have added as well (according to README):
#define SQLITE_HAS_CODEC
#define SQLITE_TEMP_STORE 2

How can I build my project so it uses sqlcipher and it’s function sqlite3_key?
Any help would be appreciated.

Thanks in advance,

Bogdan Pavkovic


#2

Hi everybody,

A small (noob) tutorial for those wanting to use SQLCipher programatically (calling sqlite3_key instead of PRAGMA) in their C projects (in the mean time I’ve managed to overcome the lack of doc).

#Building SQLCipher from source
(a small update to README.md is necessary, an update will be suggested on the official git page):

  1. $ git clone https://github.com/sqlcipher/sqlcipher.git
  2. $ cd sqlcipher
  3. $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2" LDFLAGS="-lcrypto"
  4. $ make
  5. $ make install #if you want to do a system wide install of SQLCipher

Mark the output of make install, especially the following lines:

  • libtool: install: /usr/bin/install -c .libs/libsqlcipher.a /usr/local/lib/libsqlcipher.a
  • /usr/bin/install -c -m 0644 sqlite3.h /usr/local/include/sqlcipher

these are the folders of SQLCipher headers and the library necessary when building your proper C project.

#Building your own minimal C project example
In your SQLite_example.c put the following lines (source file available at http://goo.gl/tGWDum):

  1. #include “sqlite3.h” //We want to SQLCipher extension, rather then a system wide SQLite header
  2. rc = sqlite3_open(“test.db”, &db); //open SQLite database test.db
  3. rc = sqlite3_key(db, “1q2w3e4r”, 8);//apply encryption to previously opened database

Build your example:

  • $gcc SQLite_example.c -o SQLtest -I /path/to/local/folder/with/sqlcipher/header/files/ -L /path/to/local/folder/with/sqlcipher/library.a -l sqlcipher

e.g. with paths extracted from the output of $make install

  • $gcc SQLite_example.c -o SQLtest -I /usr/local/include/sqlcipher -L /usr/local/lib/libsqlcipher.a -lsqlcipher

Finally, make sure that your SQLCipher library is in the system wide library path e.g. for (Arch)Linux:

  • $ export LD_LIBRARY_PATH=/usr/local/lib/

Run your test code ((Arch)Linux):

  • $ ./SQLtest