How to cross compile SQLite - Step by Step Guide

Introduction

This tutorial introduces the way to cross compile the SQLite database engine (versions 2.x.x and 3.x.x) on an GNU Linux embedded platform.
The host machine will be a PC on which a GNU Linux distribution is installed.
The embedded platform might be constructed on top of one of the following architectures: arm, mips, mipsel, ppc or sparc.

The SQLite's home page: http://www.sqlite.org/.
The latest available release at the time of writing of this tutorial is 3.5.6 (February 6 2008).
For people wishing to cross compile an SQLite 2 package, the latest and last available release is 2.8.17.

Downloading any SQLite 3 releases can be done by typing:

wget http://sqlite.org/sqlite-3.5.6.tar.gz
Since it is very hard to find the sqlite 2.8.17 tarball on the sqlite web site, you can directly download the file here. Also, you may download it from the SQLite cvs server by typing the following commands:

cvs -d :pserver:anonymous@www.sqlite.org:/sqlite login
cvs -d :pserver:anonymous@www.sqlite.org:/sqlite checkout sqlite
cvs update -r version_2

Overview

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

SQLite is the most widely deployed SQL database engine in the world. It is used in countless desktop computer applications as well as consumer electronic devices including cellphones, PDAs, and MP3 players. The source code for SQLite is in the public domain.

Prerequisites

To cross compile the SQLite database engine, you need either a cross compiler, or an emulated Linux running on top of a virtualization software such as QEMU, matching your specific target platform.

If you wish to build yourself such a cross compiler, you should read the crosstool tutorial. There you will find all the information you need in order to build a C, C++ 4.1 GNU compiler for an ARM platform.

If you prefer to setup an ARM Debian system on top of a traditional Ubuntun distro (after all you are an hacker, aren't you?), have a look at How to setup an ARM Debian on Ubuntu tutorial in this wiki.

In addition to the cross compiler, a native compiler should be installed on your system as well. Please check you have gcc and g++ correctly installed on your system (include files, libraries) since SQLite needs to compile native tools during the compilation process.

On Debian or Ubuntu systems, these components should be installed by successively typing:

apt-get install build-essential
apt-get install gcc
apt-get install g++

Cross Compilation Process

In this section, we will cover the process to compile both SQLite 2 and 3 releases.

Using a cross compiler

Assumptions

During the process, we will assume the cross toolchain is called xxx-yyy-linux-gnu, meaning that the gcc, g++, ar, ..., ld utilities are prefixed by xxx-yyy-linux-gnu. For example, for this toolchain the C compiler (gcc) is called xxx-yyy-linux-gnu-gcc.

We will also assume that the bin, include, lib and libexec folders of the cross toolchain are accessible through the PATH variable environment in order to work with the compilers from any place in the file system.

Finally, we suppose it is intalled in the following folder: /opt/crosstool/gcc-4.1.2-glibc-2.3.2/xxx-yyy-linux-gnu.

Organization

The releases being cross compiled are 2.8.17 and 3.5.6.

The tarball files will then be respectively extracted under the /opt/external_packages/sqlite/2.8.17/source and /opt/external_packages/sqlite/3.5.6/source folders. And the results of the compilation will be respectively stored under the /opt/external_packages/sqlite/2.8.17/compiled/xxx-yyy-linux-gnu and /opt/external_packages/sqlite/3.5.6/compiled/xxx-yyy-linux-gnu folders.

The files resulting from the cross compilation are listed here:

SQLite 2

.:
total 12
drwxr-xr-x 2 root root 4096 2008-01-02 15:35 bin
drwxr-xr-x 2 root root 4096 2008-01-02 15:35 include
drwxr-xr-x 3 root root 4096 2008-01-02 15:35 lib

./bin:
total 56
-rwxr-xr-x 1 root root 49171 2008-01-02 15:35 sqlite

./include:
total 40
-rw-r--r-- 1 root root 38781 2008-01-02 15:35 sqlite.h

./lib:
total 2272
-rw-r--r-- 1 root root 1325160 2008-01-02 15:35 libsqlite.a
-rwxr-xr-x 1 root root     818 2008-01-02 15:35 libsqlite.la
lrwxrwxrwx 1 root root      18 2008-01-07 14:27 libsqlite.so -> libsqlite.so.0.8.6
lrwxrwxrwx 1 root root      18 2008-01-07 14:27 libsqlite.so.0 -> libsqlite.so.0.8.6
-rwxr-xr-x 1 root root  979692 2008-01-02 15:35 libsqlite.so.0.8.6
drwxr-xr-x 2 root root    4096 2008-01-02 15:35 pkgconfig

./lib/pkgconfig:
total 4
-rw-r--r-- 1 root root 247 2008-01-02 15:35 sqlite.pc

SQLite 3

.:
total 12
drwxr-xr-x 2 root root 4096 2008-01-02 16:27 bin
drwxr-xr-x 2 root root 4096 2008-01-02 16:27 include
drwxr-xr-x 3 root root 4096 2008-01-02 16:27 lib

./bin:
total 1000
-rwxr-xr-x 1 root root 1016645 2008-01-02 16:27 sqlite3

./include:
total 196
-rw-r--r-- 1 root root  19433 2008-01-02 16:27 sqlite3ext.h
-rw-r--r-- 1 root root 175550 2008-01-02 16:27 sqlite3.h

./lib:
total 3440
-rw-r--r-- 1 root root 2075294 2008-01-02 16:27 libsqlite3.a
-rwxr-xr-x 1 root root     823 2008-01-02 16:27 libsqlite3.la
lrwxrwxrwx 1 root root      19 2008-01-07 14:27 libsqlite3.so -> libsqlite3.so.0.8.6
lrwxrwxrwx 1 root root      19 2008-01-07 14:27 libsqlite3.so.0 -> libsqlite3.so.0.8.6
-rwxr-xr-x 1 root root 1429254 2008-01-02 16:27 libsqlite3.so.0.8.6
drwxr-xr-x 2 root root    4096 2008-01-02 16:27 pkgconfig

./lib/pkgconfig:
total 4
-rw-r--r-- 1 root root 269 2008-01-02 16:27 sqlite3.pc

Cross compiling SQLite 2.8.17

Step 1

Install the sqlite software by untaring it into /opt/external_packages/sqlite/2.8.17/source folder and then cd to this directory.

Note: All the directories and files listed below must be moved from sqlite-2.8.17 folder to the parent folder(/opt/external_packages/sqlite/2.8.17/source):

   aclocal.m4
   art
   config.guess
   config.sub
   configure
   configure.ac
   CVS
   doc
   install-sh
   ltmain.sh
   main.mk
   Makefile.in
   Makefile.linux-gcc
   publish.sh
   README
   spec.template
   sqlite.1
   sqlite.def
   sqlite.pc.in
   src
   test
   tool
   VERSION
   www
   

Step 2

We want to cross compile sqlite with its default parameters therefore we configure it by typing:

   ./configure --host=xxx-yyy-linux-gnu --prefix=/opt/external_packages/sqlite/2.8.17/compiled/xxx-yyy-linux-gnu
   

Yet, during the configuration process, you will get the following errors:

checking for xxx-yyy-linux-gnu-gcc option to accept ANSI C... (cached) none needed
configure: error: unable to find a compiler for building build tools

Or

   checking for tcl.h... no
   checking for /usr/local/include/tcl.h... configure: error: cannot check for file existence when cross compiling
   

Solving theses errors consists in making the following changes in the configure script:

The section:

   if test "$cross_compiling" = "yes"; then
     { { echo "$as_me:$LINENO: error: unable to find a compiler for building build tools" >&5
   echo "$as_me: error: unable to find a compiler for building build tools" >&2;}
     { (exit 1); exit 1; }; }
   fi
   

Becomes:

if test "$cross_compiling" = "yes"; then
     { { echo "$as_me:$LINENO: error: unable to find a compiler for building build tools" >&5
   echo "$as_me: error: unable to find a compiler for building build tools" >&2;}
     }
   fi
   

The section:

   if eval "test "${$as_ac_File+set}" = set"; then
     echo $ECHO_N "(cached) $ECHO_C" >&6
   else
     test "$cross_compiling" = yes &&
     { { echo "$as_me:$LINENO: error: cannot check for file existence when cross compiling" >&5
   echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
      { (exit 1); exit 1; }; }
   if test -r "$dir/include/tcl.h"; then
     eval "$as_ac_File=yes"
   else
     eval "$as_ac_File=no"
   fi
   fi
   

Becomes:

   if eval "test "${$as_ac_File+set}" = set"; then
     echo $ECHO_N "(cached) $ECHO_C" >&6
   else
     test "$cross_compiling" = yes &&
     { { echo "$as_me:$LINENO: error: cannot check for file existence when cross compiling" >&5
   echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
     }
     eval "$as_ac_File=yes"
   fi
   

The section:

   if eval "test "${$as_ac_File+set}" = set"; then
     echo $ECHO_N "(cached) $ECHO_C" >&6
   else
     test "$cross_compiling" = yes &&
     { { echo "$as_me:$LINENO: error: cannot check for file existence when cross compiling" >&5
   echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
      { (exit 1); exit 1; }; }
   if test -r "$dir/include/readline.h"; then
     eval "$as_ac_File=yes"
   else
     eval "$as_ac_File=no"
   fi
   fi
   

Becomes:

   if eval "test "${$as_ac_File+set}" = set"; then
     echo $ECHO_N "(cached) $ECHO_C" >&6
   else
     test "$cross_compiling" = yes &&
     { { echo "$as_me:$LINENO: error: cannot check for file existence when cross compiling" >&5
   echo "$as_me: error: cannot check for file existence when cross compiling" >&2;}
     }
     eval "$as_ac_File=yes"
   fi
   

The previous changes are already performed in the following modified sqlite-2.8.17-configure (Should be renamed as configure) file.

Once the Makefile is produced, open it and apply the following changes:

The line:

BCC = arm-unknown-linux-gnu-gcc -g -O2
Becomes:
BCC = gcc -g -O2

Step 3

To compile and install sqlite, type successively make and make install. You should now find the directories and files listed above under /opt/external_packages/sqlite/2.8.17/compiled/xxx-yyy-linux-gnu.

Cross compiling SQLite 3.5.6

Step 1

Install the sqlite software by untaring it in the /opt/external_packages/sqlite/3.5.6/source folder and then cd to this directory.

Note:

All the below directories and files must be moved from the sqlite-3.5.6 folder to the parent folder (/opt/external_packages/sqlite/3.5.6/source)

Step 2

We want to cross compile sqlite with its default parameters then we configure it by typing:

./configure --host=xxx-yyy-linux-gnu --prefix=/opt/external_packages/sqlite/3.5.6/compiled/xxx-yyy-linux-gnu

Step 3

To compile and install sqlite, type successively make and make install. You should find the directories and files listed above under /opt/external_packages/sqlite/3.5.6/compiled/xxx-yyy-linux-gnu.

With QEMU

This section is not yet available.

Download

  • arm-sqlite-3.5.4-tar.gz
  • arm-sqlite-2.8.17-tar.gz
  • sqlite-2.8.17-configure

You may consult this section if you want to download the SQLite database engine already cross compiled for your target platform if it is available.
You can also contribute to expand the crosscompile.org community by uploading your own results in the case a given platform is not already supported.

Supported platforms

  • arm-unknown-linux-gnu


Written by David Sayada.

Edit History Last Modified March 10, 2008