How to Install C and C++ Compilers on Windows

If you want to run C or C++ programs in your Windows operating system, then you need to have the right compilers.

How to Install GCC and GDB on Windows Using MSYS2

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: 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-

5.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

5.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.

5.3. Click on Environment Variables Button

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

Environment Variables
Environment Variables.

5.4. Edit PATH Variable

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

System Variables
System Variables.

5.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.

5.6. Verify Installation on Windows

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

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

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

Leave a Comment