Setting up MinGW (via MSYS2) with VS Code
This guide explains how to install and configure the MinGW GCC compiler using MSYS2 and set it up in Visual Studio Code for C/C++ development.
- Install MSYS2
Download MSYS2 from the official website: https://www.msys2.org/
Run the installer and follow the instructions.
Recommended install path: C:\msys64
Open the MSYS2 MSYS terminal and update the package database:
pacman -Syu
If it asks you to close and reopen, do so, then run:
pacman -Su
- Install the MinGW-w64 Toolchain
In the MSYS2 MSYS terminal, install the MinGW GCC toolchain:
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
Verify installation:
gcc --version g++ --version gdb --version
- Add MinGW to PATH
Find your MinGW binary directory:
C:\msys64\mingw64\bin
Add this path to your System Environment Variables:
Press Win + R, type sysdm.cpl, and press Enter.
Go to Advanced → Environment Variables.
Edit the Path variable → Add:
C:\msys64\mingw64\bin
Restart your terminal/VS Code and check:
gcc --version
- Set Up VS Code
Download and install VS Code: https://code.visualstudio.com/
Install the C/C++ Extension Pack in VS Code: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack
Create a new C/C++ file (e.g., main.c).
Configure tasks.json to build with MinGW:
Go to Terminal → Configure Default Build Task.
Choose C/C++: g++.exe build active file.
Example .vscode/tasks.json:
{ "version": "2.0.0", "tasks": [ { "label": "build and run", "type": "shell", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Compile C++ with g++" } ] }
Run the program:
Press Ctrl + Shift + B to build.
Run the compiled .exe from the terminal.
- Debugging with VS Code
Go to Run → Add Configuration → C++ (GDB/LLDB).
Select g++.exe build and debug active file.
Example .vscode/launch.json:
{ "version": "0.2.0", "configurations": [ { "name": "C++ Debug", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "C:\msys64\mingw64\bin\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
- Useful Links
MSYS2: https://www.msys2.org/
VS Code: https://code.visualstudio.com/
C/C++ Extension Pack: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools-extension-pack
GCC Docs: https://gcc.gnu.org/onlinedocs/
✅ You’re now ready to build and debug C/C++ programs with MinGW + MSYS2 + VS Code.