Skip to content

Building Zig on Windows

Ryan Liptak edited this page Oct 11, 2022 · 110 revisions

Note: If you want the latest build of Zig on Windows, you can get it from the download page.


Option 1: Use the Windows Zig Compiler Dev Kit

This one has the benefit that LLVM, LLD, and Clang are built in Release mode, while your Zig build has the option to be a Debug build. It also works completely independently from MSVC so you don't need it to be installed.

If this does not work, the URL for the archive the current master build uses can be found in the pipelines.yml file.

Please consider sponsoring Zig. ❤️

This zip file contains:

  • An older Zig installation.
  • LLVM, LLD, and Clang libraries (.lib and .h files), version 15.0.0, built in Release mode.

Unzip the dev kit and then in cmd.exe in your Zig source checkout:

$DEVKIT\bin\zig.exe build -p stage3 --search-prefix $DEVKIT --zig-lib-dir lib -Denable-stage1 -Dstatic-llvm -Duse-zig-libcxx -Dtarget=x86_64-windows-gnu

Replace $DEVKIT with the path to the folder that you unzipped after downloading it from the link above.

Append -Drelease for a Release build.

If you get an error building at this step, it is most likely that the Zig installation inside the dev kit is too old, and the dev kit needs to be updated. In this case one more step is required:

  1. Download the latest master branch zip file.
  2. Unzip, and try the above command again, replacing the path to zig.exe with the path to the zig.exe you just extracted, and also replace the lib\zig folder with the new contents.

You now have the zig.exe binary at stage3\bin\zig.exe and you can run the tests:

stage3\bin\zig.exe build test

This can take a long time. For tips & tricks on using the test suite, see Contributing.

Option 2: Using CMake and Microsoft Visual Studio

This one has the benefit that changes to the language or build system won't break your dev kit. This option can be used to upgrade a dev kit.

First, build LLVM, LLD, and Clang from source using CMake and Microsoft Visual Studio. Or, skip this step using this pre-built binary tarball:

Please consider sponsoring Zig. ❤️

Unzip this file to a directory with no spaces, such as C:\Users\Andy\. It contains a single directory, so when you do this the full path will be e.g. C:\Users\Andy\llvm+clang+lld-13.0.1-x86_64-windows-msvc-release-mt\.

Install Build Tools for Visual Studio 2019. Be sure to select "Desktop development with C++" when prompted.

  • You must additionally check the optional component labeled C++ ATL for v142 build tools.

Install CMake.

Use git to clone the zig repository to a path with no spaces, e.g. C:\Users\Andy\zig.

Using the start menu, run x64 Native Tools Command Prompt for VS 2019 and execute these commands, replacing C:\Users\Andy with the correct value.

mkdir C:\Users\Andy\zig\build-release
cd C:\Users\Andy\zig\build-release
"c:\Program Files\CMake\bin\cmake.exe" .. -Thost=x64 -G "Visual Studio 16 2019" -A x64 -DCMAKE_PREFIX_PATH=C:\Users\Andy\llvm+clang+lld-13.0.1-x86_64-windows-msvc-release-mt -DCMAKE_BUILD_TYPE=Release
msbuild -p:Configuration=Release INSTALL.vcxproj

You now have the zig.exe binary at bin\zig.exe and you can run the tests:

bin\zig.exe build test

This can take a long time. For tips & tricks on using the test suite, see Contributing.

Note: In case you get the error "llvm-config not found" (or similar), make sure that you have no trailing slash (/ or \) at the end of the -DCMAKE_PREFIX_PATH value.