Setting Up C/C++ Debug Environment in VSCode

tasks.json Configuration

Press Ctrl+Shift+P and select "Open User Tasks" to create a reusable tasks.json template.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello",
            "type": "shell",
            "command": "echo initial",
            "problemMatcher": [],
            "group": "test"
        },
        {
            "type": "shell",
            "label": "compile with gcc",
            "command": "gcc",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Compiles the active C file."
        },
        {
            "type": "shell",
            "label": "test gcc compilation",
            "command": "gcc",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "detail": "Compiles the active C file for testing."
        },
        {
            "type": "shell",
            "label": "compile with g++",
            "command": "g++",
            "args": [
                "-I${workspaceFolder}/src/include",
                "${file}",
                "-g",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Compiles the active C++ file."
        }
    ]
}

Sync this configuration to GitHub Gist for portability across different machines.

launch.json Configuraton

Open the debugger configuration file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C Debug Session",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "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 syntax",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "compile with gcc"
        }
    ]
}

The preLaunchTask property must match a label defined in tasks.json. This ensures compilasion occurs automatically before debugging starts. Without the -g flag during compilation, breakpoints will not be functional.

Tags: VSCode C C++ debugging configuration

Posted on Sun, 09 Aug 2026 16:14:24 +0000 by tomharding