Running GCC or Clang on Windows has never been easier thanks to the MSYS2 project. Setting up the Eclipse CDT environment to use these compilers is also very easy. This post will illustrate exactly how easy it is.
Head over to the MSYS2 website and follow steps 1 - 7. At the end of it you should have a working MSYS2 shell.
Next you need to install GCC + other utilities (MAKE, GDB, BINUTILS, … etc) for windows. The Mingw-64 project provides versions of GCC that are capable of running natively on Windows and building Windows 64/32 bit target binaries, and MSYS2 has great support for mingw-64. Download the mingw64 packages using the MSYS2 package manager
There are a couple of ways to go about this - you can either download the individual packages (gcc, gdb …) or you can download all the relevant packages in one shot using the
mingw-w64-i686-toolchain depending on what you want to use. You can search the available packages using the command
pacman -Ss string_to_search.
1 2 3 4
Once this is done you will have a new shell in the /msys64 folder called
mingw32.exe (if you installed the i686 toolchain). Runnnig this shell and typing
gcc -v will give you some information on what you have installed. This shell can also be used to directly compile any makefile projects you have. Next we talk about integration with the Eclipse IDE.
GCC with Eclipse
Assuming you have already installed the Eclipse IDE for C/C++ developers. Eclipse has some built-in support for Mingw projects which depends on the presence of some environment variables like
MSYS_HOME etc. However the method I outline here is not related to this and is (imho) simpler and more robust.
In eclipse go to
File | New | C Project | and select either Cross GCC or MinGW GCC project. Then in the project properties go to
C/C++ Build | Environment
Select the option to “Replace native environment with specified one”. This is a really useful option that allows you to customize the environment for this build without having any dependencies on the system PATH.
PATHvariable to have access to the relevant
makebinaries. MSYS2 installs
/usr/bindirectory and the gcc executables are in either
TMPvariable to point to the system
TMPenvironment variable by using the syntax
Now you have configured the build environment. All that’s remaining is to setup the compiler and linker options. Go to
C/C++ Build | Settings | Tool Settings | Cross GCC Compiler and set the command to
gcc. Similarly set the
Cross GCC Linker command to
Cross GCC Assembler command should be
Thats it! Select apply and build the project.
If you wan to use GDB, you need to create an Eclipse debug configuration.
Clang with Eclipse
MSYS2 provides packages for Clang also (mingw-w64-x86_64-clang and mingw-w64-i686-clang). There seem to be some dependencies with mingw, so you are better off first installing mingw first followed by clang.
Thereafter the settings are exactly the same as above except that this time instead of
gcc you type
clang for the compiler and linker commands. The assembler is
lldb is not yet part of the MSYS2 package, so you can’t run the debugger yet; but you might be able to run gdb.