Cheatsheet: Build & Debug C in VS Code (Windows, MinGW/GDB)¶
This cheatsheet explains how to integrate building and debugging into the VS Code user interface using
tasks.json and launch.json.Before using it, complete Setting up Visual Studio Code. This page assumes that VS Code, the Microsoft C/C++ extension,
gcc and gdb are already installed and available.Project structure¶
A simple project can use the following structure:
project/ ├─ src/ │ └─ main.c ├─ include/ (optional) ├─ build/ └─ .vscode/ ├─ tasks.json └─ launch.json
Create the
build directory before compiling. GCC does not create the output directory automatically.Example main.c:
#include <stdio.h>
int main(void) {
printf("Hello VS Code!\n");
return 0;
}
Compile configuration: .vscode/tasks.json¶
Create the file
.vscode/tasks.json:{
"version": "2.0.0",
"tasks": [
{
"label": "Build C Project",
"type": "process",
"command": "gcc",
"args": [
"-Wall",
"-g",
"-O0",
"-std=c99",
"-I", "include",
"src/main.c",
"-o", "build/main.exe"
],
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Key parameters (tasks.json)¶
- type
** process: runs
** shell: runs the command through a shell (cmd/PowerShell), which is needed for shell features such as
gcc directly, without a shell** shell: runs the command through a shell (cmd/PowerShell), which is needed for shell features such as
&&, > or *.c- command: the compiler to run (
gcc) - args:
**
**
**
**
**
**
**
-Wall: enable common compiler warnings**
-g: include debug information**
-O0: disable optimizations, making debugging easier**
-std=c99: use the C99 standard**
-I include: add the include directory to the header search path**
src/main.c: source file to compile**
-o build/main.exe: output executable- problemMatcher: lets VS Code interpret GCC errors and warnings
- group: makes this the default build task (
Ctrl+Shift+B)
Debug configuration: .vscode/launch.json¶
Create the file
.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"
}
```
]
}
If MSYS2 is installed in a different location, update
miDebuggerPath to point to your gdb.exe.Key parameters (launch.json)¶
- type
** cppdbg: debugging with GCC/Clang and GDB/LLDB
** cppvsdbg: debugging with the Microsoft Visual C++ debugger
** cppvsdbg: debugging with the Microsoft Visual C++ debugger
- program: path to the executable to debug
- cwd: working directory
- MIMode: use
gdbwith MinGW - miDebuggerPath: path to
gdb.exe - preLaunchTask: build task that VS Code runs before starting the debugger
Usage¶
- Build the program with Ctrl+Shift+B.
- Set a breakpoint by clicking next to a line number, or press F9 on the selected line.
- Start debugging with F5 and select Debug C Program if VS Code asks for a configuration.
- Use the Debug toolbar to continue, step over, step into or stop the program.
Notes¶
- A hollow (unbound) breakpoint can indicate:
** the wrong executable path in
** missing debug information (the program was compiled without
** compiler optimizations are enabled
** an incorrect GDB path
launch.json** missing debug information (the program was compiled without
-g)** compiler optimizations are enabled
** an incorrect GDB path
- VS Code can automatically create build and debug configurations for the active file. The configuration shown on this page is useful when you want explicit control over how your project is built and debugged.
- The
preLaunchTaskvalue must match thelabelof the build task exactly.
Variants¶
- Change the program name:
** In
** In
tasks.json, change -o build/main.exe** In
launch.json, update "program": "${workspaceFolder}/build/main.exe"- Compile another source file:
** Replace
** or create additional tasks and debug configurations
src/main.c in tasks.json** or create additional tasks and debug configurations
- For projects with several source files, add the required
.cfiles to the compiler arguments or use a build system such as Make or CMake.