DEV Community

Cyb3r Ninja 🥷
Cyb3r Ninja 🥷

Posted on • Edited on

Step-by-Step Guide to Compiling SQLCipher (Encrypted SQLite) for Windows

Comprehensive Guide for Compiling SQLCipher on Windows with Visual Studio 2022

SQLCipher is an enhanced version of SQLite that provides encryption, making it a popular choice for secure database management. If you're looking to compile SQLCipher on Windows using Visual Studio 2022, follow this step-by-step guide to ensure a smooth build process.

Prerequisites for Compiling SQLCipher

To compile SQLCipher on Windows, ensure you have the following tools and components:

1. Visual Studio 2022 with C++ Tools

  • Install Visual Studio 2022 with the Desktop Development with C++ workload. This setup includes nmake (MSVC) and the C++ compilers needed for SQLCipher's Makefile.msc build process.
  • Open Visual Studio Installer, select your Visual Studio version, and modify it to add the required C++ development components if not already installed.

Image description

2. OpenSSL

  • Download and install a copy of OpenSSL. A reliable source for Windows binaries is: Win32/Win64 OpenSSL Installer
  • Important: Choose the full version (not the "light" version) as it includes all necessary development files.

3. TCL (Tool Command Language)

  • SQLCipher requires TCL for building SQLite, its underlying component.
  • Download TCL from IronTcl, extract it, and locate the tclsh86t.exe file in the bin directory. Rename tclsh86t.exe to tclsh.exe.

Example path to the tclsh.exe file:

D:\SQLEncrypt\irontcl-amd64-8.6.7\IronTcl\bin
Enter fullscreen mode Exit fullscreen mode

Copy this path for later use.

Building SQLCipher

With the prerequisites in place, follow these steps to compile SQLCipher for Windows.

1. Launch the Developer Command Prompt

Open the Developer Command Prompt for Visual Studio 2022. You can do this from the Start Menu or run the following command from a standard Command Prompt:

cmd /k "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"
Enter fullscreen mode Exit fullscreen mode

2. Configure for 64-bit Compilation

Set the command prompt to use 64-bit environment variables:

"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" x64
Enter fullscreen mode Exit fullscreen mode

Successful execution should display:

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.11.2
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
Enter fullscreen mode Exit fullscreen mode

3. Add TCL to Your Path

Ensure the TCL executable is accessible by adding it to your system PATH:

SET PATH=%PATH%;D:\SQLEncrypt\irontcl-amd64-8.6.7\IronTcl\bin
Enter fullscreen mode Exit fullscreen mode

4. Set Platform Environment Variable

Specify the build platform:

SET PLATFORM=x64
Enter fullscreen mode Exit fullscreen mode

Clone SQLCipher Repository

Download SQLCipher's source code:

git clone https://github.com/sqlcipher/sqlcipher.git
Enter fullscreen mode Exit fullscreen mode

Modifying Makefile.msc

Ensure the Makefile.msc in the root of the SQLCipher repository is properly configured:

1. Update Compilation Flags

  • Locate and change the value:

    • Search for -DSQLITE_TEMP_STORE=1 and change 1 to 2 in both TCC and RCC variables.
  • Add OpenSSL flags:

    • Add the following line beneath the TCC and RCC variable updates to include OpenSSL headers:
    # Flags to include OpenSSL
    TCC = $(TCC) -DSQLITE_HAS_CODEC -I"C:\Program Files\OpenSSL-Win64\include"
    

    Replace C:\Program Files\OpenSSL-Win64 with your OpenSSL installation path.

2. Update Library Paths

Find the line:

LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ICULIBDIR)
Enter fullscreen mode Exit fullscreen mode

Add these lines right after the !ENDIF:

LTLIBPATHS = $(LTLIBPATHS) /LIBPATH:$(ICULIBDIR) /LIBPATH:"C:\Program Files\OpenSSL-Win64\lib\VC\static"
LTLIBS = $(LTLIBS) libcrypto64MT.lib libssl64MT.lib ws2_32.lib shell32.lib advapi32.lib gdi32.lib user32.lib crypt32.lib kernel32.lib
Enter fullscreen mode Exit fullscreen mode

Adjust the paths to match your OpenSSL installation.

Create a Binary Build Directory

Create a directory for storing the compiled binaries:

E:\Temp\GitHub\sqlcipher-build
Enter fullscreen mode Exit fullscreen mode

Compile SQLCipher

Navigate to the binary build directory:

cd E:\Temp\GitHub\sqlcipher-build
Enter fullscreen mode Exit fullscreen mode

Run nmake to compile:

nmake /f E:\Temp\GitHub\sqlcipher\Makefile.msc TOP=E:\Temp\GitHub\sqlcipher
Enter fullscreen mode Exit fullscreen mode

Ensure all folder paths are correctly replaced with your own setup.

Final Output

Once the build completes, you should find sqlite3.exe and sqlite3.dll in your build directory. You can now copy these files to any location where they are needed for your project.

Top comments (0)