CMake is a cross-platform, open-source build system generator that produces native build files (Makefiles, Ninja files, Visual Studio solution files) from a singleDocumentation 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 description. It has become the de-facto standard for C++ projects that need to build on multiple operating systems and compilers. Visual Studio 2019 and later provide first-class CMake support — you can open any folder containing a CMakeLists.txt and Visual Studio automatically configures IntelliSense, discovers build targets, and lets you build and debug without creating any Visual Studio-specific project files. This tutorial walks through creating a CMake project from scratch, understanding the configuration files, and building and debugging a target.
Prerequisites
- Visual Studio 2019 or 2022 with the Desktop development with C++ workload installed. C++ CMake tools for Windows is included in that workload by default in Visual Studio 2022.
- If you are on Visual Studio 2019, verify that C++ CMake tools for Windows is checked in the installer’s Individual components tab.
What Visual Studio Does When You Open a CMake Folder
When you open a folder containing aCMakeLists.txt, Visual Studio:
- Detects the root
CMakeLists.txtand enables CMake integration. - Invokes
cmake.exeto generate the CMake cache (CMakeCache.txt) for the default configuration. - Displays the CMake output in the Output window (select CMake from the Show output from dropdown).
- Indexes source files in the background to power IntelliSense, Go to Definition, Find All References, and refactoring.
- Populates the Startup Item toolbar dropdown with all executable targets defined in your
CMakeLists.txt.
Creating a CMake Project
Create a new project using the CMake template
On the Visual Studio Start Window, click Create a new project. Filter by C++, Windows, and search for CMake Project. Select the template and click Next.Name the project
HelloCMake, choose a location, and click Create. Visual Studio creates a folder with this structure:Review the generated CMakeLists.txt
Open Key CMake commands used here:
CMakeLists.txt. The generated file looks like this:cmake_minimum_required— sets the oldest CMake version this file is compatible with.project("HelloCMake")— names the project and enables C and C++ language support.add_executable(HelloCMake "HelloCMake.cpp")— declares an executable target namedHelloCMakebuilt fromHelloCMake.cpp.set_property(TARGET ... CXX_STANDARD 20)— requests C++20 for the target.
Understanding CMakeLists.txt Structure
A real-worldCMakeLists.txt is more involved. Here is an annotated example for a project with a library and an executable:
add_library vs add_executable
add_library vs add_executable
add_library(name [STATIC|SHARED|MODULE] sources...) creates a library target. STATIC produces a .lib file; SHARED produces a .dll and .lib import library. add_executable(name sources...) creates an executable target.target_include_directories
target_include_directories
Specifies include paths for a target.
PUBLIC means the paths are visible to this target AND any targets that link against it. PRIVATE means only this target sees the paths. INTERFACE means only consumers see the paths.target_link_libraries
target_link_libraries
Specifies library dependencies.
PRIVATE means MathLib is linked into MyApp but is not transitively propagated to things that link MyApp. Use PUBLIC if consumers of MyApp also need MathLib’s headers.Open Folder Workflow (Existing Projects)
If you already have a CMake project on disk (or cloned from GitHub), use the Open Folder workflow instead of creating a new project:Open the folder
From the Visual Studio Start Window, click Open a local folder (or go to File > Open > Folder). Navigate to the folder that contains your root
CMakeLists.txt and click Select Folder.Visual Studio opens the folder and begins CMake configuration automatically.Switch to CMake Targets View
In Solution Explorer, click the Solutions and Folders toggle button (the dropdown at the top of Solution Explorer) and select CMake Targets View. This shows your project organized by CMake targets (executables, libraries) rather than as raw folders and files.
Build Configurations and CMake Presets
Visual Studio 2019 16.10+ and 2022 useCMakePresets.json as the recommended way to define build configurations. Earlier versions use CMakeSettings.json. The toolbar configuration dropdown (e.g., x64-Debug) reflects the presets defined in this file.
A minimal CMakePresets.json:
Building CMake Projects
There are three ways to build a CMake project in Visual Studio:Editing CMakeLists.txt
Visual Studio 2019 16.5+ provides language services for CMake scripts:- Syntax highlighting for CMake commands, variables, and string literals.
- Go to Definition (
F12) for CMake variables and functions. - Find All References for targets and variables across all
CMakeLists.txtfiles. - IntelliSense completions for CMake commands and CMake module names.
CMakeLists.txt, a yellow notification bar appears at the top of the editor offering to re-run CMake configuration. Click Configure (or let it auto-configure) to regenerate the cache.
Starting in Visual Studio 2019 16.5+, you can add and remove source files from your CMake project directly in Solution Explorer. Visual Studio automatically edits the
CMakeLists.txt to keep it in sync — no manual editing required for file additions and removals.Debugging CMake Targets
Debugging CMake projects works the same as debugging MSBuild projects:- Select your executable target in the Startup Item dropdown.
- Set breakpoints in any source file.
- Press F5 to build and launch under the debugger.
launch.vs.json file:
Enable Edit and Continue for CMake Projects
To enable Edit and Continue in a CMake project built with MSVC, add the following to yourCMakeLists.txt:
/ZI) and enables incremental linking, which is required for Edit and Continue to apply code changes while debugging.
Next Steps
Now that you understand CMake projects in Visual Studio, explore how to use the sameCMakeLists.txt to target Linux from Windows in the Linux Development Tutorial, or use vcpkg to add third-party library dependencies to your CMake project.