MinGW: How to install the GCC for Windows

A page dedicated to the installation and the use of MinGW toolchains to create MS Windows executables using the GNU Compiler Collection.

Porting the GCC to Windows

The GNU Compiler Collection (the GCC for short) is a key component of the GNU toolchain that allows the compilation of source code written in C, C++, Objective C, Objective C++, Fortran, Java, Ada, or Go. The GCC has been primarily developed for the UNIX Operating System (which now custimarily but not exclusively means any Linux OS variant). However, given that the MS Windows OS enjoys a wider popularity among end users and developers alike, various groups of open source developers have decided to “port” the GCC and its associated toolchain to Windows.

A GCC porting can be done in different ways, e.g., by creating a complete UNIX environment layer on Windows (the approach taken by the Cigwin developers) or by  creating a minimalist development environment to compile native Microsoft Windows applications. The latter approach, which allows programmers to generate Windows executables directly using a GCC toolchain, goes under the name of MinGW (Minimalist GNU for Windows). This is the approach we use, as it allows us to compile and run C/C++/Fortran/Go applications under Windows directly, both for development and for production purposes. In many cases, a GCC-compiled program built for Windows can be then ported to Linux with minor changes, if any.

MinGW in general

MinGW is a minimalist UNIX layer that allows to use GNU applications from within MS Windows. Differently from other environments such as Cygwin, MinGW provides a complete Open Source programming toolset for the development of native MS Windows applications which do not depend on any third-party C-Runtime DLLs. In other words, source code compiled and linked using a MinGW GCC toolchain is a true native executable that runs under MS Windows directly, without the need for any extra software  acting as a wrapper. A MinGW release includes:

  1. A port of the GNU Compiler Collection (GCC), which provides, at the time of writing, compilers for C, C++, Objective C, Objective C++, Fortran, Java, Ada, and Go. MinGW compilers provide access to the functionality of the Microsoft C runtime and some language-specific runtimes.
  2. GNU binary utils for Windows (i.e., assembler, linker, archive manager).

As part of the GCC compilation and linkage toolchain,  MinGW often uses tools from the MSYS2 distribution. MSYS2, which is a contraction of Minimal SYStem version 2, is a Bourne Shell command line interpreter offered as an alternative to Microsoft’s cmd.exe. MSYS provides a general purpose command line environment used in conjunction with MinGW to port  many Open Source applications to the MS-Windows platform. MSYS2 is a light-weight fork of Cygwin that includes a small selection of Unix tools chosen to facilitate the porting objective. The version of make distributed with MSYS2 is a typical tool used in the MinGW GCC toolchain (in place of MinGW’s own version of make).

There are basically two different “strains” of MinGW releases:

  • The MinGW release from the MinGW project, the original project that only supports a 32 bit software architecture (both as host and target) for the GCC. This means that such MinGW release (also known as MinGW-W32) only works on 32 bit Windows versions and generates executables that only run on 32 bit Windows versions.
  • The MinGW suite of releases from the MinGW-W64 project, which supports both 64 bit and 32 bit GCC versions.  MinGW-W64 also supports a much larger part of the Windows API. More specifically, MinGW-W64 releases cross compilers that allow the creation of executables and DLLs targeting 64 bit architectures (HW: x86_64; SW:Win64) and 32 bit architectures (HW: i686; SW: Win32). This is done using a host architecture that can be either x86_64:Win64 or i686:Win32.

Note that while you can have a Win32 version running on x86_64 hardware, you can’t have a Win64 version running on i686 hardware. Notably, these days a computer running a Win32 version is likely to be an old machine with a 32 bit i686 hardware architecture rather than one with an x86_64 HW architecture.

By default, we customarily use MinGW-W64 from an x86_64 host for an x86_64 target. The toolchain targetting Win32 may be used for compatibility reasons, as (i) Win32 systems are still operational and (ii) some C++ executables and DLLs are only available for 32 bit architectures, thus forcing an overall Win32 build.

MinGW-W64: what to install

The starting point for the download and installation of MinGW-W64 is the download page of the MinGW-W64 project website. This page presents a list of pre-built toolchains and packages that could be a bit confusing for the newcomer. However, things are quite simple in reality, keeping in mind that the objective of the MinGW-W64 project is that of prividing toolchains that target Windows (i.e., that generate executables for Windows) using the GCC  on a variety of hosts (i.e., of operating systems). So, if you want to use a GCC toolchain on a Windows machine  to generate Windows executables,  you must pick up a MinGW-W64 binary distribution where the host is Windows. In practice, this means focusing on the package called mingw-builds. At the time of writing, the relevant section of the download page looks like this:

mingw-downloads

Relevant portion of the MinGW-W64 download page highlighting the required distributions

The actual version number could be higher, indicating a newer release, but the download page outlook should by and large be the same. Clicking on the Mingw-builds link will lead you to a Sourceforge download page where you will be able to get the latest version of the MinGW-W64 installer for Windows.

You will also need to download and install MSYS to use some of its features as part of the GCC toolchain. The MinGW-W64 project provides an updated version of the MSYS package called Msys2. Clicking on the Msys2 link on the MinGW-W64 download page (see above) will lead you to a GitHub download page. Get the x86_64 version of Msys2 rather than the i686 one if – as expected – you have an x86_64 machine running Win64.

Installing the MinGW-W64 mingw-builds distribution

Run the MinGW-W64 installer downloaded from the Mingw-builds distribution. You will be presented with a startup page like the following:

MingwProposedDefaults

The default initial setup proposed by the installer – You will need to modify some installation parameters as explained below

Carefully note the psroposed Architecture, Threads model, and Exception mechanism.
DO NOT accept that configuration but note the following and change settings as suggested.

  • Version: always select the latest one from the drop-down menu (i.e., the one suggested by default) unless you have a very specific reason to do otherwise (e.g., the latest version has a known bug that affects library XYZ, so there is a need to revert back to a previous, safe version).
  • Architecture: Select x86_64 in place of i686.
  • Threads: POSIX threads are fine, leave that setting.
  • Exception mechanism: select sjlj (short jump – long jump).
  • Build revision: always select the latest one from the drop-down menu (i.e., the one suggested by default) unless you have a very specific reason to do otherwise.

Your settings page should then become something like this (possibly showing a newer version, but keeping all the rest as in the picture):

MingwOurDefaults

How the installation parameters should be for a 64 bit GCC installation

Click next. The installer will propose an installation folder as below.

InitialRootDirectory

The initial installation directory proposed by the installer – you are suggested to change it as discussed below.

The full destination folder path is something like
C:\Program Files\mingw-w64\x86_64-5.3.0-posix-sjlj-rt_v4-rev0
where the convention used to define the installation directory is self-explanatory, i.e.
C:\Program Files\mingw-w64\—-

Installing the chosen mingw release in a directory that explicitly specifies its version, architecture, threads model, expection mechanism, etc. (i.e., its specific characteristics) allows the installation of mingw versiosn with different characteristics and thus a rapid swap between them. This can be achieved by simply repointing the environment variables to a different root directory, as discussed later.

Rather than installing MinGW (plus MSYS2 and any other software to be compiled using the MinGW-based GCC toolchain like OpenCV) in the usual C:\Program Files directory, it would be better to create a C:\Program_Files directory and install everything there. The reason for this choice is simple: Avoid blank spaces in directory paths and file names if at all possible when dealing with MinGW-related stuff. The GCC toolchain did (and in some cases may still do) work incorrectly with paths and file names including blank spaces. besides, you will have a chance to group all GCC-related files in a specific directory that would be easier to standardis and manage.

So change the root MinGW directory into C:\Program_Files and your installation window path should look similar to this:

RootDirectory

How the installation directory should look like

The installer will then populate a directory structure under
C:\Program_Files\mingw-w64\x86_64-5.3.0-posix-sjlj-rt_v4-rev0
(or a similar directory with slightly different distribution and revision numbers).

Using C:\Program_Files\mingw-w64 as the root directory for installing MinGW-W64, and a specific subdirectory for each release (e.g., x86_64-5.3.0-posix-sjlj-rt_v4-rev0 in this case, it is possible to install several different releases. Afterwards, it would be easy to  change the Windows path to point to the specific version you may want to use. In fact, it will be enough to change the value of the MINGW_HOME environment variable (see below).

Adding MinGW-W64 executables to the Windows Path

Once the installation process is complete, the directory containing all the MinGW-W64 executables and DLLs should be added to the Windows Path environment variable, so that these are visible (and callable) from anywhere. Such directory is like the following:
C:\Program_Files\mingw-w64\x86_64-5.3.0-posix-sjlj-rt_v4-rev0\mingw64\bin

We can thus set an environment variable MINGW_HOME such that
MINGW_HOME = C:\Program_Files\mingw-w64\x86_64-5.3.0-posix-sjlj-rt_v4-rev0\mingw64

In this way, the Windows Path environment variable would be updated by adding the %MINGW_HOME%\bin directory, i.e.
Path = ;%MINGW_HOME%\bin;

This can be done in all Windows version. However, Windows 10 (if installed from scratch rather than updated from Win7 or Win8) provides a better GUI to manage environmnt variables. Using an environment variable to specify the path allows programmers to rapidly switch between different MinGW-W64 versions if needed. In fact, different versions (e.e., a 32 bit and a 64 bit one) could be deployed in parallel in the same root directory (e.g., C:\Program_Files\mingw-w64). To swap between different MinGW versions, it would be enough to modify the value of the MINGW_HOME environment variable, letting it point to a different MinGW_W64 version. In theory, this process could be even automated using batch commands.

Installing the MSYS2 distribution

As previously said, some components from the MSYS2 distribution are needed in place of those natively provided by MinGW-W64 for our GCC compilation and linkage toolchain.

Run the MSYS2 installer downloaded from the MSYS2 distribution, called msys2-x86_64-20150916.exe or something similar, where the meaning of the various installer name components is self-evident. Follow the installation instructions provided in the MSYS2 download page (whose salient points will be summarised in the following).

Once you begin the installation procedure, you will see a startup page like this:

Msys2Screen1

Initial MSYS2 installer screen for a 64 bit version

It is recommended to deploy MSYS2 alongside the very same root directory with no blank spaces where MinGW-W64 resides, i.e.,

msys2installationrootdirectory

Setting the MSYS2 root directory

At the end of the installation process, the following window will appear:

Msys2Screen3.jpg

The final Msys2 installer window: “Run msys2 64 bit now” and go

It is important to lunch Msys2 at this point (which is done automatically once you click the Finish button in the instraller window).  This will open a UNIX shell (remember that MSYS2 is a Minimalist Linux system running on top of Windows). However, just in case you did not launch the MSYS2 shell for some reason, you can always do it after the installation process is finished. You will need to perform this step later anyway to update and install msys components. Just go to the msys2/ directory and launch the msys2_shell.cmd batch file. In any case, the MSYS2 shell will look like this:

Msys2Shell1

The MSYS2 shell in action

Just to double-check that the window in front of you is the MSYS2 UNIX shell rather than the Windows command prompt, enter the UNIX command pwd (print working directory). If you are in the right shell, you will see thewhere you are in terms of the MSYS2 UNIX environment sitting on top of Windows. Typically, you will be in the directory /home/. The use of forward slashes (i.e., /) in place of back slashes is the very indication that you actually are in a UNIX environment rather than in the Windows environment.

Adding (needed) extra components to MSYS2

Once the MSYS2 installer has finished its task, you have only completed stage one of the MSYS2 environment installation process. More specifically, some of the most sought after MSYS2 components – such as make, which you will need as part of the GCC compilation and linkage toolchain under MinGW-W64 – must be separately install.

To do so, carefully follow the instruction provided in the MSYS2 download page, which are summarised as follows.

  1. In the MSYS2 UNIX shell, enter
    pacman -Sy pacman
    to update the package DB  and pacman, which is the UNIX package manager that MSYS2 uses to do this task
  2. Once pacman has finished its update task, close the MSYS2 shell. Then launch the shell again (see above if you don’t remember where to find it). Update the package DB and install the core system packages by entering this command:
    pacman -Syu
  3. Close the Msys2 shell and launch it again. Update the rest with
    pacman -Su

You should now have a working MSYS2 environment.

Adding make to the MSYS2 bundle

Open the MSYS2 shell again and type

pacman -S make

This will install the make utility.

Adding MSYS2 executables to the Windows Path

Once the installation process is complete, the directory containing all the MSYS2 executables and DLLs (i.e., those that run under Windows and that provide the MSYS UNIX functionalities once called from Windows) should be added to the Windows Path environment variable, so that these are visible (and callable) from anywhere. Such directory is like the following

C:\Program_Files\msys2\msys2-x86_64-20160921\usr\bin

We can thus set an environment variable MSYS_HOME such that
MSYS_HOME = C:\Program_Files\msys2\msys2-x86_64-<date>\usr
where, for instance, <date> = 20160921 (or a later date).
In this way, the Windows Path environment variable would be updated by adding the %MSYS_HOME%\bin directory, i.e., Path = ;%MSYS_HOME%\bin;
This can be done in all Windows versions.


Page last updated by ECOMPAT on 10/10/2016