C++ Project - Debug & Build (VSCode) 🐛

How to set up C++ Compiler with Visual Studio Code. - Setup Visual Studio Code for Multi-File C++ Projects

see also

# Toolings

# _clangd ⮺

This is an opensource alternatives to microscoft extension which are not supported by VSCodium or Code-server. Provides C/C++ language IDE features for VS Code

  • code completion
  • compile errors and warnings
  • go-to-definition and cross references
  • include management
  • code formatting
  • simple refactorings

Fill more snappy and less ressource hungry than microsoft native solution.

For global includes the best way to expose them is to have .clangd file:

CompileFlags:
  Add: [
    "-I/home/yves/DEV/cpp/"
  ]

For local includes that are spread out in project workspace (my CodingGame style)…

Here the workaround is to have script to generate a compile_flags.txt (since there is no matching compilatino), to expose them to clangd (it can be replayed anytime you need).

clangd_init.rb
#!/usr/bin/env ruby
require 'set'
require 'pathname'

root = (ARGV[0] || Dir.pwd)
root_path = Pathname.new(root).realpath

header_dirs = Set.new

# Collect directories containing .hh files
Dir.glob(File.join(root_path, "**/*.hh")).each do |file|
  header_dirs << File.dirname(Pathname.new(file).realpath.to_s)
end

# Always include project root
header_dirs << root_path.to_s

flags = []
flags << "-std=c++20"
flags << "-x"
flags << "c++"

header_dirs.each do |dir|
  flags << "-I#{dir}"
end

File.write(File.join(root_path, "compile_flags.txt"), flags.join("\n") + "\n")

puts "Generated compile_flags.txt with #{header_dirs.size} include paths"

It would output something very concise like:

-std=c++20
-x
c++
-I/home/yves/DEV/Codingame/CG-Winter-Challenge-2026-SnakeByte
-I/home/yves/DEV/Codingame/CG-Winter-Challenge-2026-SnakeByte/smitsimax

# C/C++ Extension Pack

Is the dark side stronger? No… no… no. Quicker, easier, more seductive. - Yoda

This is the microsoft extension that work only with VSCode. It is also quite ressources hungry. This is the easy path -

Notes
In ~/.cache/vscode-cpptools you will find that VSCode store a huge amount of file info, that you can safely clean to regain some disk spaces.

# Others

# Quick start

# Code runner ⮺

Install Code runner extension.

Customize it inside your project to have includes path, eg:

// Edit Executor map
"code-runner.executorMap": {
        "cpp": "cd $dir && g++ -std=c++20 -I ~/DEV/cpp -g $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

For one file program, it’s good enough => It will compile them with debug info, in an executable of same name (use )

# Debug w/MS

To debug this single files, use the C/C++ microsoft extension and invoke directly the gdb config (using current file as target).

.vscode/launch.json
// in .vscode/launch.json
{
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "Set Disassembly Flavor to Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }
  ],
  "version": "2.0.0"
}

# Build task ⮺ - ctrl+shift+b

VSCode’s build in keyboard shortcut to task by making it of type build. The easiest way to set up tasks is to press ctrl+shift+b.

.vscode/tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "make",	// your shell command here
            "group": {
                "kind": "build",
                "isDefault": true,
            },
            
            "problemMatcher": "$gcc"
        }
    ]
}

# Debug Task ⮺ / LLDB / GDB

Ctrl+F5 => Run / F5 => Debug

launch.json

"preLaunchTask": "build"

# C/C++ configurations ⮺

Written on September 19, 2020, Last update on March 25, 2026
debug-c++ vscode gdb c++ build-system