Writings on what I've learned as I study my two favorite programming languages C++ and Python.

Friday, May 2, 2008

Using a C++ compiler with Eclipse on Windows

I installed the Eclipse CDT in my Windows PCs (both at home and in the office) and I realized that I could simply write C++ code but not build nor run them. The development tools that are so readily and easily available (and I sometimes take for granted) in Linux are simply not that out-of-the-box on Windows. I've installed Microsoft's Visual C++ 2008 at home but I have a difficulty making it work with Eclipse (because obviously they are competing products). So I fired off a google search for a free C++ compiler for Windows and the recommendation I got from what I've read: MinGW (Minimalist GNU for Windows).

So MinGW it is. (I've tried Cygwin, too, but you'll need to run the binaries using Cygwin as well, unlike MinGW which produces native Windows/DOS executables.) Here's what I did to install MinGW and make it work with Eclipse.

First, get the following files from MinGW's Sourceforge download site:
binutils-2.17.50-20060824-1.tar.gz
gcc-core-3.4.5-20060117-3.tar.gz
gcc-g++-3.4.5-20060117-3.tar.gz
gdb-5.2.1-1.exe
mingw32-make-3.81-20080326-2.tar.gz
mingw-runtime-3.14.tar.gz
mingw-utils-0.3.tar.gz
w32api-3.11.tar.gz
You can get newer versions, as long as they are "Current Releases" or you might be in for a nasty surprise later.

Create a folder C:\MinGW. Extract the *.tar.gz files (except gdb) into this folder. I recommend that you use WinRAR for the extraction as WinZip was known to have some issues. Simply run the gdb exe file (double click on it) and to install it as well at C:\MinGW.

After unpacking those files, we now have to make the compiler and other MinGW utilities visible to the rest of the system. Go to Start-> Settings-> Control Panel-> System. Click on Advanced tab, then click on the Environment Variables button. Create a "Path" user variable if don't have it yet and assign (or add) to it C:\MinGW\bin.

That's it, we've just installed MinGW! As a simple check, try opening a command prompt: Start-> Run... and type in "cmd". Or you can also just do Start-> Programs-> Accessories-> Command Prompt. In the command prompt, try typing "g++ -v", then Enter. You should see something like this:

H:\>g++ -v
Reading specs from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs
Configured with: ../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld
--with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads
--disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry
--disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt
--without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter
--enable-hash-synchronization --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.5 (mingw-vista special r3)

H:\>


Now using MinGW with Eclipse is just straightforward. Run Eclipse, and when you create a new C++ project, select the MinGW GCC as your toolchain. With this, you can create native Windows executables and libraries and debug them as well.

-- Allister (http://cxxpython.blogspot.com)

2 comments:

Doug Schaefer said...

You should try Wascana. It bundles MinGW with the CDT. It's a project out on SourceForge.

Allister said...

Thanks for the heads up!