A Circular Reference

Adventures of a vagabond electron.

Using GCC and Clang With Eclipse on Windows

| Comments

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.

Installing MSYS2

  • 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 pacman.

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-x86_64-toolchain or 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
# This should install the toolchain into the /msys64/mingw64 folder
pacman -S mingw-w64-x86_64-toolchain
# Install make if it's not already installed. Eclipse will use make to run it's autogenerated makefiles. It goes into the /usr/bin directory
pacman -S make

Once this is done you will have a new shell in the /msys64 folder called mingw64.exe or 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 MINGW_HOME, 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.

  • Set the PATH variable to have access to the relevant gcc and make binaries. MSYS2 installs make in the /usr/bin directory and the gcc executables are in either mingw64/bin or mingw32/bin.

  • Define a TMP variable to point to the system TMP environment variable by using the syntax ${env_var:TMP}

image

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 gcc. The Cross GCC Assembler command should be as.

Thats it! Select apply and build the project.

image

image

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 llvm-as. 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.

Comments