Solarian Programmer

My programming ramblings

Install GCC 9 on Windows - Build C, C++ and Fortran programs

Posted on November 5, 2019 by Paul

In this article, I will show you how the install the GCC compilers on your Windows system using the MSYS2 software distribution.

The advantage of this setup is that you will be able to build C, C++ and Fortran programs on your Windows machine and generate native Windows executables. Also, MSYS2 tends to offer the latest stable versions of GCC, which at the time of this writing is 9.2.

There is also a video version of this tutorial:

Another advantage of using the MSYS2 software distribution is that you will be able to install, if necessary, other C and C++ libraries like SDL2 or OpenCV using the pacman package manager which is included with MSYS2.

Please note that all graphical libraries provided by MSYS2 use the native Win32 API. This means that when you build for example an OpenGL application it will use a native Windows Win32 window.

Get the MSYS2 installer. I recommend to use the 64 bits version if possible:

MSYS2 installer

Once the download is finished, start the installer and accept the defaults. You should end up with a screen that asks if you want to Run MSYS2 64bit now., make sure it is checked and press Finish.

After the above step, a command window, or a Terminal, should open. Write this in the MSYS Terminal:

1 $ pacman -Syu

and write Y when asked if you want to proceed with the installation. This should update the base system.

If you get an error with the above command, try this approach:

1 $ pacman -Sydd filesystem
2 $ pacman -Su

When finished, you should see a message about closing your terminal window. Use the X button to close the window.

Next, from your Windows Start menu, go to MSYS2 64 bitMSYS2 MSYS:

Windows Start menu MSYS2 MSYS

Please note, that in the above image you have three entries, the MSYS2 MSYS is the one you will want to use every time you need to install a package, search for a package or upgrade a package with the pacman package manager.

Repeat the above command to finish the system upgrade:

1 $ pacman -Syu

Once the update is finished, you are ready to search and install packages using the pacman package manager. For example, if you want to search for gcc use the next command:

1 $ pacman -Ss gcc

you should see a long list with all available packages that contain the gcc string. We are interested in the 64 bits packages only, so a better option is to filter the results to show only the ones that contain the mingw64 string:

1 $ pacman -Ss gcc | grep mingw64

Now, the list of packages is cleaner and only shows the 64 bits versions. This is what I see on my machine, as of November 2019. You will probably see slightly newer versions of the packages:

1 $ pacman -Ss gcc | grep mingw64
2 mingw64/mingw-w64-x86_64-gcc 9.2.0-2 (mingw-w64-x86_64-toolchain)
3 mingw64/mingw-w64-x86_64-gcc-ada 9.2.0-2 (mingw-w64-x86_64-toolchain)
4 mingw64/mingw-w64-x86_64-gcc-fortran 9.2.0-2 (mingw-w64-x86_64-toolchain)
5 mingw64/mingw-w64-x86_64-gcc-libgfortran 9.2.0-2 (mingw-w64-x86_64-toolchain)
6 mingw64/mingw-w64-x86_64-gcc-libs 9.2.0-2 (mingw-w64-x86_64-toolchain)
7 mingw64/mingw-w64-x86_64-gcc-objc 9.2.0-2 (mingw-w64-x86_64-toolchain)
8 mingw64/mingw-w64-x86_64-lcov 1.14-1
9 mingw64/mingw-w64-x86_64-perl 5.28.0-1

From the above, we see that the GCC 9.2 compiler is available in a larger group of packages named mingw-w64-x86_64-toolchain. Let’s install it:

1 $ pacman -S mingw-w64-x86_64-toolchain

Press Enter to select the defaults when asked what you want to install from this group. Write Y when asked if you want to proceed with the installation.

Optionally, if you know that you need a Terminal based text editor like nano or vim you can install it with:

1 $ pacman -S vim


1 $ pacman -S nano

Optionally, if you know that you need it, you can install the Clang compiler with:

1 $ pacman -S mingw-w64-x86_64-clang

We are done with the installation. Write exit in the MSYS2 Terminal to exit cleanly:

1 $ exit

Next, when you want to compile and run C, C++ or Fortran programs you will need to go to Windows Start → MSYS2 64 bitMSYS2 MinGW 64-bit:

Windows Start menu MSYS2 MinGW 64-bit

be careful which option you select, otherwise you will get errors when you try to use the compilers.

Check GCC’s version with:

1 $ gcc --version

This is what I see, on my machine:

1 $ gcc --version
2 gcc.exe (Rev2, Built by MSYS2 project) 9.2.0
3 Copyright (C) 2019 Free Software Foundation, Inc.
4 This is free software; see the source for copying conditions.  There is NO

Similarly, you can check gfortran’s version, with:

1 $ gfortran --version
2 GNU Fortran (Rev2, Built by MSYS2 project) 9.2.0
3 Copyright (C) 2019 Free Software Foundation, Inc.
4 This is free software; see the source for copying conditions.  There is NO

By default, your MSYS2 MinGW 64-bit Terminal starts in your home folder (like on a macOS or Linux system). You can find the MSYS2 home folder from Windows by going to C:\msys2\home\user_name where user_name is your user’s name.

You can go to your home folder from the MSYS2 MinGW 64-bit Terminal with:

1 $ cd ~

Next, create or add a C++ program named fs_test.cpp into your home folder with the next content:

1 #include <iostream>
2 #include <filesystem>
4 int main() {
5     for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
6         std::cout << file.path() << '\n';
7     }
8 }

The above program uses C++17 Filesystem to recursively list all files and directories from your home.

Double check that you have a C++ file named fs_test.cpp in your home folder:

1 $ cd ~
2 $ ls

If you see the above file name, you are ready to build it. This is how to compile it using GCC:

1 $ g++ -std=c++17 -Wall -Wextra -pedantic fs_test.cpp -o fs_test

If you see no error, run the generated executable with:

1 $ ./fs_test

Here is an example of a simple Fortran program:

1 program hello
2     implicit none
3     real:: x = 2.5, y = 4.0
4     print*, "Hello, from Fortran!"
5     print*, "x + y = ", x + y
6 end program hello

save the program as hello.f90 and build it with:

1 $ gfortran hello.f90 -o hello

This is what I see if I run the above program:

1 $ ./hello
2  Hello, from Fortran!
3  x + y =    6.50000000

A word of warning, GCC and Clang under MSYS2 have some platform limitations, for example you can’t use the sanitizers, Clang needs to use the C++ standard library provided by GCC and so on … If you want the full GCC or Clang experience you will need to use Linux or macOS. On Windows you can also install WSL or install a Linux distribution in a virtual machine. Another option is to install Cygwin.

If you want to learn more about C++17 I would recommend reading C++17 in Detail by Bartlomiej Filipek:

If you need to brush your Fortran knowledge a good book is Modern Fortran Explained by M. Metcalf, J. Reid and M. Cohen:

Show Comments