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
- 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
- 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
- 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.
- Change to “Release” configuration.
- Remove “ZLIB_WINAPI;” from the “zlibstat” project’s property page: “Configuration Properties → C/C++ → Preprocessor → Preprocessor Definitions“
- Build the solution.
- 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
- 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"
- Copy the header files.
xcopy "C:\Users\%USERNAME%\Downloads\lib\zlib-1.2.7\*.h" "C:\workspace\lib\zlib\zlib-1.2.7\zlib"
- 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"
- 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
- Create a new project, “LibTest” in MSVC 2012.
- Explicitly add the zlib library to the project: Project → Properties →Linker → Input → Additional Dependencies = “zlibstat.lib;”
- 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“
Pingback: Building Static PoDoFo 0.9.1 with MSVC 2012 | Hyperfocus
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?
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.
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.