Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt

Use this file to discover all available pages before exploring further.

cl.exe is the Microsoft C/C++ compiler and is the central tool in the MSVC toolchain. It preprocesses, compiles, and optionally links C and C++ source files into COFF object files (.obj) and executables. Compiler options are case-sensitive and may be prefixed with either / or -. This reference covers the most important options grouped by purpose, with real examples for common build scenarios.
cl.exe can only be run from a Visual Studio Developer Command Prompt or after calling vcvarsall.bat to configure the environment. See Building from the Command Line for environment setup instructions.

Language Standard

Control which C++ standard is used to compile your code with the /std option.
OptionStandardAvailable Since
/std:c++14ISO C++14VS 2015 Update 3
/std:c++17ISO C++17VS 2017 15.3
/std:c++20ISO C++20VS 2019 16.11
/std:c++23previewISO C++23 (preview)VS 2022 17.13
/std:c++latestLatest draft + experimental featuresVS 2015 Update 3
/std:c11ISO C11VS 2019 16.8
/std:c17ISO C17VS 2019 16.8
# Compile with C++20
cl /std:c++20 /EHsc main.cpp

# Use the latest draft features
cl /std:c++latest /EHsc main.cpp
To make __cplusplus report the correct value (rather than 199711L), also pass /Zc:__cplusplus alongside /std:c++17 or later. For example: cl /std:c++17 /Zc:__cplusplus /EHsc main.cpp.

Optimization

Optimization options control the trade-off between code size, speed, and compile time.
OptionDescriptionExpands To
/OdDisable optimization (default for Debug)
/O1Minimize size/Og /Os /Oy /Ob2 /GF /Gy
/O2Maximize speed (default for Release)/Og /Oi /Ot /Oy /Ob2 /GF /Gy
/OxMaximum optimization (superset of /O2)/Og /Oi /Ot /Oy /Ob2
/OgEnable global optimizations
/OiGenerate intrinsic functions
/OsFavor small code
/OtFavor fast code
/Ob0Disable inlining
/Ob1Inline only functions explicitly marked inline, __inline, or __forceinline
/Ob2Inline any function the compiler deems suitable (default under /O1 and /O2)
/OyOmit frame pointer (x86 only)
# Debug build — no optimization
cl /Od /Zi /EHsc main.cpp

# Release build — maximum speed
cl /O2 /EHsc /DNDEBUG main.cpp

# Minimize binary size (e.g., for embedded or size-constrained builds)
cl /O1 /EHsc /DNDEBUG main.cpp
See Optimizations for whole-program optimization (/GL) and profile-guided optimization (PGO).

Debug Information

Control the format and location of debugging symbols.
OptionDebug FormatPDB?Edit & Continue
(none)No debug infoNoNo
/Z7Full symbols embedded in .objNo (but linker can create one)No
/ZiSeparate PDB fileYesNo
/ZIPDB with Edit and Continue supportYesYes
# Standard debug build with PDB
cl /Zi /Od /EHsc main.cpp
link /DEBUG /PDB:myapp.pdb main.obj

# Edit and Continue support (x86/x64 only)
cl /ZI /Od /EHsc main.cpp

# Embed debug info in object files (useful for static library distribution)
cl /Z7 /Od /EHsc main.cpp
/ZI (Edit and Continue) is incompatible with /GL (Whole Program Optimization) and /Ox. Use /Zi for release builds that still need debuggable PDBs.

Warning Levels

OptionDescription
/W0Disable all warnings
/W1Severe warnings only
/W2Significant warnings
/W3Production quality (default)
/W4Informational warnings (recommended for new code)
/WallAll warnings including informational ones (very verbose)
/WXTreat all warnings as errors
/wd<nnnn>Disable specific warning number
/we<nnnn>Treat specific warning as an error
/wo<nnnn>Issue specific warning once
# Recommended for new projects
cl /W4 /WX /EHsc /std:c++17 main.cpp

# Disable a specific noisy warning (e.g., C4100: unreferenced formal parameter)
cl /W4 /wd4100 /EHsc main.cpp

# Treat one specific warning as an error
cl /W3 /we4715 /EHsc main.cpp

Runtime Library (CRT Linking)

The /MD, /MT, and /LD options control how the C Runtime Library (CRT) is linked. All modules in a program must use the same runtime library option.
OptionLibraryCRTUsage
/MDMSVCRT.libDynamic (DLL)Release builds linked to MSVCRT.dll
/MDdMSVCRTD.libDynamic (DLL) — DebugDebug builds, defines _DEBUG
/MTLIBCMT.libStaticSelf-contained release builds, no CRT DLL dependency
/MTdLIBCMTD.libStatic — DebugSelf-contained debug builds
/LDImplies /MT (unless /MD is also specified)Create a DLL
/LDdImplies /MT — DebugCreate a debug DLL
# Dynamic CRT — typical for applications and DLLs
cl /MD /EHsc main.cpp

# Static CRT — self-contained executable, no MSVCRT.dll dependency
cl /MT /EHsc main.cpp

# Debug build with dynamic CRT
cl /MDd /Zi /Od /EHsc main.cpp
Mixing runtime library options within a single linked binary (e.g., one .obj with /MD and another with /MT) causes linker errors and undefined behavior. Use the same option consistently across all source files and all libraries you link against.

Preprocessor Definitions

OptionDescription
/D<name>Define a preprocessor macro
/D<name>=<value>Define a macro with a value
/U<name>Undefine a macro
# Common release build defines
cl /DNDEBUG /DWIN32 /D_WINDOWS /EHsc main.cpp

# Define a version string
cl /DAPP_VERSION=\"2.1.0\" /EHsc main.cpp

# Define an integer constant
cl /DMAX_CONNECTIONS=100 /EHsc main.cpp

# Undefine a macro set by the system headers
cl /UUNICODE /EHsc main.cpp

Output File Control

OptionControlsExample
/Fe<name>Executable output file name/Fe:myapp.exe
/Fo<name>Object file output name or directory/Fo:obj\ or /Fo:main.obj
/Fp<name>Precompiled header file name/Fp:pch\stdafx.pch
/Fd<name>PDB file name (for compiler-generated PDB)/Fd:debug\vc143.pdb
/Fm<name>Map file name/Fm:myapp.map
/Fa<name>Assembly listing file/Fa:main.asm
# Place all output in a build directory
cl /EHsc /O2 /MD ^
   /Fe:build\myapp.exe ^
   /Fo:build\ ^
   src\main.cpp src\engine.cpp

Exception Handling

OptionDescription
/EHscEnable standard C++ exception handling with extern "C" assumed to not throw
/EHaEnable C++ exceptions and structured exception handling (SEH)
/EHsEnable C++ exceptions (assume extern "C" may throw)
/EHrAlways generate noexcept runtime termination checks
# Standard recommendation for most C++ code
cl /EHsc main.cpp
Always specify /EHsc for standard C++ code. Without an /EH option, stack-unwinding destructors may not be called on exception, leading to resource leaks.

RTTI (Run-Time Type Information)

OptionDescription
/GREnable RTTI — allows dynamic_cast and typeid (default: ON)
/GR-Disable RTTI — reduces binary size, cannot use dynamic_cast or typeid
# Explicitly enable RTTI (default)
cl /GR /EHsc main.cpp

# Disable RTTI for size-constrained builds
cl /GR- /EHsc main.cpp

Common Build Scenarios

A typical debug build: no optimization, full debug symbols, dynamic CRT debug library.
cl ^
  /std:c++17 ^
  /EHsc ^
  /GR ^
  /W4 ^
  /Od ^
  /ZI ^
  /MDd ^
  /DDEBUG ^
  /D_DEBUG ^
  /Fo:obj\debug\ ^
  /Fe:bin\debug\myapp.exe ^
  src\main.cpp src\engine.cpp src\utils.cpp

Compiler Options Quick Reference

CategoryOptionSummary
Language/std:c++17C++17 standard
Language/std:c++20C++20 standard
Language/Zc:__cplusplusCorrect __cplusplus value
Language/permissive-Strict standards conformance
Optimization/OdDisable (Debug)
Optimization/O1Minimize size
Optimization/O2Maximize speed (Release default)
Optimization/GLWhole-program optimization
Debug/ZiPDB file
Debug/ZIPDB with Edit and Continue
Debug/Z7Symbols in .obj
Warnings/W4Level 4 (recommended)
Warnings/WXWarnings as errors
CRT/MDDynamic CRT release
CRT/MDdDynamic CRT debug
CRT/MTStatic CRT release
CRT/MTdStatic CRT debug
Exceptions/EHscStandard C++ exceptions
RTTI/GREnable dynamic_cast
Output/Fe:Name executable
Output/Fo:Name / directory for .obj
Preprocessor/DDefine macro
Preprocessor/UUndefine macro
Includes/IAdd include directory

Build docs developers (and LLMs) love