Install GCC 9 on Windows - Build C, C++ and Fortran programs
Posted on November 5, 2019 by Paul
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:
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:
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:
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 bit → 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:
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:
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:
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:
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:
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:
Optionally, if you know that you need it, you can install the Clang compiler with:
We are done with the installation. Write exit in the MSYS2 Terminal to exit cleanly:
Next, when you want to compile and run C, C++ or Fortran programs you will need to go to Windows Start → MSYS2 64 bit → 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:
This is what I see, on my machine:
Similarly, you can check gfortran’s version, with:
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:
Next, create or add a C++ program named fs_test.cpp into your home folder with the next content:
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:
If you see the above file name, you are ready to build it. This is how to compile it using GCC:
If you see no error, run the generated executable with:
Here is an example of a simple Fortran program:
save the program as hello.f90 and build it with:
This is what I see if I run the above program:
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: