This is the second part of a three part series on using C++0×11 on OSX via Eclipse. If you need a little bit more nudging, you might want to look at Part0 of this series, where I make the case that the Langauge is moving in this direction and as such does not make sense for us to use older outdated tools.

In this post I’ll discuss the process of getting and installing the GCC4.7 compiler. First we need to arm our computer with the right tools.

We’ll be configuring and installing the following tools:

  • Macports: A tool for configuring / building / installing  tools, binaries, etc from terminal on OSX similar to ports or apt-get
  • Xcode 4.2+ : Required for above
  • Xcode 4.2+ Command Line Support: Required to build macports
  • Eclipse CDT: Our editor

Installing xcode on your system:

The easiest way to install the command line tools needed by Macports on your system, is to install XCode.
Browse to developer.apple.com and download the latest version of xcode for your system.
It’s going to take a while…

Xcode Install options

Ensure that those of the following options, which are available in the installer for your version of Xcode are selected:

  • UNIX Development
  • System Tools
  • X11 SDK
  • Command Line Support

Installing Macports:

Configuring MacPorts

Lets make sure everything is installed and configured in Macports, open terminal and type

sudo port selfupdate
sudo port upgrade outdated

Macports update error – Ooops?

You might run into some problems here, I had one upgrading Perl5.8, but this worked for me:

sudo port -f uninstall perl5.8 @5.8.9_3+universal
sudo port clean perl5.8
sudo port install perl5.8

Install Eclipse CDT

Eclipse is the editor we’ll be using for our examples, but in theory any C++ editor will work just as well, or none if you feel comfortable creating / editing your own MAKE files. If you want to follow allow verbatim, I recommend installing EclipseCDT standalone, not installing as a plugin and appending to your existing Eclipse install.

That’s my preference, I actually have standalone installs for Python / Java / C++ / Haxe versions of eclipse, with their own workspace. There is a lot of room for subjectivity here, so I’ll leave that argument alone for another blog entry.

As a side note, I created a different eclipse icon here:

http://onedayitwillmake.com/blog/2010/11/better-eclipse-icon/

Now to finally install the latest GCC compiler toolchain (GCC4.7 at the time of writing)

In terminal type

sudo port install -d gcc47

The “-d” flag provided us with a lot of verbose output. I think it’s always nice to see whats going on behind the scenes as you might spot something interesting you didn’t know to look for.

This process will take a long time, mine took like an hour. I think there’s something cool about using GCC4.2 to build GCC4.2 library. So if you want to see what it’s doing

Open a new terminal tab and type the following to see the location of the log file:

port logfile gcc47

Open it in an editor that automatically watches the file for changes, and watch it grow and grow ( I used BBEdit for this )

Activating GCC4.7

For this we’ll be using the built in utility called “port select”. It allows us to easily switch between multiple versions of a library. However first lets activate GCC4.7

sudo port install -d gcc47

Switching between GCC versions and using GCC4.7

Finally now that thats complete, lets check out what we have here:

port select --list gcc

Mine looked like this, however yours will look a little bit different:

recrease-2:~ onedayitwillmake$ port select --list gcc
Available versions for gcc:
	gcc42
	llvm-gcc42 (active)
	mp-gcc46
	mp-gcc47
	none

Lets select and activate GCC4.7, by typing the the following in terminal

sudo port select gcc mp-gcc47

Uh oh!

It looks like there’s an error – it’s failing because a certain directory doesn’t exist.
I think that’s a bug in the port installation but it’s no big deal since we’re not using java, to fix it type:

sudo touch /opt/local/bin/gcj-mp-4.7

Follow this again for the Fortran version, since we’re not using that either:

sudo touch /opt/local/bin/gfortran-mp-4.7

Lets try selecting again “sudo port select gcc mp-gcc47″, mine looked like this

Selecting 'mp-gcc47' for 'gcc' succeeded. 'mp-gcc47' is now active

Awesome… type the following to see the fruits of your success:

gcc --version & g++ --version

Wrap up

That’s it – we’re done! Now you can go to town with all the C++11 features that are all the rage. In the next post I’ll go over creating a C++11 project in EclipseCDT and compiling it.