How to Install GCC and GDB on Windows Using MSYS2 — Tutorial

Sajid Anam Ifti
3 min readNov 22, 2023

--

C Programming.

For developers transitioning to or working in a Windows environment, setting up a robust toolchain for C and C++ programming is crucial. In this tutorial, I will guide you through the process of installing GCC (GNU Compiler Collection) and GDB (GNU Debugger) on Windows using MSYS2, a software distribution and building platform.

Step 1: Download and Install MSYS2

Begin by downloading the MSYS2 installer from the official website. Follow the installation instructions, ensuring that you choose the appropriate architecture (32-bit or 64-bit) for your system.

Download Link: MSYS2

MSYS2 Download Page.

Step 2: Update the System and Install Base Packages

Once MSYS2 is installed, open the MSYS2 MinGW terminal and update the package database along with the base packages using the following commands one by one:

pacman -Syu
pacman -Su

Tip: Ctrl+V or Ctrl+Shift+V may not work on Msys2 shell window. Use right click to paste anything. Also, just press Enter whenever there is a prompt in the terminal. It will default to Y or Yes. You can also type Y and then press Enter. Both works.

MSYS2 Window.

Step 3: Install GCC for C and C++

Now, open MSYS2 shell once again. To install GCC for both 32-bit and 64-bit architectures, use the following commands:

For 64-bit:

pacman -S mingw-w64-x86_64-gcc

For 32-bit:

pacman -S mingw-w64-i686-gcc

Step 4: Install GDB for C and C++ (Optional)

To install GDB for debugging, use the following commands:

For 64-bit:

pacman -S mingw-w64-x86_64-gdb

For 32-bit:

pacman -S mingw-w64-i686-gdb

Step 5: Verify Installations on MSYS2

After installing GCC and GDB, verify the versions to ensure successful installations on MSYS2:

gcc --version
g++ --version
gdb --version

Step 6: Set the Path Environment Variable

To make the installed tools globally accessible from the command line, add the MSYS2 binary directory to the system’s PATH environment variable. To do that-

6.1. Locate MSYS2 MINGW64 Binaries

Find and copy the path of the mingw64 binary folder (bin). It should be located where you installed MSYS2. Typically, it resides in C drive.

Sample Path:

C:\msys64\mingw64\bin

6.2. Open Environment Variable Editing Panel

Open the start menu and search for Edit Environment Variables for Your Account. Open it.

Search for Edit Environment Variables for Your Account.

6.3. Click on Environment Variables Button

To edit environment variables, click on the Environment Variables button.

Environment Variables
Environment Variables.

6.4. Edit PATH Variable

Under the System Variables tab, select Path. Then, click on Edit button.

System Variables
System Variables.

6.5. Add The Copied Path

Click on New. Paste the copied path (C:\msys64\mingw64\bin).

Paste the copied path
Paste the copied path.

Now, click on OK button of every opened window.

6.6. Verify Installation on Windows

Open a PowerShell or Windows Terminal window and run the following commands:

gcc --version
g++ --version
gdb --version

If all steps are done correctly, you will see some outputs describing the version information.

PowerShell
Verify Installation on PowerShell.

All set! Ready to use GCC and GDB on your Windows machine.

--

--