MSVC cl.exe and clang-cl
Purpose
Guide agents through Windows C/C++ compilation: MSVC cl.exe, clang-cl as MSVC-compatible driver, MSBuild project settings, and runtime library choices.
Triggers
- "How do I compile with MSVC from the command line?"
- "What is the MSVC equivalent of
-O2?" - "/MT vs /MD — which do I use?"
- "How do I use clang-cl instead of cl.exe?"
- "I'm getting LNK errors on Windows"
- "How do I generate PDB files?"
Workflow
1. Set up the environment
MSVC requires the Visual Studio environment variables. Use the Developer Command Prompt or set up manually:
In CMake, use the "Visual Studio 17 2022" generator or pass -DCMAKE_GENERATOR="Visual Studio 17 2022".
2. Flag translation: GCC → MSVC
3. Runtime library selection
This is the most common source of LNK errors on Windows.
Rule: All objects and libraries in a link must use the same runtime. Mixing /MT and /MD causes LNK2038: mismatch detected.
4. clang-cl
clang-cl is Clang's MSVC-compatible driver. It accepts cl.exe-style flags and can replace cl.exe in most MSBuild/CMake projects.
Minimum Clang version for MSVC STL compatibility: Clang 8+. For C++20 features with MSVC STL: Clang 14+.
Source: https://learn.microsoft.com/en-us/cpp/build/clang-support-msbuild
5. PDB files and debugging
For WinDbg or VS debugger, ensure the PDB is alongside the executable or set the symbol path.
6. Common LNK errors
7. Useful cl.exe flags
For flag details, see references/flags.md [blocked].
Related skills
- Use
skills/compilers/clangfor clang-cl's Clang-native diagnostics - Use
skills/build-systems/cmakefor CMake toolchain configuration on Windows - Use
skills/debuggers/lldbor WinDbg for debugging MSVC-built binaries


