A C++ Dev Environment on Linux with VS Code, CMake, and vcpkg
Table of Contents
VS Code’s Using C++ on Linux
tutorial is a good way to get a single helloworld.cpp compiling with g++ and
debugging with GDB. The moment a project grows past one file, though, the
hand-written tasks.json arguments and the include list in
c_cpp_properties.json become a second project to maintain.
This is the setup I use instead: CMake owns the build, vcpkg owns third-party dependencies, and the CMake Tools and C/C++ extensions connect the two. You get IntelliSense from the real compile flags, one-key builds, and GDB debugging without maintaining compiler arguments or include paths by hand.
The stack #
| Piece | What it does |
|---|---|
| VS Code | Editor and debugger front end. |
| C/C++ extension | IntelliSense, navigation, and GDB integration. |
| CMake Tools extension | Configure, build, test, and debug targets from the editor. |
| CMake + Ninja | Generates and runs the build. |
| vcpkg | Installs the libraries the project declares. |
| GCC + GDB | Compiles the code and debugs the binary. |
The important idea is that CMake is the single source of truth. VS Code does not get its own list of compiler flags or include directories; it asks CMake Tools, which asks CMake. When a dependency is added, both the build and IntelliSense see it.
1. Install the native toolchain #
The commands below assume a Debian or Ubuntu based distribution. Adjust the package names for Fedora, Arch, or whatever you run.
sudo apt update
sudo apt install -y \
build-essential \
gdb \
cmake \
ninja-build \
git \
curl \
zip \
unzip \
tar \
pkg-config
Some vcpkg ports run autoconf, automake, libtool, bison, or flex
during their build. Install those now so a dependency does not fail halfway
through:
sudo apt install -y autoconf automake libtool bison flex python3 perl
Check that everything is on PATH:
g++ --version
cmake --version
ninja --version
gdb --version
CMake 3.21 or newer is enough for the presets below. The CMake Tools extension recommends 3.27 or newer; Ubuntu 24.04 and Debian 13 ship a version that is new enough. If your distribution is older, the Kitware APT repository has current packages.
2. Install vcpkg #
I keep one vcpkg checkout in /opt/vcpkg and share it between projects. The
directory has to be writable by your user because vcpkg updates its own
registry and downloads sources there:
sudo mkdir -p /opt/vcpkg
sudo chown "$USER":"$USER" /opt/vcpkg
git clone https://github.com/microsoft/vcpkg.git /opt/vcpkg
/opt/vcpkg/bootstrap-vcpkg.sh -disableMetrics
If you would rather not use sudo, ~/vcpkg works just as well; the only
thing that changes is the value of VCPKG_ROOT.
Run the bootstrap script as yourself, not with sudo. Running it as root
leaves root-owned files behind and the next vcpkg command will fail with a
permission error. If you already did that, fix it with:
sudo chown -R "$USER":"$USER" /opt/vcpkg
-disableMetrics opts out of vcpkg’s telemetry. Drop it if you want to keep
telemetry enabled.
Now put the checkout on your PATH and export VCPKG_ROOT. Add this to
~/.bashrc or ~/.zshrc:
export VCPKG_ROOT="/opt/vcpkg"
export PATH="$VCPKG_ROOT:$PATH"
Reload the shell and check:
source ~/.bashrc # or: source ~/.zshrc
vcpkg version
echo "$VCPKG_ROOT"
VCPKG_ROOT matters more than the PATH entry: the CMake toolchain file lives
at $VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake, and that is how CMake finds
the packages vcpkg installs.
A VS Code gotcha: a VS Code window launched from the desktop icon may not inherit exports from
~/.bashrc. Open the project withcode .from a terminal, or put the exports in~/.profile(or~/.config/environment.d/vcpkg.conf) so the graphical session sees them. Restart VS Code after changing the environment; an already-running window keeps its old environment.
3. Create the project and its manifest #
mkdir -p ~/dev/hello-vcpkg
cd ~/dev/hello-vcpkg
Inside the project, create a vcpkg manifest and add fmt as a dependency:
vcpkg new --application
vcpkg add port fmt
vcpkg new --application creates two files:
vcpkg.json— the manifest that lists this project’s dependencies.vcpkg-configuration.json— the registry and baseline information that pins the versions vcpkg resolves.
Then vcpkg add port fmt edits the manifest. It should look like this:
{
"name": "hello-vcpkg",
"version-string": "0.1.0",
"dependencies": [
"fmt"
]
}
The name comes from the directory, so yours may differ. This is manifest mode: dependencies are declared per project instead of being installed globally. That is what makes the setup reproducible for the next person, or for future you on a new machine.
Keep both JSON files under source control. vcpkg-configuration.json is what
keeps the dependency versions from drifting.
4. Add the CMake project #
Create CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(hello_vcpkg
VERSION 0.1.0
DESCRIPTION "VS Code + CMake + vcpkg demo"
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(fmt CONFIG REQUIRED)
add_executable(hello main.cpp)
target_link_libraries(hello PRIVATE fmt::fmt)
target_compile_options(hello PRIVATE -Wall -Wextra -Wpedantic)
find_package(fmt CONFIG REQUIRED) is the line vcpkg makes possible. The
fmt::fmt target carries the include directories, compile definitions, and
link libraries that fmt needs. There is no -I or -L to maintain.
Add main.cpp:
#include <fmt/core.h>
#include <string>
int main() {
const std::string name = "vcpkg";
fmt::print("Hello, {}!\n", name);
return 0;
}
Then add CMakePresets.json at the project root:
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": {
"value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"type": "FILEPATH"
},
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
}
},
{
"name": "debug",
"displayName": "Debug",
"inherits": "base",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "release",
"displayName": "Release",
"inherits": "base",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "debug",
"configurePreset": "debug"
},
{
"name": "release",
"configurePreset": "release"
}
]
}
The base preset is hidden and holds the shared settings. debug and
release inherit it and only change CMAKE_BUILD_TYPE, which keeps the two
builds from sharing a cache by using build/debug and build/release.
Why the important fields are there:
generator: Ninjais fast and has good dependency tracking.binaryDirkeeps generated files out of the source tree.CMAKE_TOOLCHAIN_FILEis the bridge to vcpkg. It has to be set when CMake first configures the project.CMAKE_EXPORT_COMPILE_COMMANDSwrites acompile_commands.jsonfile, which is useful for clangd and other tools. The C/C++ extension gets its information from CMake Tools instead, but the file costs nothing.
At this point the project looks like this:
hello-vcpkg/
├── CMakeLists.txt
├── CMakePresets.json
├── main.cpp
├── vcpkg-configuration.json
└── vcpkg.json
5. Build from the terminal first #
Before involving VS Code, prove the build works:
cmake --preset debug
cmake --build --preset debug
./build/debug/hello
You should see:
Hello, vcpkg!
The first configure takes a while. CMake invokes vcpkg, vcpkg reads
vcpkg.json, downloads the fmt sources, builds them, and installs them under
build/debug/vcpkg_installed. Later configures reuse the binary cache in
~/.cache/vcpkg/archives and are much faster.
If this step fails, fix it here. Debugging CMake through the editor before the command line works just adds a layer of indirection.
6. Configure VS Code #
Install the two extensions, either from the Extensions view or from the command line:
code --install-extension ms-vscode.cpptools
code --install-extension ms-vscode.cmake-tools
If code is not on your PATH, install the same two extensions from the
Extensions view and open the folder with File > Open Folder. Otherwise:
cd ~/dev/hello-vcpkg
code .
Create .vscode/settings.json:
{
"cmake.useCMakePresets": "always",
"cmake.configureOnOpen": true,
"cmake.buildBeforeRun": true,
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"files.associations": {
"CMakeLists.txt": "cmake"
}
}
What these do:
cmake.useCMakePresetstells CMake Tools to useCMakePresets.jsoninstead of asking you to pick a kit.cmake.configureOnOpenconfigures the project when the folder opens, so IntelliSense has real compile flags quickly.cmake.buildBeforeRunrebuilds the launch target before starting the debugger. This replaces apreLaunchTaskinlaunch.json.C_Cpp.default.configurationProvidermakes the C/C++ extension ask CMake Tools for include paths and definitions. This is why you do not need a hand-writtenc_cpp_properties.json.
If you followed the single-file VS Code tutorial and already have a
.vscode/c_cpp_properties.json, delete it or set its configurationProvider
to ms-vscode.cmake-tools as well. A configuration file in the workspace takes
precedence over the default setting, and a stale one will keep showing the old
include paths.
Optionally, add .vscode/extensions.json so anyone opening the repository is
prompted to install the same two extensions:
{
"recommendations": [
"ms-vscode.cpptools",
"ms-vscode.cmake-tools"
]
}
Open the project root in VS Code, not the build directory. Then run these
commands from the Command Palette:
- CMake: Select Configure Preset →
debug. - CMake: Configure.
- CMake: Build.
- CMake: Set Launch/Debug Target →
hello.
The CMake Tools status bar shows the active preset and build target. If it
still asks you to select a kit, cmake.useCMakePresets is not set to
"always" or the folder was opened above the project root.
Debug configuration #
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug hello",
"type": "cppdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"cwd": "${command:cmake.getLaunchTargetDirectory}",
"args": [],
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable GDB pretty printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
${command:cmake.launchTargetPath} is resolved by CMake Tools to the binary
for the selected launch target, so there is no hard-coded output path.
${command:cmake.getLaunchTargetDirectory} sets the working directory to the
binary’s directory. If your program loads assets relative to the repository
root, change cwd to ${workspaceFolder}.
There is deliberately no tasks.json. CMake Tools owns configuring and
building; duplicating that in a task is how the two get out of sync. Use
tasks.json only for jobs CMake does not know about, such as running a code
generator.
7. Debug it #
Open main.cpp, click the gutter next to the fmt::print line to set a
breakpoint, and press F5. The debugger stops before the print, the
Variables pane shows name, and the Watch pane can evaluate
expressions. Step over the call with F10, or step into a function with F11
when debug information is available.
Set "stopAtEntry": true in launch.json if you want the debugger to stop in
main before anything runs. Add runtime arguments to the "args" array, for
example ["--verbose"], and they will be passed to the program.
If breakpoints show as hollow circles, the binary was built without debug
information. Check that the debug preset is selected and that
CMAKE_BUILD_TYPE is Debug in build/debug/CMakeCache.txt.
8. Add and remove dependencies #
Adding a library is a three-step loop. For example, for JSON support:
vcpkg add port nlohmann-json
Then in CMakeLists.txt:
find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(hello PRIVATE nlohmann_json::nlohmann_json)
Finally, reconfigure so CMake picks up the new package and IntelliSense sees the headers:
- Command Palette → CMake: Configure, or
cmake --preset debugfrom a terminal.
To remove a dependency, edit vcpkg.json to delete it and remove the
find_package/target_link_libraries lines from CMakeLists.txt, then
reconfigure. vcpkg remove is for classic mode; in manifest mode the manifest
is the source of truth.
The next configure installs the new dependency — from vcpkg’s binary cache if it has been built before — and CMake regenerates the build files. This is normal; subsequent builds are incremental.
9. What to commit #
Commit the files that describe the environment:
.gitignore
CMakeLists.txt
CMakePresets.json
vcpkg.json
vcpkg-configuration.json
main.cpp
.vscode/settings.json
.vscode/launch.json
.vscode/extensions.json
Ignore the generated directories:
build/
vcpkg_installed/
.vscode/ipch/
build/ contains the CMake cache, the binaries, and the vcpkg-installed
packages. Anything in it is reproducible from the files above.
Troubleshooting #
| Symptom | Fix |
|---|---|
vcpkg: command not found | The shell has not reloaded, or PATH does not include $VCPKG_ROOT. Run source ~/.bashrc, then echo "$VCPKG_ROOT" and vcpkg version. |
Could not find a package configuration file provided by "fmt" | The toolchain file was not applied, or the manifest is out of date. Check that CMAKE_TOOLCHAIN_FILE appears in build/debug/CMakeCache.txt, run vcpkg add port fmt, and reconfigure. If you changed the toolchain after configuring, delete build/ (or run CMake: Delete Cache and Reconfigure) and configure again. |
IntelliSense cannot find fmt/core.h but the build works | C_Cpp.default.configurationProvider is missing or pointed at the wrong extension. Set it to ms-vscode.cmake-tools, run CMake: Configure, then C/C++: Reset IntelliSense Database. |
| CMake Tools keeps asking for a kit | Set cmake.useCMakePresets to "always" and select the debug preset. |
VS Code cannot see VCPKG_ROOT | The editor was launched from the desktop and did not inherit your shell. Start it with code . from a configured terminal, or move the exports to ~/.profile. |
| The debugger will not start | Select a launch target first (CMake: Set Launch/Debug Target), make sure program resolves to an existing binary, and check that /usr/bin/gdb exists. |
| vcpkg bootstrap fails or files are not writable | Missing curl, zip, or tar; or the checkout was bootstrapped with sudo. Install the tools and sudo chown -R "$USER":"$USER" /opt/vcpkg. |
| The first configure takes minutes | vcpkg is building dependencies from source. Later builds use its binary cache. |
Wrapping up #
The final loop is short: declare a dependency in vcpkg.json, add a
find_package and a target_link_libraries line, reconfigure, and keep
coding. CMake handles the build graph; CMake Tools exposes it to VS Code; the
C/C++ extension supplies IntelliSense and GDB from the same information.
That is a lot more setup than the single-file g++ task in the VS Code
tutorial, but it is setup you do once per project. After that, adding a library
is a manifest edit instead of a hunt through compiler flags, and a new machine
needs the toolchain plus a configure to reproduce the build.
The VS Code C++ on Linux tutorial is still worth reading for the editor basics, and the vcpkg CMake tutorial covers the same manifest workflow from Microsoft’s side.