Building Static zlib v1.2.7 with MSVC 2012
Posted by Hemprasad Y. Badgujar on January 22, 2015
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.
- Decompress anduntar the library with7zip and you’ll end up with a directory path similar to this:
- Modify “libs\zlib-1.2.7\contrib\masmx86\bld_ml32.bat,” adding “/safeseh” to the following two lines.
ml /coff /Zi /c /Flmatch686.lst match686.asm
ml /coff /Zi /c /Flinffas32.lst inffas32.asm
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 fileis created in a newsubfolder:
- Create a place for the zlib library with “zlib” and “lib”subfolders.
- Copy the header files.
- Copy the library file.
- 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.
- 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.
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.
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”