VS Code C++ on Windows: MSVC, CMake, and vcpkg Without the Headache
Setting up C++ in VS Code on Windows looks simple until the compiler error says it cannot find cl.exe. I helped three people through this last semester and every one of them had the same root cause: VS Code was using the wrong shell, so the MSVC environment was never loaded. Here is the setup I hand them now.
Install the pieces
1. Visual Studio Build Tools, with "Desktop development with C++" selected. You do not need the full Visual Studio IDE.
2. CMake from cmake.org. Choose the Windows x64 installer and let it add itself to PATH.
3. Git for Windows, if you do not already have it.
4. VS Code from code.visualstudio.com.
In VS Code install these extensions: C/C++ (Microsoft), CMake Tools (Microsoft), and optionally CMake Language Support. That is enough. Do not install five different C++ extensions; they fight over IntelliSense.
Open a terminal inside VS Code and run:
cl
cmake --version
If cl is not recognized, open "Developer PowerShell for VS 2022" from the Start menu and run it there. If it works there but not in VS Code, you have the shell problem described below.
The shell problem
MSVC is not on PATH by default. It is initialized by a script that ships with Visual Studio. When VS Code launches a plain PowerShell, that script never runs, so cl.exe cannot be found.
The fix is to tell VS Code to start a developer shell. In settings.json:
"terminal.integrated.profiles.windows": {
"Developer PowerShell": {
"source": "PowerShell",
"args": ["-NoExit", "-Command", "& 'C:\\Program Files\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Auxiliary\\Build\\vcvarsall.bat' x64 && powershell"]
}
},
"terminal.integrated.defaultProfile.windows": "Developer PowerShell"
Adjust the path if you installed a different edition. Close and reopen the terminal, then run cl again. This step solved the problem for all three people.
A tiny CMake project
Make a folder with two files.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(hello)add_executable(hello main.cpp)
main.cpp:
#include <iostream>int main() {
std::cout << "hello from cmake" << std::endl;
return 0;
}
In VS Code, press Ctrl+Shift+P and run "CMake: Configure". Pick the Visual Studio generator when asked. Then run "CMake: Build". The executable appears in out/build/. Run it from the terminal to confirm.
If Configure fails with "Could not find compiler", go back to the cl check. It is almost never a CMake problem on a fresh Windows machine.
Where vcpkg fits
vcpkg is a package manager for C++ libraries. Install it from GitHub:
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
Install a library, for example nlohmann/json:
.\vcpkg install nlohmann-json:x64-windows
Then tell CMake about the toolchain file when configuring:
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "C:/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
In CMakeLists.txt you can then do find_package(nlohmann_json CONFIG REQUIRED) and link it. The important part is passing the toolchain file before the first configure. Changing it after CMake has cached a build directory requires deleting that directory and configuring again. That cost me an afternoon once.
What I would tell a beginner
Get cl working in VS Code's own terminal before writing any code. Everything else is downstream of that one check. When it works, CMake, vcpkg, and IntelliSense fall into place. When it does not, no amount of reinstalling extensions helps.