Categories: ZIP

Building zlib for Windows

Ensure these components are installed before building zlib:

  1. The MSVC v140 – VS 2015 C++ build tools (v14.00) individual component in the Visual Studio Installer
  2. The Windows 8.1 SDK from the Windows SDK and emulator archive

Once these are installed, run the commands below in a Visual Studio 2022 Developer Command Prompt to build zlib for Windows. The x64 directory will contain zlibwapi.dll, which can be renamed to zlib.dll according to zlib/readme.txt (from the latest commit as of this post).

git clone https://github.com/madler/zlib
cd zlib/contrib/vstudio/vc14
git switch --detach v1.2.11
msbuild zlibvc.vcxproj /p:Configuration=Release /p:Platform=x64
msbuild zlibvc.vcxproj /p:Configuration=Release /p:Platform=Win32

mkdir C:\dev\software\zlib\win32
copy x86\ZlibDllRelease\zlibwapi.dll C:\dev\software\zlib\win32\zlib.dll
copy x86\ZlibDllRelease\zlibwapi.lib C:\dev\software\zlib\win32\zdll.lib

Why Build zlib?

As part of Tracking Down Missing Headers in LLVM for Windows, I ran into NSIS compiler errors and decide to create a debug build of NSIS to debug them myself since there was no definitive solution online. Turns out, zlib is one of the prereqs for NSIS as per Code / [r7368] /NSIS/branches/WIN64/INSTALL (sourceforge.net).

Unfortunately (or maybe fortunately?), I didn’t see any binaries at zlib. There is a link to the zlib GitHub repo though and zlib/DLL_FAQ.txt at master · madler/zlib (github.com) says to review the zlib site for an alternative download location. Sure enough, it does have a link to zlib for Windows 9x/NT/2000/XP/2003 (DLL version, plus related utilities). That doesn’t inspire much confidence in the binaries though… Might as well build them myself.

Investigating How to Build zlib

Open a Visual Studio Developer Command Prompt then build the project I saw in the docs:

git clone https://github.com/madler/zlib
cd zlib/contrib/vstudio/vc14
msbuild zlibvc.vcxproj

There are other prereqs, apparently:

MSBuild version 17.3.1+2badb37d1 for .NET Framework
Build started 9/29/2022 11:01:01 AM.
Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" on node 1 (default targets).
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Microsoft\VC\v170\Microsoft.CppBuild.targets(460,5): error MSB8020: The build tools for Visual Studio 2015 (Platform Toolset = 'v140') cannot b
e found. To build using the v140 build tools, please install Visual Studio 2015 build tools.  Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the
 solution, and then selecting "Retarget solution". [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
Done Building Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" (default targets) -- FAILED.


Build FAILED.

This component is 3.63 GB but that’s not the only prereq! This toolset requires the Windows 8.1 SDK! As per visual studio 2019 – VS2019 without Windows 8.1 SDK – Stack Overflow, it needs to be downloaded from the Windows SDK and emulator archive

D:\dev\repos\zlib\contrib\vstudio\vc14> msbuild zlibvc.vcxproj
MSBuild version 17.3.1+2badb37d1 for .NET Framework
Build started 9/29/2022 11:23:13 AM.
Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" on node 1 (default targets).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\Win32\PlatformToolsets\v140\Toolset.targets(34,5): error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Wi
ndows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
Done Building Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" (default targets) -- FAILED.


Build FAILED.

After all that effort, the reward is the error below. Searching for “bat” is helpful: Issues · madler/zlib (github.com)

D:\dev\repos\zlib\contrib\vstudio\vc14> msbuild zlibvc.vcxproj
MSBuild version 17.3.1+2badb37d1 for .NET Framework
Build started 9/29/2022 11:51:10 AM.
Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" on node 1 (default targets).
PrepareForBuild:
  Creating directory "x86\ZlibDllDebug\Tmp\".
  Creating directory "x86\ZlibDllDebug\Tmp\zlibvc.tlog\".
InitializeBuildStatus:
  Creating "x86\ZlibDllDebug\Tmp\zlibvc.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
PreBuildEvent:
  cd ..\..\masmx86
  bld_ml32.bat
  :VCEnd
  The system cannot find the path specified.
  'bld_ml32.bat' is not recognized as an internal or external command,
  operable program or batch file.
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: The command "cd ..\..\masmx86 [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: bld_ml32.bat [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(123,5): error MSB3073: :VCEnd" exited with code 9009. [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
Done Building Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" (default targets) -- FAILED.


Build FAILED.

To find out which commit removed this batch file, run this from the root of the repo:

git log --full-history -1 -- contrib/masmx86/bld_ml32.bat

This looks like really bad development on the zlib repo. Removing scripts without removing outdated documentation, much less documenting the new way to build. This is Please fix 1.2.12 compile · Issue #631 · madler/zlib (github.com). Instead of using the workarounds there, just build 1.2.11 and let the zlib folks deal with that mess.

git switch --detach v1.2.11
cd zlib/contrib/vstudio/vc14
msbuild zlibvc.vcxproj

Linking fails with error LNK2026:

match686.obj : error LNK2026: module unsafe for SAFESEH image. [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
inffas32.obj : error LNK2026: module unsafe for SAFESEH image. [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
     Creating library x86\ZlibDllDebug\zlibwapi.lib and object x86\ZlibDllDebug\zlibwapi.exp
x86\ZlibDllDebug\zlibwapi.dll : fatal error LNK1281: Unable to generate SAFESEH image. [D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj]
Done Building Project "D:\dev\repos\zlib\contrib\vstudio\vc14\zlibvc.vcxproj" (default targets) -- FAILED.

This option is on by default as per /SAFESEH (Image has Safe Exception Handlers) | Microsoft Learn. However, it only affects the x86 platform. I’m only interested in x64 so this command suffices:

msbuild zlibvc.vcxproj /p:Configuration=Release /p:Platform=x64

The compiler generates the zlibwapi DLL in the x64 directory. Unfortunately, NSIS requires a Win32 build so run this as well:

msbuild zlibvc.vcxproj /p:Configuration=Release /p:Platform=Win32

Deploy the binaries to the desired location, e.g.

mkdir C:\dev\software\zlib\win32
copy x86\ZlibDllRelease\zlibwapi.dll C:\dev\software\zlib\win32\zlib.dll
copy x86\ZlibDllRelease\zlibwapi.dll C:\dev\software\zlib\x64\zlib.dll

Article info




Leave a Reply

Your email address will not be published. Required fields are marked *