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
You may have to edit Makefile.gcc if some headers can't be found.
CC = $(PREFIX)gcc -I/mingw/include RC = $(PREFIX)windres -I/mingw/include
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 or this post). Compile 64-bit zlib as follows:
make -f win32/Makefile.gcc CC=x86_64-w64-mingw32-gcc RC=x86_64-w64-mingw32-windres
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
Compiling 32-bit/64-bit Zlib with TDM-GCC MinGW-w64
As far as I know, TDM-GCC does not come with MSYS. So we'll use the Command Prompt (cmd.exe). To build a 32-bit zlib, we need to edit win32/Makefile.gcc so that LOC and RCFLAGS have the following values.
LOC = -m32 -mms-bitfields RCFLAGS = -F pe-i386 --define GCC_WINDRES
To build a 64-bit zlib, we need different values for LOC and RCFLAGS.
LOC = -m64 -mms-bitfields RCFLAGS = -F pe-x86-64 --define GCC_WINDRES
Now, let's run mingw32-make on Makefile.gcc.
C:\MinGW64\bin\mingw32-make.exe -f win32\Makefile.gcc
We get these files: zlib1.dll, libz.a, libz.dll.a.
Cross-compile Zlib from Linux
To cross-compile Zlib for Window from inside Linux, just type this command (for Debian Wheezy and Later):
make -f win32/Makefile.gcc CC=/usr/bin/i686-w64-mingw32-gcc RC=/usr/bin/i686-w64-mingw32-windres
For Debian squeeze and previous versions, you should have installed the mingw32-runtime, mingw32-binutils and mingw packages. The command will be a bit different.
make -f win32/Makefile.gcc CC=/usr/bin/i586-mingw32msvc-gcc RC=/usr/bin/i586-mingw32msvc-windres
Then, install the generated files as shown above. Read this post for more information on setting up MinGW on Linux.
libz.la and zlib.pc
Depending on situations, you may have to create libz.la and zlib.pc. libz.la is a libtool library file and it is used when the compilation process involves the use of libtool. I found that libz.la is needed when I try to compile a shared library for the latest version (1.6.0) of libpng. Either write it on your own with a text editor or download one from the Internet. The following is a sample libz.la that should be put in /mingw/lib.
# libz.la - a libtool library file # Generated by ltmain.sh (GNU libtool) 2.2.6b # # Please DO NOT delete this file! # It is necessary for linking the library. # The name that we can dlopen(3). dlname='../bin/zlib1.dll' # Names of this library. library_names='libz.dll.a' # The name of the static archive. old_library='libz.a' # Linker flags that can not go in dependency_libs. inherited_linker_flags='' # Libraries that this one depends upon. dependency_libs='' # Names of additional weak libraries provided by this library weak_library_names='' # Is this an already installed library? installed=yes # Should we warn about portability when linking against -modules? shouldnotlink=no # Files to dlopen/dlpreopen dlopen='' dlpreopen='' # Directory that this library needs to be installed in: libdir='/mingw/lib'
The following is a sample zlib.pc and should be placed in /mingw/lib/pkgconfig.
prefix=/mingw
exec_prefix=/mingw/bin
libdir=/mingw/lib
sharedlibdir=/mingw/lib
includedir=/mingw/include
Name: zlib
Description: zlib compression library
Version: 1.2.7
Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz
Cflags: -I${includedir}

