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


Step 1: Install the C/C++ extension

  1. Open VS Code.
  2. Press Ctrl+Shift+X to open Extensions.
  3. Search for C/C++ by Microsoft.
  4. Click Install.
  5. Restart VS Code.

Step 2: Install MinGW-w64 (compiler + debugger)

Option A (recommended): Install via MSYS2

  1. Download MSYS2 from msys2.org.
  2. Open MSYS2 terminal and update packages:
pacman -Syu
  1. Install GCC and GDB:
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-gdb
  1. Add C:\msys64\mingw64\bin to your system PATH.

Option B: Standalone MinGW-w64


Step 3: Verify installation

Open PowerShell or Command Prompt:

g++ --version
gdb --version

You should see GCC/GDB version info.


Step 4: Create a test project

  1. Create a folder:
mkdir C:\cpp-hello
cd C:\cpp-hello
  1. Open in VS Code:
code .
  1. 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": true,
      "MIMode": "gdb",
      "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe"
    }
  ]
}

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/mingw64/bin/g++.exe",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-gcc-x64"
    }
  ]
}

Step 8: Build and run

  1. Build: Press Ctrl+Shift+B. → Output: build\main.exe
  2. Run:
.\build\main.exe

Output:

Hello, C++ from Windows with MinGW!
  1. Debug:

Step 9: Common issues

Quick reference