Master VSCode and GDB: Complete Guide to C/C++ Debugging

This comprehensive tutorial walks you through installing VSCode and GDB on Linux, macOS, and Windows, configuring launch.json and tasks.json, mastering basic and advanced debugging techniques such as breakpoints, watchpoints, multithreaded and remote debugging, and troubleshooting common issues for C/C++ development.

Deepin Linux
Deepin Linux
Deepin Linux
Master VSCode and GDB: Complete Guide to C/C++ Debugging

Why Use VSCode + GDB

Accurate debugging is essential for C/C++ development. VSCode provides a lightweight, cross‑platform editor, while GDB offers a powerful command‑line debugger. Combined they give a fast, feature‑rich debugging environment.

Environment Setup

Install VSCode

Download VSCode from https://code.visualstudio.com/ and install the package for your OS.

Install GDB

Linux :

sudo apt-get update && sudo apt-get install -y gdb

macOS : brew install gdb (additional code‑signing may be required).

Windows : Install MinGW or TDM‑GCC, which include GDB, and add the bin directory to PATH.

Install the C/C++ Extension

In VSCode open the Extensions view (Ctrl+Shift+X), search for “C/C++”, and install the Microsoft extension. It adds syntax highlighting, IntelliSense, and debugging support.

Configure VSCode for GDB

launch.json

Create a debugging configuration via Run → Add Configuration → C++ (GDB/LLDB) . A typical launch.json is:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C++ Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/a.out",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "console": "integratedTerminal",
      "MIMode": "gdb",
      "setupCommands": [
        { "description": "Enable pretty‑printing", "text": "-enable-pretty-printing", "ignoreFailures": true }
      ]
    }
  ]
}

Key fields: program: absolute or workspace‑relative path to the executable (must be built with -g). stopAtEntry: true pauses at main, false starts running immediately. MIMode: selects GDB as the backend. setupCommands: runs before the session (e.g., enable pretty‑printing).

tasks.json (optional)

Automate compilation with a build task. Example:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "group": { "kind": "build", "isDefault": true },
      "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": true, "clear": false },
      "problemMatcher": ["$gcc"]
    }
  ]
}

Press Ctrl+Shift+B to compile the active file with debugging symbols.

Basic Debugging Operations

Setting Breakpoints

Click the gutter next to a line to create a red breakpoint, or use the GDB console command break line_number or break function_name. VSCode also supports conditional, log, and function breakpoints via the context menu.

Running and Continuing

Start debugging with the green “Start” button or the GDB command run (alias r). Use the “Continue” button or continue (alias c) to resume execution until the next breakpoint.

Viewing Variables

The “Variables” pane shows all in‑scope variables when execution stops. In the GDB console you can also type print variable_name (or p variable_name) to display a specific variable or expression.

Step Execution

step

steps into the next line (including function calls). next steps over a function call, executing it as a single step.

Watchpoints

Set a watchpoint with watch expression. The program pauses whenever the value of the expression changes.

Jump Command

Use jump line_number or jump *address to move the instruction pointer to a different line or memory address, useful for skipping code sections.

Advanced Debugging Techniques

Conditional Breakpoints

Right‑click a breakpoint, choose “Add Condition”, and enter an expression (e.g., i == 10). The breakpoint triggers only when the condition evaluates to true.

Multithreaded Debugging

The “Threads” panel lists all active threads. Select a thread to view its call stack and variables. Thread‑specific breakpoints can be set by specifying a thread ID in the breakpoint dialog.

Remote Debugging

To debug a program running on a remote host:

Install the “C/C++” and “Remote‑SSH” extensions.

Use Remote‑SSH: Connect to Host… to open an SSH session to the remote machine.

Ensure the remote machine has a C/C++ compiler and GDB installed.

Create a launch configuration that includes miDebuggerServerAddress with the remote IP and port (the remote GDB server must be started, e.g., gdbserver :1234 ./a.out).

Set breakpoints locally and start debugging; VSCode forwards commands over SSH.

Example remote launch.json entry:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Remote C++ Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "/path/to/executable",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "console": "integratedTerminal",
      "MIMode": "gdb",
      "miDebuggerServerAddress": "remote_host_ip:1234",
      "setupCommands": [
        { "description": "Enable pretty‑printing", "text": "-enable-pretty-printing", "ignoreFailures": true }
      ]
    }
  ]
}

Common Issues and Solutions

GDB not found : Verify installation with gdb -v and ensure the binary directory is in PATH.

Launch configuration errors : Check that program points to the correct executable and that MIMode is set to gdb.

Breakpoints not hit : Re‑compile with -g, ensure the breakpoint is on an executable line, and confirm the binary matches the source.

Remote connection failures : Verify network connectivity, correct IP/port in miDebuggerServerAddress, and that firewalls allow the chosen port.

debuggingCVSCodeGDBremote debugginglaunch.jsontasks.json
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.