Debug & Build C++ Project (VSCode)

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

see also

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.

Quick start

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 )

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

// 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

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.

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",
            "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 September 28, 2024
debug-c++ vscode gdb c++