Ch1.7: MSVC (Windows Only)
What Is MSVC?
MSVC (Microsoft Visual C++) is Microsoft’s official C and C++ compiler, included with Visual Studio. It is the default compiler for Windows development and is tightly integrated with the Windows SDK, Microsoft’s debugger, and the Visual Studio IDE.
Install Visual Studio (MSVC)
If you are on Windows, you can use Microsoft’s official C++ compiler: MSVC (Microsoft Visual C++), which is included with Visual Studio.
We recommend installing Visual Studio Insider instead of the stable channel. The Insider version provides the newest MSVC toolchain, updated headers, and the latest C++ features.
Download Visual Studio Insider here:
https://visualstudio.microsoft.com/insiders/
To install:
- Download and run the Visual Studio Installer
- Select Desktop development with C++
- Install the MSVC toolchain and Windows SDK
After installation, you must use the Visual C++ Developer Command Prompt for your architecture:
- x64 Native Tools Command Prompt (most common)
- x86 Native Tools Command Prompt
- ARM64 Native Tools Command Prompt (Surface Pro 11, Surface Laptop 7, etc.)
These command prompts configure all required environment variables such as INCLUDE,
LIB, and PATH.
Running cl.exe from a normal command prompt will not work correctly.
Compile a Windows Binary Using MSVC
Once inside the correct Developer Command Prompt, you can compile a Windows binary using:
cl /EHsc /Ox hello.cpp /std:c++latest /GL /MD
This produces hello.exe, linked against the Microsoft Visual C++ runtime.
You Do Not Need MSVC to Create MSVC-Compatible Windows Binaries
Although MSVC is the official Microsoft compiler, you do not need MSVC to create Windows binaries that use the MSVC runtime.
In Ch1.4 and Ch1.5, we showed how to use Clang with the windows-msvc-sysroot package. This sysroot contains all MSVC headers and libraries, extracted and packaged by the author of this tutorial.
With this sysroot, you can build MSVC-compatible Windows binaries on:
- Linux
- macOS
- Android
- FreeBSD
- Windows
- Any platform where Clang runs
This means you do not need Visual Studio or MSVC installed at all.
Many C++ developers consider MSVC’s compiler front-end and diagnostics inferior to Clang and GCC. You are welcome to try MSVC, but for this tutorial we strongly encourage using Clang or GCC instead.
Why This Tutorial Uses Clang
In this tutorial, we primarily use Clang because:
- It cross-compiles easily across all platforms
- It can target Windows, Linux, macOS, Android, and WebAssembly
- It integrates cleanly with
windows-msvc-sysroot - It produces WebAssembly binaries
- It supports advanced tools such as WebAssembly Memory Tagging
If you have not yet read Ch1.4 and Ch1.5, we recommend reviewing them to understand how Clang cross-compilation works.