This guide covers the usage of CMake in both Visual Studio Code and Visual Studio.
- Prerequisites
- Installing CMake
- Using CMake with Visual Studio Code
- Using CMake with Visual Studio
- Common CMake Commands
- Troubleshooting
Before starting with CMake, ensure you have:
- A C/C++ compiler (such as MSVC, GCC, or Clang)
- Usually cmake is pre-installed in every Visual Studio
- Git (optional, but recommended for version control)
-
Download CMake:
- Visit the official CMake website
- Download the appropriate installer for your platform
- For Windows, choose the Windows x64 Installer
-
Install CMake:
- Run the installer
- Make sure to select "Add CMake to the system PATH" during installation
- Choose either "Add for all users" or "Add for current user"
-
Verify Installation:
- Open a terminal/command prompt
- Run
cmake --version - You should see the installed CMake version
-
Open Visual Studio Installer:
- Launch Visual Studio Installer from your desktop or Start menu
-
Choose your installed product
- Choose your installed product (Build Tools or Visual Studio) and press Modify
- Go to the "Individual components" tab
-
Find and install cmake
- Type "cmake" in the search box to quickly find the component
- Check the box for "C++ CMake tools for Windows" and/or "CMake tools for Windows"
- Click "Modify" to install the selected components
-
Install Required Extensions:
-
Configure a Project:
- Open a folder containing a CMakeLists.txt file
- VS Code should automatically detect the CMake project
- Click on the CMake icon in the sidebar to access CMake tools
- Select a compiler/kit when prompted
- Open folder in VS
- Select configure preset
- Compile (Ctrl+B)
-
"CMake not found" Error:
- Make sure CMake is in your PATH
- Restart VS Code/Visual Studio after installing CMake
-
Compiler Not Found:
- Install a C/C++ compiler
- Make sure the compiler is in your PATH
- For Visual Studio Code, select a kit manually (F1 > CMake: Select a Kit)
-
Build Errors:
- Check the CMake output panel for detailed error messages
- Verify that all required libraries are installed
- Check CMakeLists.txt for errors
-
Visual Studio CMake Cache Out of Sync:
- Delete the build directory and reconfigure
- Project > Clear CMake Cache
- CMake Documentation: https://cmake.org/documentation/
- CMake Tools Extension Documentation: https://vector-of-bool.github.io/docs/vscode-cmake-tools/
- Visual Studio CMake Documentation: https://docs.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio