Install gcc compiler on Windows with MSYS2 for C/C++

Advertisement

Advertisement

DISCLAIMER: Don't hold me accountable for any bad advice or incorrect information provided here. Use this information at your own risk. I was barely able to hack it together myself and there are still a few details I am not 100% clear on.

Intro

There are a variety of toolchains you can install on Windows for compiling C and C++ applications. Microsoft provides Visual Studio Community for free, and is a great IDE and toolchain. If you are developing exclusively for Windows, that is the tool I would recommend. For those who need or prefer the cross-platform GNU toolchain, you can install it using MSYS2. We will cover how to install gcc using MSYS2.

Another option for using gcc in Windows is to download Code::Blocks. They provide binary releases that come with the Mingw toolchain. For example, http://www.codeblocks.org/downloads/.

Qt Creator is another free IDE that optionally comes with the mingw toolchain. Check out https://www.qt.io/download and https://www.qt.io/qt-features-libraries-apis-tools-and-ide/ for more information.

If you do not need to target Windows specifically, but you are just looking to learn and play with the GNU toolchain and you have a windows machine, I recommend installing Ubuntu or Suse on Windows through the Microsoft Store. This will provide a Linux environment in Windows for you to work with. Alternatively you could install a distribution like Ubuntu inside a VirtualBox or VMWare Player virtual machine.

Installing and running MSYS2

Download MSYS2 from http://www.msys2.org/. Download the .exe file and follow the installation instructions on the site. After installing, navigate to the directory where it was installed, and run msys2.exe. For this tutorial, we will assume the default location of C:\msys64. After opening it you should find yourself in a bash shell.

Updating MSYS2

MSYS2 uses the pacman package manager that the Arch Linux distribution uses. After your initial install it is a good idea to update all the packages. Update everything using:

pacman -Syu

Installing gcc using MSYS2

In the MSYS2 bash shell, use pacman again to install the build toolchain and compilers. Run the command below to install the mingw-w64-x86_64-toolchain package group.

# Install make, autoconf, etc to C:\msys64\usr\bin
pacman -S base-devel gcc vim cmake

Note that vim and cmake are optional, but handy to have. Here is the list of packages that are installed with the above. After installing those packages, you will have gcc and the other tools available to be used while in the MSYS2 shell. If you want to use them in the command prompt or otherwise outside of the MSYS2 shell, they are available in the C:\msys64\usr\bin directory.

# pacman -S base-devel
   1) asciidoc  2) autoconf  3) autoconf2.13  4) autogen  5) automake-wrapper
   6) automake1.10  7) automake1.11  8) automake1.12  9) automake1.13
   10) automake1.14  11) automake1.15  12) automake1.16  13) automake1.6
   14) automake1.7  15) automake1.8  16) automake1.9  17) bison  18) diffstat
   19) diffutils  20) dos2unix  21) file  22) flex  23) gawk  24) gdb
   25) gettext  26) gettext-devel  27) gperf  28) grep  29) groff  30) help2man
   31) intltool  32) lemon  33) libtool  34) libunrar  35) libunrar-devel
   36) m4  37) make  38) man-db  39) pacman  40) pactoys-git  41) patch
   42) patchutils  43) perl  44) pkg-config  45) pkgfile  46) quilt  47) rcs
   48) scons  49) sed  50) swig  51) texinfo  52) texinfo-tex  53) ttyrec

# pacman -S gcc
   binutils-2.30-1  isl-0.19-1  mpc-1.1.0-1
   msys2-runtime-devel-2.11.1-2
   msys2-w32api-headers-6.0.0.5223.7f9d8753-1
   msys2-w32api-runtime-6.0.0.5223.7f9d8753-1
   windows-default-manifest-6.4-1  gcc-7.3.0-3

After installing the above the .exe files will be available in C:\msys64\usr\bin. Some of the executables that will be available are: awk, base64, curl, dd, diff, dos2unix, file, gcc, gdb, gpg, grep, gzip, head, hexdump, hostname, ld, ldd, ls, make, man, md5sum, merge, mkdir, more, mv, objdump, openssl, perl, python2, python2.7, readelf, rm, rmdir, sed, sha1sum, sha256sum, sha512sum, sort, split, stat, tail, tee, telnet, uniq, uname, vim, wget, which, whoami, yes.

There are many other executables in the C:\msys64\usr\bin directory. Keep this in mind when making the decision to add the bin directory to your Windows PATH environment variable.

Installing other packages with pacman

If you want to compile an SSL program that links to libssl and libcrypto with -lssl -lcrypto you will need to install openssl-devel as shown below. There are many other devel packages. For example, libbz2-devel, libelf-devel, libunrar-devel, and libyaml-devel. The environment is rather limited, but it can be useful for learning.

# Install all *-devel packages
pacman -S development

Packages included in "development" group:

:: There are 78 members in group development:
:: Repository msys
   1) apr-devel  2) apr-util-devel  3) aspell-devel  4) bash-devel  5) cloog-devel
   6) gamin-devel  7) gettext-devel  8) glib2-devel  9) gmp-devel  10) heimdal-devel
   11) icu-devel  12) isl-devel  13) jansson-devel  14) jsoncpp-devel  15) libarchive-devel
   16) libargp-devel  17) libassuan-devel  18) libatomic_ops-devel  19) libbobcat-devel
   20) libbz2-devel  21) libcares-devel  22) libcrypt-devel  23) libcurl-devel  24) libdb-devel
   25) libedit-devel  26) libelf-devel  27) libevent-devel  28) libexpat-devel  29) libffi-devel
   30) libgc-devel  31) libgcrypt-devel  32) libgdbm-devel  33) libgnutls-devel
   34) libgpg-error-devel  35) libgpgme-devel  36) libgpgme-python2  37) libgpgme-python3
   38) libguile-devel  39) libiconv-devel  40) libidn-devel  41) libidn2-devel  42) libksba-devel
   43) liblz4-devel  44) liblzma-devel  45) liblzo2-devel  46) libmetalink-devel
   47) libneon-devel  48) libnettle-devel  49) libnghttp2-devel  50) libnpth-devel
   51) libp11-kit-devel  52) libpipeline-devel  53) libpsl-devel  54) libreadline-devel
   55) librhash-devel  56) libsasl-devel  57) libserf-devel  58) libsqlite-devel
   59) libssh2-devel  60) libtasn1-devel  61) libtirpc-devel  62) libtre-devel-git
   63) libunistring-devel  64) libuv-devel  65) libxml2-devel  66) libxslt-devel
   67) libyaml-devel  68) mpc-devel  69) mpfr-devel  70) ncurses-devel  71) openssl-devel
   72) pcre-devel  73) pcre2-devel  74) protobuf-devel  75) ucl-devel  76) util-macros
   77) xproto  78) zlib-devel

You also have a ton of other packages available via the pacman package manager. View the full list of options at https://github.com/msys2/msys2/wiki/Packages, or use the command below to search

# Search pacman for a package (cmake)
pacman -Ss cmake

# msys/cmake 3.12.2-1
#   A cross-platform open-source make system

# Install with
pacman -S cmake
pacman -S clang
pacman -S vim

# Search for gcc (notice the msys/, mingw32/, and mingw64/ prefixes)
pacman -Ss gcc

Packages prefixed with msys/ will be installed to the C:\msys64\usr\ environment. Some packages are prefixed with mingw64. Those are installed to C:\msys64\mingw64\ environment.

Optional: Add executables to your Windows PATH

If you want to access everything from your Windows Command Prompt, then add the bin directory to your Windows PATH environment variable. Keep in mind this adds a lot of executables to your path which might conflict with other applications. The usr\bin\ directory contains the whole slew of executables listed above. There is a lot of unnecessary stuff in that directory. mingw64\bin\ directory has a sma

C:\msys64\mingw64\bin
C:\msys64\usr\bin

Header files and libraries

Libraries and include files can be found in two places.

# The dynamic lib runtime .dll files will be in bin dirs
# Add the bin directory to PATH environment variable so it can find the .dll files
C:\msys64\mingw64\bin
C:\msys64\usr\bin

# Static libraries
C:\msys64\usr\lib
C:\msys64\mingw64\lib

# Header files
C:\msys64\usr\include
C:\msys64\mingw64\include

A note about msys vs mingw64 repos

Some things are only available in the msys/ repo like vim and git, and will only be available in \usr\bin\. Some things like gcc are available in msys\, mingw32\, and mingw64\ repos and can potentially end up being installed in both \usr\bin\ and \mingw64\bin\. This is where you have to be careful about how you set up your PATH environment variable. If you add \usr\bin\ in order to make vim or git available, you will also add everything in that directory, which may conflict with something if you also add the \mingw64\bin\ directory to your path. If you only want the toolchain without as much extra stuff, use the mingw64 packages. Then you can add only the \mingw64\bin directory to your PATH if desired. The mingw64 repository generally has more libraries available for install that are unavailable in the general msys repo (e.g. SDL, exif, freeglut). The msys packages are intended to be used inside the msys shell, and the mingw packages are intended to be used outside of msys2.

# Install gcc in C:\msys64\mingw64\bin\ directory
# To go with C:\msys64\mingw64\include and C:\msys64\mingw64\lib
pacman -S mingw-w64-x86_64-toolchain

Packages (17)
mingw-w64-x86_64-binutils-2.30-5
mingw-w64-x86_64-crt-git-7.0.0.5245.edf66197-1
mingw-w64-x86_64-gcc-8.2.0-3
mingw-w64-x86_64-gcc-ada-8.2.0-3
mingw-w64-x86_64-gcc-fortran-8.2.0-3
mingw-w64-x86_64-gcc-libgfortran-8.2.0-3
mingw-w64-x86_64-gcc-libs-8.2.0-3
mingw-w64-x86_64-gcc-objc-8.2.0-3
mingw-w64-x86_64-gdb-8.2-1
mingw-w64-x86_64-headers-git-7.0.0.5245.edf66197-1
mingw-w64-x86_64-libmangle-git-7.0.0.5230.69c8fad6-1
mingw-w64-x86_64-libwinpthread-git-7.0.0.5231.7da6518b-1
mingw-w64-x86_64-make-4.2.1-2
mingw-w64-x86_64-pkg-config-0.29.2-1
mingw-w64-x86_64-tools-git-7.0.0.5242.1b29d1bc-1
mingw-w64-x86_64-winpthreads-git-7.0.0.5231.7da6518b-1
mingw-w64-x86_64-winstorecompat-git-7.0.0.5230.69c8fad6-1

Configuring Eclipse

For some tips on setting up Eclipse to work with this toolchain, check out tips for setting up Eclipse with MSYS2.

Advertisement

Advertisement