Cheatsheet: Build & Debug C in VS Code (Windows, MinGW/GDB)¶
0) Requirements for Windows¶
- Compiler: MinGW-w64 (for example in MSYS2 UCRT64: C:\msys64\ucrt64\bin\gcc.exe and gdb.exe)
- VS Code + Extensions:
- C/C++ (Microsoft)
Please, note the the compiler and GDB might be in a different path. You should know where they are installed or have it in the Windows Path
1) Project structure¶
project/
├─ src/
│ └─ main.c
├─ include/ (optional)
└─ .vscode/
├─ tasks.json
└─ launch.json
Example main.c:
#include <stdio.h>
int main(void) {
printf("Hello VS Code!\n");
return 0;
}
2) Compile configuration: .vscode/tasks.json¶
{
"version": "2.0.0",
"tasks": [
{
"label": "Build C Project",
"type": "process",
"command": "gcc",
"args": [
"-g",
"-O0",
"-std=c99",
"-I", "include",
"src/main.c",
"-o", "build/main.exe"
],
"problemMatcher": "$gcc",
"group": { "kind": "build", "isDefault": true },
"options": {
"env": {
"PATH": "C:\\msys64\\ucrt64\\bin;${env:PATH}"
}
}
}
]
}
Key parameters (tasks.json)¶
- type
- process: run gcc directly, no shell
- shell: use a shell (cmd/powershell), needed for &&, >, *.c
- command: the compiler (gcc)
- args:
- -g: include debug symbols (breakpoints)
- -O0: no optimizations (easier debugging)
- -std=c99: use the C99 standard
- -I include: add extra include path
- src/main.c: source file
- -o build/main.exe: output file
- problemMatcher: parses gcc output into clickable errors
- group: default build task (Ctrl+Shift+B)
- options.env.PATH: ensures gcc/gdb are found
3) Debug configuration: .vscode/launch.json¶
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug C Program",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main.exe",
"args": [],
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe",
"setupCommands": [
{ "description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true }
],
"preLaunchTask": "Build C Project"
}
]
}
Key parameters (launch.json)¶
- type
- cppdbg: for GCC/Clang with GDB/LLDB
- cppvsdbg: for MSVC (Visual Studio debugger)
- program: path to the executable
- cwd: working directory
- MIMode: use gdb for MinGW
- miDebuggerPath: path to gdb.exe
- preLaunchTask: build task to run first
4) Usage¶
- Build only: Ctrl+Shift+B (default build task)
- Debug with breakpoints: F5 and select *Debug C Program
- Run without debugging: Ctrl+F5 (same launch config, no debugger)
Notes¶
- Hollow (unbound) breakpoints mean:
- Wrong program path in launch.json
- Missing debug symbols (compile without -g)
- Optimizations enabled (use -O0 instead)
- Wrong gdb path
5) Variants¶
- Change program name:
- tasks.json: change output -o build/square.exe
- launch.json: update "program": "${workspaceFolder}/build/square.exe"
- Compile another source:
- tasks.json: replace src/main.c with the new file
- or create multiple tasks/configurations
6) Tips¶
- process type = no shell, more predictable
- shell type = shell features but quoting issues possible
- The C/C++ extension’s *Run C/C++ File* compiles only the active file, ignoring your configs. For projects, always use your own tasks/launch.json.