Compiling zlib.lib on Windows

Update: see comments section for a more up to date method of doing this. Don’t forget to run contrib\masmx86\bld_ml32.bat from the Visual Studio Command Prompt before compiling this way though.

This article still applies if you want to compile on older versions e.g. MSVC 6.0, for older projects or if you have problems with the new method.


zlib is the standard for lossless data compression. The DEFLATE compression algorithm is the basis for just about every lossless compression format out there, including “zip” and “gzip”, which is itself part of zlib.

There are two ways that it can be used from C/C++ projects in Windows.

Firstly, it can be used by dynamic linking (dll). This means using zdll.lib and shipping the appropriate version of zlib1.dll with your project. This is not a problem, as Windows versions of both of these files are provided.

The second way is to use static linking. That is, having all of the code in one .lib file and compiling it into your exe so that you do not have to distribute zlib1.dll. This means compiling zlib.lib.

In version 1.2.4 of zlib, a “projects” directory was provided, with a Microsoft Visual C++ 6.0 project. However, it seems that version 1.2.5 has not included this project. This means that the best solution is to go and get the 1.2.4 source and compile it yourself. However, the zlib project seems to be kept inside the libpng project on sourceforge.net, so it is not immediately obvious where to find older versions of the zlib source code.

zlib 1.2.4 source (zip)

Extract the zip, open projects\visualc6\zlib.dsp in Visual Studio (I used 2005) and compile “LIB Release” (and optionally “LIB Debug”)

Copy zlib.h and zconf.h from “include” to your Visual Studio “include” directory, and zlib.lib (and zlibd.lib if you made it) to your Visual Studio “lib” directory.

On 64 bit Windows, with Visual Studio 2005, this is “C:\Program Files (x86)\Microsoft Visual Studio 8\VC\” so adjust for your version of Visual Studio.

You now just need to add “zlib.lib” to your “Linker -> Input -> Additional Dependencies” line in your C++ project configuration to use it (and optionally zlibd.lib for the debug version).

Tags:

3 Responses to “Compiling zlib.lib on Windows”

  1. PinoyUser says:

    Thanks for sharing this! Ive been trying the whole day making my VC++ project to work with zlib. This saved my day :) Thank you

  2. moshiko says:

    goto http://www.zlib.net/
    Download source code and extract it.
    open zlib/contrib/vstudio/vc10 and open the sln file.
    Now change the project to lib instead of dll and compile it.

  3. George Helyar says:

    Thanks for posting this but you missed the step

    zlib\contrib\masmx86\bld_ml32.bat

    or

    zlib\contrib\masmx64\bld_ml64.bat

    from the Visual Studio Command Prompt

    and changing the project to lib did not seem to work correctly.

    However, zlib\contrib\vstudio\vc10\x86\ZlibStatRelease\zlibstat.lib is output without needing to change the project to lib.

    These project files are also only provided for Visual Studio 2008 and 2010. It is possible to edit the project files to work in .net or 2005 but this may cause problems.

Leave a Reply