cpp
dev environment
settings
vscode
VSCode CPP dev environment
C/C++ IntelliSense, debugging, and code browsing.
Interfacing with the build process
Build
Using CMake presets
CMake Presets are a standardized, version-controlled way to define build configurations (compiler, build dir, flags, generator, cache vars) so everyone builds the project the same way — from CLI or IDE.
CMake Presets contain information how to:
- configure,
- build,
- test
- package a project.
Preset information is stored in a JSON files:
{
"version" : 6 ,
"cmakeMinimumRequired" : {
"major" : 3 ,
"minor" : 23 ,
"patch" : 0
},
"configurePresets" : [
{
// ...
}
],
"buildPresets" : [
{
// ...
}
],
"testPresets" : [
{
// ...
}
]
}
Preset Types
1️⃣ Configure presets
Define how CMake configures the project
"configurePresets" : [
{
"name" : "debug" ,
"generator" : "Ninja" ,
"binaryDir" : "build/debug" ,
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Debug"
}
}
]
Equivalent to:
cmake -S . -B build/debug -DCMAKE_BUILD_TYPE= Debug
run from cli #cmake --preset <preset name>
cmake --preset debug
2️⃣ Build presets
Define how to build after configuration
"buildPresets" : [
{
"name" : "debug" ,
"configurePreset" : "debug"
}
]
Equivalent to:
cmake --build build/debug
#cmake --build --preset <preset name>
cmake --build --preset debug
VSCode
{
"cmake.useCMakePresets" : "always"
}
Demo: cmake presets with debug and release
{
"version" : 6 ,
"cmakeMinimumRequired" : {
"major" : 3 ,
"minor" : 23 ,
"patch" : 0
},
"configurePresets" : [
{
"name" : "debug" ,
"generator" : "Ninja" ,
"binaryDir" : "build/debug" ,
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Debug"
}
},
{
"name" : "release" ,
"generator" : "Ninja" ,
"binaryDir" : "build/release" ,
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Release"
}
}
],
"buildPresets" : [
{
"name" : "debug" ,
"configurePreset" : "debug"
},
{
"name" : "release" ,
"configurePreset" : "release"
}
]
}
VSCode preset commnads
cmake: Configure
cmake: Build
cmake: Clean
cmake: select configure preset
cmake: select build preset
Compilers
Clang
Ubuntu Version
Default Clang
Default GCC
Default C++ Standard Usable
22.04 (Jammy)
14
11
C++20 (usable)
24.04 (Noble)
18
13
C++20 / C++23 (good support)
sudo apt install -y \
clang \
clangd \
lldb \
lld \
cmake \
ninja-build \
build-essential \
pkg-config
| Tool | Purpose |
| --------------- | --------------------------- |
| clang | Compiler |
| clangd | IDE integration |
| lldb | Debugger |
| lld | Fast linker |
| cmake | Build system |
| ninja | Fast build backend |
| build-essential | System headers + base tools |
| pkg-config | Library detection |
Clangd
Language Server Protocol (LSP) server for C++.
Provides:
Autocomplete
Jump to definition
Error diagnostics
Refactoring
VSCode
Install clangd ext.
install clangd by LLVM
don't forget
To install clangd binary
Disabled microsoft intellisense
"C_Cpp.intelliSenseEngine" : "disabled"
compile_command.json
It is a database of exact compiler commands used to build every .cpp file in your project.
The ground truth of how your project is compiled.
Generate compile_command.json
cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS= ON
Include paths
Defines
Compiler flags
C++ standard version
Without this file → clangd guesses → incorrect IntelliSense.
.vscode/settings.json "clangd.arguments" : [
"--compile-commands-dir=build"
]
Using LLDB
Install CodeLLDB
using it with `launch.json"
.vscode/launch.json {
"version" : "0.2.0" ,
"configurations" : [
{
"name" : "Debug with LLDB" ,
"type" : "lldb" ,
"request" : "launch" ,
"program" : "${workspaceFolder}/build/debug/my_app" ,
"args" : [],
"cwd" : "${workspaceFolder}" ,
"stopOnEntry" : false
}
]
}
---
#### VSCode projec t
- usi n g cla n g
- usi n g cmake
- usi n g cmake prese ts
VSCode ex t .
- cla n gd
- CodeLLDB
- cmake t ools
```c t i tle = "CMakeLists.txt"
cmake_mi n imum_required(VERSION 3.22 )
projec t (MyApp VERSION 1.0 LANGUAGES CXX)
se t (CMAKE_CXX_STANDARD 20 )
se t (CMAKE_CXX_STANDARD_REQUIRED ON)
se t (CMAKE_C_COMPILER cla n g)
se t (CMAKE_CXX_COMPILER cla n g++)
se t (CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_execu ta ble(my_app
src/mai n .cpp
)
.vscode/settings.json {
"cmake.useCMakePresets" : "always" ,
"C_Cpp.intelliSenseEngine" : "disabled" ,
"clangd.arguments" : [
"--compile-commands-dir=build"
]
}
.vscode/launch.json {
"version" : "0.2.0" ,
"configurations" : [
{
"name" : "Debug with LLDB" ,
"type" : "lldb" ,
"request" : "launch" ,
"program" : "${workspaceFolder}/build/debug/my_app" ,
"args" : [],
"cwd" : "${workspaceFolder}" ,
"stopOnEntry" : false
}
]
}
.vscode/task/json {
"version" : "2.0.0" ,
"tasks" : [
{
"label" : "CMake: Configure (debug preset)" ,
"type" : "shell" ,
"command" : "cmake --preset debug" ,
"problemMatcher" : []
},
{
"label" : "CMake: Build (debug preset)" ,
"type" : "shell" ,
"command" : "cmake --build --preset debug" ,
"group" : {
"kind" : "build" ,
"isDefault" : true
},
"dependsOn" : "CMake: Configure (debug preset)" ,
"problemMatcher" : "$gcc"
},
{
"label" : "CMake: Configure (release preset)" ,
"type" : "shell" ,
"command" : "cmake --preset release" ,
"problemMatcher" : []
},
{
"label" : "CMake: Build (release preset)" ,
"type" : "shell" ,
"command" : "cmake --build --preset release" ,
"group" : "build" ,
"dependsOn" : "CMake: Configure (release preset)" ,
"problemMatcher" : "$gcc"
}
]
}
CMakePresets.json {
"version" : 3 ,
"cmakeMinimumRequired" : {
"major" : 3 ,
"minor" : 21 ,
"patch" : 0
},
"configurePresets" : [
{
"name" : "debug" ,
"displayName" : "Debug" ,
"generator" : "Ninja" ,
"binaryDir" : "${sourceDir}/build/debug" ,
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Debug"
}
},
{
"name" : "release" ,
"displayName" : "Release" ,
"generator" : "Ninja" ,
"binaryDir" : "${sourceDir}/build/release" ,
"cacheVariables" : {
"CMAKE_BUILD_TYPE" : "Release"
}
}
],
"buildPresets" : [
{
"name" : "debug" ,
"configurePreset" : "debug"
},
{
"name" : "release" ,
"configurePreset" : "release"
}
]
}