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.
For more details about installing MSYS2 check out Install gcc compiler on Windows with MSYS2 for C/C++.
Trying to get an open source toolchain configured in Windows for C++ can sometimes be a headache. In Windows, you can simply install Visual Studio and use the Microsoft C++ compiler, but you may want to use the free GNU compiler (g++) instead. This will outline the process for getting a simple C++ toolchain ready in Windows using Eclipse as an IDE. This guide focuses on the 64 bit version only.
It is important to note that if you install all the MSYS2 development tools first and add the directories to your PATH before running Eclipse and creating a workspace, then Eclipse will be able to find the compiler and tools without having to explicitly set and include directories and will generally work better. I highly recommend setting up the MSYS2 tools and updating your PATH before loading Eclipse.
The toolchain being used is:
- Eclipse C++ (currently Oxygen)
- MSYS2, which provides the MinGW gcc/g++ compiler.
MSYS2
For a compiler we want to use g++, part of the GNU compiler collection (gcc). To get this on Windows we want a minimal system (MSYS) with minimalist GNU for Windows (MinGW) setup. You could use the older MinGW+MSYS tools, but I recommend using MSYS2, which is all I will covere here. MSYS2 comes with the pacman package manager, the same one Arch Linux uses. Simply download the installer and then use the "MSYS2 MSYS" icon that it creates in the start menu, or run it manually from C:\msys64\msys2.exe.
After getting in to the MSYS2 bash shell, update everything with:
pacman -Syu
Then install the base development tools like make and autoconf with the base-devel package and
# Install tools like make, autoconf, and more
pacman -S base-devel
# Install the compiler(gcc), debugger (gdb), and more
pacman -S mingw-w64-x86_64-toolchain
Optional: Add MSYS2 executables to your Windows PATH
If you want to access everything from your Windows Command Prompt, including vim, pacman, g++, make, and everything else, then add these to your PATH:
# .dll files are also in the bin directories
C:\msys64\mingw64\bin
C:\msys64\usr\bin
Eclipse C++
Since you already have the compiler toolchain installed and available, you can compile C/C++ project and get coding, but you will likely want to set up an IDE to make things easier. There are many options including NetBeans, Qt Creator, CodeBocks, CLion, Visual Studio Code, Sublime Text, Atom, vim, emacs, etc. Since we are using the free GNU toolchain that is available on several platforms, I recommend using an IDE that is also cross-platform, so you can use the same IDE+compiler across Windows, Mac, and Linux. For this tutorial, I chose Eclipse. It is a very mature IDE, has a good debugger, is heavily customizable and supports many other languages if you choose to commit to it and use it for other languages.
When choosing a project, choose MinGW as the compiler.
Configure environment variables
First set up your environment variables so Eclipse knows where to look for things. It might try to auto detect some of these, but you want to at a minimum make sure your PATH variable includes the directories with make and g++.
- C:\msys64\mingw64\bin
- C:\msys64\usr\bin
Project->Properties->C/C++ Build->Environment:
# Set these environment variables in the C/C++ Build options
MINGW_HOME=C:\msys64\mingw64
MSYS_HOME=C:\msys64
PATH=${MSYS_HOME}\usr\bin;${MINGW_HOME}\bin
Note the path var may already be set if you manually updated your path earlier to add the bin directoriess.
At this point you should be able to compile your program, but the code editor will tell you it can't resolve the standard library symbols like std::cout.
Set up include paths
To fix the unresolved references, Eclipse needs to know where to look for the header file. Have it include the header file for the standardb C++ library in MinGW, by adding the following include path:
Project->Properties->C/C++ Build->Settings->g++ compiler->Includes
# Add this include path to resolve standard C++ library references
C:\msys64\mingw64\include\c++\7.3.0
# Other includes in
C:\msys64\mingw64\include
# And others in
C:\msys64\usr\include
Optional: Specify C++ Standards Level
To specify the language level, go to Project->Properties->C/C++ Build->Settings->GCC C++ Compiler->Dialect and choose the appropriate language level, like -std=c++0x
Optional: 32-bit toolchain
To install the 32-bit toolchain, run:
# Install 32-bit toolchain
pacman -S mingw-w64-i686-toolchain
And add the 32-bit toolchain path to your PATH environment variable.
# Add this to PATH instead of 64-bit version to get 32-bit gcc
C:\msys64\mingw32\bin
Optional: CMake
Make is already installed with the base-devel package, but if you want to use CMake you need to install it separately from base-devel and toolchain.
# Install 64-bit cmake
pacman -S mingw-w64-x86_64-cmake
# Install 32-bit cmake
pacman -S mingw-w64-i686-cmake
Enjoy
At this point, you should have a working toolchain to compile C++ on Windows using Eclipse!