Enable C++ Development in VS Code on Windows (MinGW-w64 + GCC + GDB)
Step-by-step guide to configure C++ compiling and debugging in Visual Studio Code on Windows using MinGW-w64.
Prerequisites
- Windows 11 (Windows 10 also works). On Windows 11 on Arm (e.g., Snapdragon laptops), the x64 toolchain below runs through Windows’ built-in x64 emulation.
- Visual Studio Code
- MinGW-w64 (GCC + GDB), via MSYS2 or standalone
Step 1: Install the C/C++ extension
- Open VS Code.
- Press
Ctrl+Shift+Xto open Extensions. - Search for C/C++ by Microsoft.
- Click Install.
- Restart VS Code.
Step 2: Install MinGW-w64 (compiler + debugger)
Option A (recommended): Install via MSYS2
- Download and run the installer from msys2.org.
Keep the default install folder,
C:\msys64. - From the Start menu, open MSYS2 UCRT64 and update packages:
pacman -Syu
If the terminal closes to finish the update, reopen MSYS2 UCRT64 and run
pacman -Syu again.
- Install the toolchain (GCC, GDB, and related tools). Press Enter to accept the default selection, then Y to confirm:
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
- Add
C:\msys64\ucrt64\binto your PATH. On Windows 11: open Start, search for Edit environment variables for your account, select Path under User variables, click Edit → New, paste the folder, then click OK in each dialog.
Older guides use the mingw64 environment (mingw-w64-x86_64-* packages and
C:\msys64\mingw64\bin). It still works; UCRT64 is MSYS2’s current default. If you already
installed mingw64, either keep it and use C:\msys64\mingw64\bin everywhere this
guide says ucrt64, or switch fully to UCRT64. Don’t put both on PATH.
Option B: Standalone MinGW-w64
- mingw-w64.org doesn’t host an installer itself; its downloads page links to prebuilt toolchains. A common choice is WinLibs (pick the UCRT, Win64 zip).
- Extract it to a folder without spaces, such as
C:\mingw64, then addC:\mingw64\binto PATH as above. - In Steps 6 and 7, replace
C:/msys64/ucrt64/binwith your folder.
Step 3: Verify installation
Open a new PowerShell or Command Prompt window (windows opened before you changed PATH won’t see it):
g++ --version
gdb --version
You should see GCC/GDB version info. If VS Code was already open, close it completely and reopen it so it picks up the new PATH.
Step 4: Create a test project
- Create a project folder and its
buildoutput folder. Thebuildfolder is required;g++won’t create it for you.
mkdir C:\cpp-hello
cd C:\cpp-hello
mkdir build
- Open in VS Code:
code .
- Create
main.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, C++ from Windows with MinGW!" << endl;
return 0;
}
Step 5: Configure build tasks (tasks.json)
Create .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C++: Build with g++",
"command": "g++",
"args": [
"-std=c++17",
"-g",
"main.cpp",
"-o",
"build\\main.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
Step 6: Configure debugging (launch.json)
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++: Debug build/main.exe (GDB)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\build\\main.exe",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "C++: Build with g++",
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe"
}
]
}
"externalConsole": false runs the program in VS Code’s integrated terminal. With
true, it opens a separate window that closes as soon as the program ends.
Step 7: Configure IntelliSense (c_cpp_properties.json)
Create .vscode/c_cpp_properties.json:
{
"version": 4,
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/ucrt64/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
]
}
Step 8: Build and run
- Build: Press
Ctrl+Shift+B. → Output:build\main.exe - Run:
.\build\main.exe
Output:
Hello, C++ from Windows with MinGW!
- Debug:
- Go to Run and Debug (
Ctrl+Shift+D) - Select C++: Debug build/main.exe (GDB)
- Start debugging, set breakpoints, step through code
Step 9: Common issues
g++not found: Ensure the MinGW-w64binfolder (e.g.,C:\msys64\ucrt64\bin) is in PATH, then fully restart VS Code.cannot open output file build\main.exe: Thebuildfolder doesn’t exist. Runmkdir buildin the project folder.The value of miDebuggerPath is invalid: ConfirmmiDebuggerPathpoints to agdb.exethat exists, in the same folder as theg++you build with.- IntelliSense mismatch: Update
compilerPathto the sameg++.exe. - Spaces or OneDrive in the project path: Keep the project in a short path without
spaces, such as
C:\cpp-hello, rather than a OneDrive-synced Documents folder. - Windows blocks
main.exe: On some Windows 11 PCs, Smart App Control or antivirus may block newly compiled, unsigned programs. Check Windows Security → App & browser control.
Quick reference
- Build:
Ctrl+Shift+B - Debug:
Ctrl+Shift+D→ Start - Compiler: MinGW-w64
g++ - Debugger:
gdb