Article by Ayman Alheraki on January 11 2026 10:36 AM
Microsoft Visual C++ (MSVC) is a powerful compiler that integrates with Visual Studio but can also be used independently via the Developer Command Prompt or PowerShell. While most large C++ projects use CMake or MSBuild, it is entirely possible to build and manage projects manually using MSVC’s cl.exe compiler.
This guide walks through using MSVC to compile small and large C++ projects, link libraries, enable C++20, C++23, and C++26, optimize builds, and automate the process with scripts.
Before compiling, ensure you have MSVC installed.
To use MSVC’s compiler (cl.exe), open:
x64 Native Tools Command Prompt for VS
x86 Native Tools Command Prompt for VS (for 32-bit builds)
Alternatively, run this in PowerShell:
x& "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
This sets up the correct environment variables for cl.exe.
For a basic program:
xcl main.cpp
This produces main.exe.
MSVC defaults to C++14. Use /std:c++20 or newer for modern C++ features:
xcl /std:c++20 main.cppcl /std:c++23 main.cppcl /std:c++latest main.cpp # Experimental C++26 (C++2b)
C++23 Features in MSVC:
std::expected for error handling.
std::mdspan for multi-dimensional views.
std::print for modern-style printing.
C++26 Features (Experimental)
Deduction in this pointer.
Pattern Matching (if approved).
For larger projects with multiple source files (main.cpp and helper.cpp):
xcl main.cpp helper.cpp /Fe:my_program.exe
This compiles and links both files.
.obj) for Incremental CompilationInstead of recompiling everything:
xcl /c main.cpp helper.cppcl main.obj helper.obj /Fe:my_program.exe
/c → Only compiles to .obj (no linking).
/Fe:output.exe → Sets the executable name.
For large projects, organize files properly:
xmy_project/├── src/│ ├── main.cpp│ ├── helper.cpp│ ├── module/│ │ ├── math.cpp│ │ ├── physics.cpp├── include/│ ├── helper.hpp│ ├── module/│ │ ├── math.hpp│ │ ├── physics.hpp├── build/
/I)If header files are in include/:
xcl /Iinclude src\main.cpp src\helper.cpp /Fe:my_program.exe
For efficiency, compile separately:
xcl /c /Iinclude src\main.cpp /Fo:build\main.objcl /c /Iinclude src\helper.cpp /Fo:build\helper.objcl /c /Iinclude src\module\math.cpp /Fo:build\math.objcl build\main.obj build\helper.obj build\math.obj /Fe:my_program.exe
xcl main.cpp /link /NODEFAULTLIB:LIBCMT /DEFAULTLIB:msvcrt.lib
.lib)xcl main.cpp mylib.lib /Fe:my_program.exe
.dll)First, ensure you have mylib.lib and mylib.dll, then:
xcl main.cpp mylib.lib /Fe:my_program.exe
Place mylib.dll in the same folder as my_program.exe for runtime linking.
/O1 → Optimize for size.
/O2 → Optimize for speed.
/Ox → Maximum optimizations.
/GL → Whole-program optimization.
Example:
xcl /O2 main.cpp /Fe:optimized.exe
/Zi and /FS/Zi → Debugging symbols.
/FS → Multi-threaded compilation.
/Od → Disable optimizations for debugging.
Example:
xcl /Zi /Od main.cpp /Fe:debug.exe
xcl /EHsc /DWIN32 main.cpp /Fe:my_program32.exe
xcl /EHsc /DWIN64 main.cpp /Fe:my_program64.exe
xcl /D_ARM64 main.cpp /Fe:my_program_arm64.exe
Instead of manually compiling files, use a batch file (build.bat):
build.batx@echo offmkdir buildcl /c /Iinclude src\main.cpp /Fo:build\main.objcl /c /Iinclude src\helper.cpp /Fo:build\helper.objcl /c /Iinclude src\module\math.cpp /Fo:build\math.objcl build\main.obj build\helper.obj build\math.obj /Fe:my_program.exeecho Build complete!
Run it:
xbuild.bat
While manual builds work for many projects, a build system becomes necessary for:
Cross-platform support.
Dependency management.
Automating complex builds.
However, understanding MSVC without CMake is crucial for:
Debugging build errors.
Creating simple builds.
Learning how compilers work internally.
MSVC provides powerful tools to build C++ programs without relying on CMake or MSBuild. By using cl.exe, you can:
Compile simple and large projects manually.
Optimize builds for performance.
Link external libraries (.lib and .dll).
Automate builds with batch scripts.
This method is useful for learning, debugging, and small projects, but for large applications, CMake or MSBuild is recommended.