Building Static zlib v1.2.7 with MSVC 2012

This post will explain how to obtain and build the zlib C programming library from source, using MS Visual Studio 2012 on Windows 7. The result will be a static release version that you can use in your C/C++ projects for data compression, or as a dependency for other libraries.

Downloads

The Environment

  1. Decompress and untar the library with 7zip and you’ll end up with a directory path similar to this:
    C:\Users\%USERNAME%\Downloads\lib\zlib-1.2.7\

Building

  1. Modify “libs\zlib-1.2.7\contrib\masmx86\bld_ml32.bat,” adding “/safeseh” to the following two lines.
    Before:

    ml /coff /Zi /c /Flmatch686.lst match686.asm
    ml /coff /Zi /c /Flinffas32.lst inffas32.asm
    

    After:

    ml /safeseh /coff /Zi /c /Flmatch686.lst match686.asm
    ml /safeseh /coff /Zi /c /Flinffas32.lst inffas32.asm
    
  2. Open the solution file that came with the package, “libs\zlib-1.2.7\contrib\vstudio\vc10\zlibvc.sln,” and upgrade the solution file if necessary to MSVC 2012.
  3. Change to “Release” configuration.
  4. Remove “ZLIB_WINAPI;” from the “zlibstat” project’s property page: “Configuration Properties → C/C++ → Preprocessor → Preprocessor Definitions
  5. Build the solution.
  6. The new static library file is created in a new subfolder:
    C:\Users\%USERNAME%\Downloads\lib\zlib-1.2.7\contrib\vstudio\vc10\x86\ZlibStatRelease\zlibstat.lib

Installing

  1. Create a place for the zlib library with “zlib” and “lib” subfolders.
    mkdir "C:\workspace\lib\zlib\zlib-1.2.7\zlib"
    mkdir "C:\workspace\lib\zlib\zlib-1.2.7\lib"
    
  2. Copy the header files.
    xcopy "C:\Users\%USERNAME%\Downloads\lib\zlib-1.2.7\*.h" "C:\workspace\lib\zlib\zlib-1.2.7\zlib" 
  3. Copy the library file.
    xcopy "C:\Users\%USERNAME%\Downloads\lib\zlib-1.2.7\contrib\vstudio\vc10\x86\ZlibStatRelease\zlibstat.lib" "C:\workspace\lib\zlib\zlib-1.2.7\lib\zlibstat.lib" 
  4. Add the include and lib paths to the default project property page in MSVC 2012:
    View → Other Windows → Property Manager → Release/Debug → Microsoft.Cpp.Win32.user
    .
    Be sure to save the property sheet so that the changes take effect.

Testing

  1. Create a new project, “LibTest” in MSVC 2012.
  2. Explicitly add the zlib library to the project: Project → Properties →Linker → Input → Additional Dependencies = “zlibstat.lib;”
  3. Create a source file in the project and copy the “zpipe.c” example code.

Build the project. It should compile and link successfully.

Potential Issues

These are some of the problems that you might run into while trying to build zlib.

LNK2026: module unsafe for SAFESEH image

Need to include support for safe exception handling. Modify “libs\zlib-1.2.7\contrib\masmx86\bld_ml32.bat,” adding “/safeseh” to the following two lines.
Before:

ml /coff /Zi /c /Flmatch686.lst match686.asm
ml /coff /Zi /c /Flinffas32.lst inffas32.asm

After:

ml /safeseh /coff /Zi /c /Flmatch686.lst match686.asm
ml /safeseh /coff /Zi /c /Flinffas32.lst inffas32.asm

LNK2001: unresolved external symbol _inflateInit_

The code is trying to link with the DLL version of the library instead of the static version. Remove “ZLIB_WINAPI;” from the “zlibstat” project’s property page: “Configuration Properties → C/C++ → Preprocessor → Preprocessor Definitions

About these ads
This entry was posted in Programming and tagged , , , , . Bookmark the permalink.

4 Responses to Building Static zlib v1.2.7 with MSVC 2012

  1. Pingback: Building Static PoDoFo 0.9.1 with MSVC 2012 | Hyperfocus

  2. Allen says:

    I followed exactly the same steps to build zlib 1.2.7 with MSVC2012, but I run into unistd.h header can not be found error:
    c:\mysql-build\zlib\zconf.h(447) : fatal error C1083: Cannot open include file: ‘unistd.h’: No such file or directory

    This error bugs me with VS2005 and VS2010 environment, but migration to using MSVC2012 does not provide the answer.

    How did you manage to escape this error? Any suggestion, hint?

  3. Jack says:

    Haven’t tried this with 2005 or 2010. unistd.h is a header file for OS POSIX compliance and is used on Linux systems. It looks like somehow, zlib was trying to build for Linux. Some possibilities: 1) Comment out that line and see what happens. 2) Try following the blog post again from scratch. 3) Create your own unistd.h file with Windows equivalents of what zlib wants. 4) Install MinGW and tinker with the included libraries.

    • Allen says:

      I did follow the blog post from scratch with MS2012, and got stuck on the same unistd.h line.

      After some research, this is my work around:

      Get a copy libgw32c, modify the zconf.h to replace unistd.h with unistdx.h, and add libgw32c/include/winx to the header path (avoid libgw32c/include/glibc). This works for all VS2005/2010/2012.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s