Sunday, June 28, 2009

MinGW: Compiling Zlib

Zlib is an essential compression library that's required by most open-source software. Download the zlib source and unpack it with unzip or 7zip. Then, compile Zlib like this:


make -f win32/Makefile.gcc

Manually copy the files as follows:


cp -iv zlib1.dll /mingw/bin
cp -iv zconf.h zlib.h /mingw/include
cp -iv libz.a libz.dll.a /mingw/lib




Building 64-bit zlib


Install MinGW64 (as shown in this post). Edit win32/Makefile.gcc so it contains the following lines.



CC=x86_64-w64-mingw32-gcc
RC=x86_64-w64-mingw32-windres


Run the following command to compile 64-bit zlib.


make -f win32/Makefile.gcc


Then, install the files.


make -f win32/Makefile.gcc install BINARY_PATH=/mingw/bin INCLUDE_PATH=/mingw/include LIBRARY_PATH=/mingw/lib SHARED_MODE=1


Importing Official Zlib Build


Normally, you don't have to compile zlib yourself as the official precompiled zlib1.dll is distributed from zlib.net. For convenience, we can just use the precompiled package zlib124-dll.zip from zlib.net. Unpack zlib124-dll.zip to an empty folder. Copy the files to the /mingw tree as follows:


/mingw/bin/zlib1.dll

/mingw/include/zconf.h

/mingw/include/zlib.h

/mingw/lib/zdll.exp

/mingw/lib/zdll.lib

/mingw/lib/zlib.def

To import zlib.dll, we have to create zlib.def.


pexports -h /mingw/include/zlib.h /mingw/bin/zlib1.dll | tr -d '@[:digit:]$' > /mingw/lib/zlib.def

Change to /mingw/lib and create the import library libz.dll.a like this:


cd /mingw/lib
dlltool --def /mingw/lib/zlib.def --dllname zlib1.dll --output-lib /mingw/lib/libz.dll.a


Cross-compile Zlib from Linux


This section describes the steps to cross-compile Zlib for Window from inside Linux. First, install the mingw32-runtime, mingw32-binutils and mingw packages. Create a mingw tree at /opt/mingw:



mkdir /opt/mingw
mkdir /opt/mingw/bin
mkdir /opt/mingw/include
mkdir /opt/mingw/lib


Then, set up the following environment variables:



AR="/usr/bin/i586-mingw32msvc-ar rc"
CC=/usr/bin/i586-mingw32msvc-gcc
CPP=/usr/bin/i586-mingw32msvc-cpp
CPPFLAGS="-I/usr/i586-mingw32msvc/include -I/opt/mingw/include"
CXX=/usr/bin/i586-mingw32msvc-g++
LDFLAGS="-L/usr/i586-mingw32msvc/lib -L/opt/mingw/lib"
PATH=/usr/i586-mingw32msvc/bin:$PATH
RANLIB=/usr/bin/i586-mingw32msvc-ranlib
RC=/usr/bin/i586-mingw32msvc-windres
WINDRES=/usr/bin/i586-mingw32msvc-windres

export AR CC CPP CPPFLAGS CXX LDFLAGS PATH RANLIB RC WINDRES


Then, run the commands below:



/configure --prefix=/opt/mingw
make
make install


At last, create zlib1.dll:



/usr/bin/i586-mingw32msvc-gcc -shared -o /opt/mingw/bin/zlib1.dll -Wl,--out-implib=/opt/mingw/lib/libz.dll.a [!em]*.o

4 comments:

  1. Wonderful help!
    I built zlib easily for qt/mingw environment following your howto.

    Thank you very much.

    ReplyDelete
  2. Trying to get zlib installed for mingw on Ubuntu but when i run:
    make -f win32/Makefile.gcc

    I'm getting error:
    unrecognized option '--out-implib'

    unfortunately I'm a total linux noob and can't find any other hints on what the problem might be, any suggestions much appreciated.

    ReplyDelete
  3. instead of the manual copy, both versions 1.2.5 and 1.2.6 support using the flags BINARY_PATH LIBRARY_PATH and INCLUDE_PATH. e.g.:

    make install -f win32/Makefile.gcc BINARY_PATH=/mingw/bin INCLUDE_PATH=/mingw/include LIBRARY_PATH=/mingw/lib

    Danny - you're trying to use a MinGW build on Ubuntu which is not going to work, try './configure' instead.

    ReplyDelete

About This Blog

KBlog logo This blog seeks to provide a collection of interesting pictures found on the Web. Thanks for visiting the blog and posting your comments.

© Contents by KBlog

© Blogger template by Emporium Digital 2008

Followers

Total Pageviews

Powered By Blogger