Getting GNU compilers on Windows (MSYS2/MinGW-x64/boost/Windows 10)
Okay… first of all…
There are multiple packages and tutorials available to get one or the other working on Windows 10. This tutorial focuses on how to create a comfortable maintainable compiler environment that can be used for a long time. If you want to skip to the actual steps of installation, you can skip the next section and jump directly to Steps of installation.
1. Why is this needed?
To answer this, let us lay down all the things that are out there:
- Cygwin (https://cygwin.com/). Designed for people who want to use Unix tools on Windows. It provides a POSIX wrapper over Win32 API via its own DLL (cygwin1.dll). It aims to provide an environment where you can compile any program written for Unix without making code level changes (i.e., without porting Unix specific code such as pthreads or the networking stack to Windows). If you compile your program via the toolchain shipped with Cygwin, your program will be linked to cygwin1.dll and it won’t run without it.
Problem: Windows specific code will not compile. Unix specific code will require Cygwin to run.
- MinGW (http://www.mingw.org/). This is the original pure Windows compiler. Its toolchain doesn’t support POSIX but Win32 API. Your programs will run natively on Windows without requiring any external dependencies.
Problem: It doesn’t have 64-bit support.
- MSYS (http://www.mingw.org/wiki/MSYS). It is just a build environment for MinGW. That is, you can use MinGW to compile to native Win32 binaries but MinGW itself is compiled and managed via MSYS. MSYS started as a fork of Cygwin with some custom patches specific to MinGW. For a normal user, installing MinGW is the only thing to worry about.
Problem: It is shipped with MinGW and there is no point in having MSYS without MinGW. It is not designed to be used for the development of independent code.
- Mingw-w64 (https://mingw-w64.org/). Its history is documented at https://sourceforge.net/p/mingw-w64/wiki2/History/ . In short, Mingw-w64 has 64-bit support.
Problem: Default installation doesn’t ship with many libraries such as boost. Package management is confusing and may force you to recompile all the libraries you have installed yourself.
- MinGW Distro (https://nuwen.net/mingw.html). It is a zip-file distribution containing the full package of Mingw-w64/MSYS2 with extra libraries such as boost and SDL etc. All shipped libraries are listed on its website. Good for quick deployment if all you want is boost.
Problem: Same as Mingw-w64, i.e., package management. If you want a library that isn’t shipped with it, you are back to square one.
- MSYS2 (http://www.msys2.org/). MSYS2 is a newer fork of Cygwin (independent of MSYS) and is closely tied to Mingw-w64. Unlike MSYS, MSYS2 is very usable even without Mingw-w64. It has its own package manager based on pacman from Archlinux, surpassing even Cygwin in this department.
Problem: None. Its usability is very close to Cygwin but without cygwin1.dll dependency and with a real package management tool.
You can read further at https://stackoverflow.com/questions/25019057/how-are-msys-msys2-and-msysgit-related-to-each-other written by MSYS2 developer himself.
Apart from the above options, there are multiple tutorials you can find on the internet to install libraries for all the different combinations. If you want to install boost itself, a Google search will confuse you more than pointing you in the right direction. To this end, let me blurt out the one-true answer: MSYS2.[NOTE]
NOTE: One caveat for purists: While MinGW/MSYS strictly follows public Win32 API that can be sourced to MSDN. Mingw-w64 has a relaxed approach and it incorporates reverse-engineered code too.
2. Steps of installation
The list of steps is not huge. It may look daunting but it is one-time, finishes fast, repetitive and is exhaustive. YELLOW steps are optional that may be required if you encounter any problem.
- Download and install MSYS2. It is a simple download-run-next-next-finish routine. For this tutorial, we will assume you have installed it at C:\msys64 directory. This will install some shortcuts for you: “MSYS2 MSYS” and “MSYS2 MinGW” (32- and 64-bit, as per your choice). Both links open the same terminal (which is also shipped with all the above mentioned different solutions), with some environment variables set differently to support your choice of the compiler. (PS: God bless the people who wrote mintty, for cmd.exe doesn’t cut it for those who are baked in the way of Unix.)
- Run either of the newly installed shortcuts to start MSYS2.
- Run pacman to update the system. Remember, this is not a Linux environment and pacman doesn’t work exactly the way you might remember from Archlinux.
Shell1$ pacman -Syuu
- Open pacman.conf file (normally situated at C:\msys64\etc\pacman.conf) in Notepad. Under [options] section, uncomment/add the following line:
1SigLevel = Never
- Run MSYS2 again.
- Run pacman again:
Shell1$ pacman -Syuu
After completion of this step, pacman will stop showing any update.
- The close-rerun process is, unfortunately, a painful re-occurrence for package management because for some reason pacman reloads shell after installation of some packages but since windows doesn’t support ‘fork’, it hangs with constant 100% CPU usage. Luckily it is only an annoyance and doesn’t affect the actual installation.
- Now revert the changes you made in pacman.conf back to the default, i.e., SigLevel should have this value:
1SigLevel = Required DatabaseOptional
- Now you can install your favorite toolchain:
Shell1$ pacman -S mingw-w64-x86_64-toolchain
NOTE: Unfortunately, this doesn’t install git and make (it will install mingw32-make.exe which is a link to make inside mingw directory). We will come to that in the next steps.
- If you want to search for a package, use pacman -Ss substring to search. To remove a package, use pacman -R packagename. Pacman tutorials are aplenty, so we won’t cover it here.
- Now we will install make:
Shell1$ pacman -S make
- You can install other required libraries as per your wish using similar mechanism. For example, you can install Boost C++ libraries for 32-bit as follows:
Shell1$ pacman -S mingw32/mingw-w64-i686-boost
Shell1$ pacman -S mingw64/mingw-w64-x86_64-boost
MSYS2 is very close to a well functioning Unix-like environment with proper Win32 support (without POSIX). You can install vim or emacs to make your stay longer.
3. What next?
You can continue installing any other program via source code using your newly installed toolchain. All you need to do is to start MSYS2 with the appropriate shortcut and specify prefix to be /mingw64 (or mingw32 if installing for 32-bit). Normally, configure will automatically pick that up itself.