|
| 1 | +**important file you can put inside your `.vscode` directory** when working with **C++ and CMake projects** (especially if you’re using `CMakePresets.json` and `CMakeLists.txt`): |
| 2 | + |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +Here is my (simplified) `CMakePresets.json` |
| 7 | + |
| 8 | +```json |
| 9 | + |
| 10 | +{ |
| 11 | + "version": 3, |
| 12 | + "configurePresets": [ |
| 13 | + { |
| 14 | + "name": "ninja-multi", |
| 15 | + "generator": "Ninja Multi-Config", |
| 16 | + "binaryDir": "${sourceDir}/build/", |
| 17 | + "cacheVariables": { |
| 18 | + "CMAKE_POLICY_DEFAULT_CMP0048": "NEW", |
| 19 | + "CMAKE_CONFIGURATION_TYPES": "Debug;Release;RelWithDebInfo;MinSizeRel" |
| 20 | + } |
| 21 | + } |
| 22 | + ], |
| 23 | + "buildPresets": [ |
| 24 | + { |
| 25 | + "name": "ninja-multi-debug", |
| 26 | + "configurePreset": "ninja-multi", |
| 27 | + "configuration": "Debug" |
| 28 | + }, |
| 29 | + { |
| 30 | + "name": "ninja-multi-release", |
| 31 | + "configurePreset": "ninja-multi", |
| 32 | + "configuration": "Release" |
| 33 | + }, |
| 34 | + ... |
| 35 | + ] |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## 1. tasks.json |
| 42 | + |
| 43 | +Defines **custom build tasks** you can run with `Ctrl+Shift+B` or from the Command Palette. |
| 44 | + |
| 45 | +Even though you have `CMakePresets.json`, sometimes you still want tasks for: |
| 46 | + |
| 47 | +* running `ninja` or `make` manually |
| 48 | +* generating compile commands |
| 49 | +* running custom scripts |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "version": "2.0.0", |
| 54 | + "tasks": [ |
| 55 | + { |
| 56 | + "label": "CMake Configure (ninja-multi)", |
| 57 | + "type": "shell", |
| 58 | + "command": "cmake", |
| 59 | + "args": [ |
| 60 | + "--preset", |
| 61 | + "ninja-multi" |
| 62 | + ], |
| 63 | + "group": "build", |
| 64 | + "problemMatcher": [] |
| 65 | + }, |
| 66 | + { |
| 67 | + "label": "CMake Build Debug", |
| 68 | + "type": "shell", |
| 69 | + "command": "cmake", |
| 70 | + "args": [ |
| 71 | + "--build", |
| 72 | + "build", |
| 73 | + "--config", |
| 74 | + "Debug" |
| 75 | + ], |
| 76 | + "group": "build", |
| 77 | + "problemMatcher": ["$gcc"] |
| 78 | + }, |
| 79 | + { |
| 80 | + "label": "CMake Build Release", |
| 81 | + "type": "shell", |
| 82 | + "command": "cmake", |
| 83 | + "args": [ |
| 84 | + "--build", |
| 85 | + "build", |
| 86 | + "--config", |
| 87 | + "Release" |
| 88 | + ], |
| 89 | + "group": "build", |
| 90 | + "problemMatcher": ["$gcc"] |
| 91 | + } |
| 92 | + ] |
| 93 | +} |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +If you want to limit parallel jobs (e.g. 8 jobs max) |
| 98 | + |
| 99 | +```bash |
| 100 | +cmake --build build --config Release --parallel 8 |
| 101 | +``` |
| 102 | +So in tasks.json, adjust your build tasks like this: |
| 103 | + |
| 104 | +```json |
| 105 | + |
| 106 | +{ |
| 107 | + "label": "CMake Build Release", |
| 108 | + "type": "shell", |
| 109 | + "command": "cmake", |
| 110 | + "args": [ |
| 111 | + "--build", |
| 112 | + "build", |
| 113 | + "--config", |
| 114 | + "Release", |
| 115 | + "--parallel", |
| 116 | + "8" |
| 117 | + ], |
| 118 | + "group": "build", |
| 119 | + "problemMatcher": ["$gcc"] |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +``` |
| 124 | + |
| 125 | + |
| 126 | +**Important things to put:** |
| 127 | + |
| 128 | +* `label`: Name shown in the task list. |
| 129 | +* `command`: The tool you want to run (e.g., `cmake`, `ninja`, `make`). |
| 130 | +* `args`: Arguments (e.g., presets). |
| 131 | +* `problemMatcher`: Helps VS Code parse compiler errors and warnings. |
| 132 | +* `group`: (e.g., `build`) so it appears in *Run Build Task* menu. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## 2. settings.json |
| 137 | + |
| 138 | +This file **overrides workspace settings** just for this folder. |
| 139 | +When using C++ and CMake, this is very important for: |
| 140 | + |
| 141 | +* IntelliSense configuration |
| 142 | +* Default CMake presets |
| 143 | +* Code formatting |
| 144 | +* Compiler paths |
| 145 | + |
| 146 | + **Example**: |
| 147 | + |
| 148 | +```json |
| 149 | +{ |
| 150 | + // Configure on open |
| 151 | + "cmake.configureOnOpen": true, |
| 152 | + |
| 153 | + // Pick your default configure and build presets |
| 154 | + "cmake.defaultConfigurePreset": "ninja-multi", |
| 155 | + "cmake.defaultBuildPreset": "ninja-multi-release", |
| 156 | + |
| 157 | + // Let IntelliSense know about the compiler (optional but good for accuracy) |
| 158 | + "C_Cpp.default.compilerPath": "/usr/bin/clang++", // or gcc if you use gcc |
| 159 | + |
| 160 | + // Optional - adjust include paths for IntelliSense if you have custom headers |
| 161 | + "C_Cpp.default.includePath": [ |
| 162 | + "${workspaceFolder}/include", |
| 163 | + "${workspaceFolder}/external" |
| 164 | + ], |
| 165 | + |
| 166 | + // Optional - default IntelliSense mode |
| 167 | + "C_Cpp.default.intelliSenseMode": "linux-clang-x64", // or linux-gcc-x64 |
| 168 | + |
| 169 | + // Optional - use clang-format for formatting |
| 170 | + "C_Cpp.clang_format_style": "file", |
| 171 | + |
| 172 | + // Optional - build directory pattern so VS Code finds the right place |
| 173 | + "cmake.buildDirectory": "${workspaceFolder}/build" |
| 174 | +} |
| 175 | + |
| 176 | +``` |
| 177 | + |
| 178 | +**Important things to put:** |
| 179 | + |
| 180 | +* `cmake.defaultConfigurePreset` and `cmake.defaultBuildPreset` |
| 181 | +* `C_Cpp.default.compilerPath` |
| 182 | +* `C_Cpp.default.includePath` |
| 183 | +* `C_Cpp.default.defines` |
| 184 | +* Formatting and code style options |
| 185 | +* Enable `cmake.configureOnOpen` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +## 3. launch.json |
| 190 | + |
| 191 | +Defines **debug launch configurations**. Since we are using presets, VS Code can grab the executable path automatically using: |
| 192 | + |
| 193 | +```json |
| 194 | +"${command:cmake.launchTargetPath}" |
| 195 | +``` |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + **Example**: |
| 200 | +Here’s an example for debugging your app built in Release: |
| 201 | + |
| 202 | +```json |
| 203 | +{ |
| 204 | + "version": "0.2.0", |
| 205 | + "configurations": [ |
| 206 | + { |
| 207 | + "name": "Debug My App (Release)", |
| 208 | + "type": "cppdbg", |
| 209 | + "request": "launch", |
| 210 | + "program": "${command:cmake.launchTargetPath}", |
| 211 | + "args": [], |
| 212 | + "stopAtEntry": false, |
| 213 | + "cwd": "${workspaceFolder}", |
| 214 | + "environment": [], |
| 215 | + "externalConsole": true, |
| 216 | + "MIMode": "gdb", |
| 217 | + "setupCommands": [ |
| 218 | + { |
| 219 | + "description": "Enable pretty-printing for gdb", |
| 220 | + "text": "-enable-pretty-printing", |
| 221 | + "ignoreFailures": true |
| 222 | + } |
| 223 | + ] |
| 224 | + } |
| 225 | + ] |
| 226 | +} |
| 227 | + |
| 228 | +``` |
| 229 | + |
| 230 | +**Important things to put:** |
| 231 | + |
| 232 | +* `program`: Path to your executable (usually in `build/{preset}`). |
| 233 | +* `args`: Command-line arguments. |
| 234 | +* `cwd`: Working directory. |
| 235 | +* `MIMode`: `gdb` (Linux) or `lldb` (macOS) or `cppvsdbg` (Windows). |
| 236 | +* `setupCommands`: Enable pretty printing for better variable visualization. |
| 237 | +* `environment`: Any custom environment variables. |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +## Integrate CMakePresets |
| 242 | + |
| 243 | +Since you use `CMakePresets.json`, you can **directly refer to presets**: |
| 244 | + |
| 245 | +**Launch.json** with `cmake.buildDirectory`: |
| 246 | + |
| 247 | +```json |
| 248 | +"program": "${command:cmake.launchTargetPath}" |
| 249 | +``` |
| 250 | + |
| 251 | +This will pick the executable from your active CMake preset. |
| 252 | + |
| 253 | +**settings.json**: |
| 254 | + |
| 255 | +```json |
| 256 | +{ |
| 257 | + // Configure on open |
| 258 | + "cmake.configureOnOpen": true, |
| 259 | + |
| 260 | + // Pick your default configure and build presets |
| 261 | + "cmake.defaultConfigurePreset": "ninja-multi", |
| 262 | + "cmake.defaultBuildPreset": "ninja-multi-release", |
| 263 | + |
| 264 | + // Let IntelliSense know about the compiler (optional but good for accuracy) |
| 265 | + "C_Cpp.default.compilerPath": "/usr/bin/clang++", // or gcc if you use gcc |
| 266 | + |
| 267 | + // Optional - adjust include paths for IntelliSense if you have custom headers |
| 268 | + "C_Cpp.default.includePath": [ |
| 269 | + "${workspaceFolder}/include", |
| 270 | + "${workspaceFolder}/external" |
| 271 | + ], |
| 272 | + |
| 273 | + // Optional - default IntelliSense mode |
| 274 | + "C_Cpp.default.intelliSenseMode": "linux-clang-x64", // or linux-gcc-x64 |
| 275 | + |
| 276 | + // Optional - use clang-format for formatting |
| 277 | + "C_Cpp.clang_format_style": "file", |
| 278 | + |
| 279 | + // Optional - build directory pattern so VS Code finds the right place |
| 280 | + "cmake.buildDirectory": "${workspaceFolder}/build" |
| 281 | +} |
| 282 | + |
| 283 | +``` |
| 284 | + |
| 285 | +This makes everything auto-sync. |
| 286 | + |
| 287 | +--- |
0 commit comments