CMake is the de facto standard build system for cross-platform C++ development, and Visual Studio has first-class CMake support built in. When you open any folder containing aDocumentation 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.
CMakeLists.txt, Visual Studio automatically configures IntelliSense, parses build targets, and offers a rich editing and debugging experience — with no .sln or .vcxproj files required. The modern CMakePresets.json workflow lets you define named configurations for every platform and share them with your team through source control.
The CMakePresets.json Workflow
CMakePresets.json (introduced in CMake 3.19) defines named configure, build, and test presets that work identically on the command line, in Visual Studio, and in CI pipelines. Place the file at the root of your repository.
Conditional Platform Logic in CMakeLists.txt
A well-structuredCMakeLists.txt uses CMAKE_SYSTEM_NAME and compiler-detection variables to apply platform-specific settings while keeping the common build logic in one place:
Toolchain Files for Cross-Compilation
When building for a different CPU architecture — such as ARM for embedded systems or an Android target — CMake uses a toolchain file (-DCMAKE_TOOLCHAIN_FILE=...) to set the cross-compiler, sysroot, and system name. Visual Studio passes the toolchain file through the preset’s toolchainFile field.
Example: ARM Cortex-M4 Toolchain File
Switching Between MSVC, Clang, and GCC in Visual Studio
- MSVC (cl.exe)
- Clang-cl (LLVM)
- GCC (Linux / WSL)
- Clang (Linux / macOS)
MSVC is selected when
CMAKE_CXX_COMPILER is cl.exe. Use it for Windows targets with full Visual Studio debugger integration:Using Generator Expressions for Per-Config Settings
CMake generator expressions ($<...>) evaluate at build time, allowing per-configuration logic without if-statements:
Running the Same Build from the Command Line
One of the key advantages ofCMakePresets.json is that your CI pipeline uses the exact same preset names as your IDE:
vcpkg Integration for Cross-Platform Dependencies
vcpkg integrates natively with CMake presets to manage C++ library dependencies across platforms. Add the vcpkg toolchain file to your preset:vcpkg.json manifest at the repo root to declare dependencies:
x64-windows, x64-linux, arm64-linux, etc.) so the same CMakeLists.txt finds libraries on every platform.