VSCode Debugger

Table of contents
  1. C++ Debugging (Apple Silicon)
    1. Compiler Configuration
    2. Debug Configuration
      1. Tasks
      2. Launch

C++ Debugging (Apple Silicon)

Required extensions:

ms-vscode.cpptools
vadimcn.vscode-lldb

Wiil be using lldb debugger.

In your .vscode directory, ultimately three files will be created:

c_cpp_properties.json  # Compiler path, etc.
launch.json  # CLI arguments, etc.
tasks.json  # Pre-build and post-build tasks go here

Compiler Configuration

Open Command Palette and search for C/C++: Edit Configurations (JSON). Default settings will be created in c_cpp_properties.json.

Some things you often want to change:

  • compilerPath: Default is /usr/bin/clang, but you could change it to /usr/bin/g++, etc.
  • cppStandard: Default is c++17, but you could change it to c++11, etc.
Example `c_cpp_properties.json`
{
  "configurations": [
    {
      "name": "Mac",
      "includePath": ["${workspaceFolder}/**"],
      "defines": [],
      "macFrameworkPath": [
        "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
      ],
      "compilerPath": "/usr/bin/g++",
      "cStandard": "c17",
      "cppStandard": "c++11",
      "intelliSenseMode": "macos-clang-arm64"
    }
  ],
  "version": 4
}

Debug Configuration

Open Command Palette and search for C/C++: Add Debug Configuration.

Select whichever compiler you are using, and the basic prebuild tasks and launch configurations will be created.

Tasks

In tasks.json, you can add pre-build and post-build tasks.

Some things you often want to change:

  • label: Task name, if you’re adding multiple tasks
    • This value is referenced in launch.json
  • args: Compiler arguments
    • Removing ${file} and replace with ${fileDirname}/*.cpp, so that all .cpp files in the directory are compiled (useful for debugging)

      If you do not remove ${file}, the compiler will complain about multiple definitions.

    • Adding -std=c++11, etc. to enforce a specific C++ standard

Example `tasks.json`
{
  "tasks": [
    {
      "type": "cppbuild",
      "label": "Build all files in directory",
      "command": "/usr/bin/g++",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${fileDirname}/*.cpp",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
        "-std=c++11",
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": "build",
      "detail": "Task generated by Debugger."
    }
  ],
  "version": "2.0.0"
}

Launch

You need to set the debugger type to lldb in launch.json.

{
  ...
  "type": "lldb",
  ...
}

With lldb, remove the unsupported options:

These unsupported options will be highlighted yellow.

  • stopAtEntry
  • environment
  • externalConsole
  • MIMode

Some things you often want to change:

  • name: To remind yourself what this configuration is for
  • type: lldb
  • program: Path to the binary file

    If you modified the output binary name in tasks.json, you need to change the program in launch.json to match.

  • preLaunchTask: The task name in tasks.json

    If you modified the label in tasks.json, you need to change the preLaunchTask in launch.json to match.

Example `launch.json`
{
  "configurations": [
    {
      "name": "Lets' Debug!",
      "type": "lldb",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "cwd": "${fileDirname}",
      "preLaunchTask": "Build all files in directory",
    }
  ],
  "version": "2.0.0"
}

References: