How to Cross Compile Zlib Compression Library - Step by Step Guide

Introduction

This tutorial introduces the way to cross compile the Zlib compression library 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 web site dealing with the Zlib compression library can be reached at the following URL: http://www.zlib.net/. The latest available release at the time of writing of this tutorial is 1.2.3 (July 18 2005).

This release can be downloaded by typing the following command:

US mirror:
wget http://www.zlib.net/zlib-1.2.3.tar.gz

France mirror:
wget http://www.gzip.org/zlib/zlib-1.2.3.tar.gz

Sourceforge:
wget http://prdownloads.sourceforge.net/libpng/zlib-1.2.3.tar.gz?download

Overview

zlib is designed to be a free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system. The zlib data format is itself portable across platforms. Unlike the LZW compression method used in Unix compress(1) and in the GIF image format, the compression method currently used in zlib essentially never expands the data. (LZW can double or triple the file size in extreme cases.) zlib's memory footprint is also independent of the input data and can be reduced, if necessary, at some cost in compression.

Prerequisites

To cross compile the Zlib compression library, 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 general, Expat does not need specific dependencies.

Cross compilation process

Using a cross compiler

Assumptions

During the process, we will refer to the cross toolchain with xxx-yyy-linux-gnu prefix. For example, the C compiler is called xxx-yyy-linux-gnu-gcc. That is applied on all the tools and utilities such gcc, g++, ar, ld, etc.

We will also assume that 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 assume that it is intalled in the following folder:
/opt/crosstool/gcc-4.1.2-glibc-2.3.2/xxx-yyy-linux-gnu.

Organization

As mentioned above, the release being cross compiled is 1.2.3. The tar.gz file will then be extracted under the /opt/external_packages/zlib/1.2.3/source folder and the results of the compilation will be stored under the /opt/external_packages/zlib/1.2.3/compiled/xxx-yyy-linux-gnu folder.

The files resulting from the cross compilation are listed here:


.:
total 12
drwxr-xr-x 2 root root 4096 2008-02-07 15:36 include
drwxr-xr-x 2 root root 4096 2008-02-07 15:36 lib
drwxr-xr-x 3 root root 4096 2008-02-07 15:36 share

./include:
total 84
-rw-r--r-- 1 root root  9544 2008-02-07 15:36 zconf.h
-rw-r--r-- 1 root root 66188 2008-02-07 15:36 zlib.h

./lib:
total 112
-rwxr-xr-x 1 root root 109708 2008-02-07 15:36 libz.a

./share:
total 4
drwxr-xr-x 3 root root 4096 2008-02-07 15:36 man

./share/man:
total 4
drwxr-xr-x 2 root root 4096 2008-02-07 15:36 man3

./share/man/man3:
total 8
-rw-r--r-- 1 root root 4486 2008-02-07 15:36 zlib.3

Cross compilation

Step 1

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

Note:

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

adler32.c
compress.c
deflate.c
gzio.c
inffixed.h
Makefile
old
trees.h
zlib.3
algorithm.txt
configure
deflate.h
INDEX
inflate.c
Makefile.in
projects
uncompr.c
zlib.h
amiga
contrib
example.c
infback.c
inflate.h
make_vms.com
qnx
win32
zutil.c
as400
crc32.c
examples
inffast.c
inftrees.c
minigzip.c
README
zconf.h
zutil.h
ChangeLog
crc32.h
FAQ
inffast.h
inftrees.h
msdos
trees.c
zconf.in.h

Step 2

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

./configure --prefix=/opt/external_packages/zlib/1.2.3/compiled/xxx-yyy-linux-gnu

Once it is done, it is necessary to edit the produced Makefile and to proceed to the following changes:

CC=gcc

becomes

CC=xxx-yyy-linux-gnu-gcc

LDSHARED=gcc

becomes

LDSHARED=xxx-yyy-linux-gnu-gcc

CPP=gcc -E

becomes

CPP=xxx-yyy-linux-gnu-gcc -E

AR=ar rc

becomes

AR=xxx-yyy-linux-gnu-ar rc

RANLIB=ranlib

becomes

RANLIB=xxx-yyy-linux-gnu-ranlib

Step 3

To compile and install zlib, type successively make and make install. You should find the directories and files listed in section IV.1.2 under /opt/external_packages/zlib/1.2.3/compiled/xxx-yyy-linux-gnu.

With QEMU

This section is not yet available.

Download

  • arm-zlib-1.2.3-tar.gz

You may consult this section if you want to download the Zlib compression library 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 August 16, 2008