how do i build gcc on a mac?

MacOS

Question or issue on macOS:

I’d like to build the latest version of gcc on a mac. I have the latest xcode but I’m looking for some of the c++0x features that are in more recent versions (the lambda functions, etc).

Are there any good step-by-step tutorials on doing this?

How to solve this problem?

Solution no. 1:

One option is to install MacPorts and install the gcc46 package:

sudo port install gcc46

Another option is to download the source code and build it as follows:

tar xzvf gcc-4.6.0.tar.gz
cd gcc-4.6.0
./configure
make

Note that GCC 4.6.0 requires as prerequisites GMP 4.2+, MPFR 2.3.1+, and MPC 0.8.0+. If ./configure fails, it’s probably because you’re missing one of these (though it should give you a helpful error message in any case).

Building will take a while—likely several hours, depending on your hardware.

Solution no. 2:

You should look at the Homebrew project.

Homebrew allows you to do things like this:

brew install gcc

Mac homebrew installation instructions are available here.

Solution no. 3:

Add GCC support to a fresh Xcode 4.2 installation using this homebrew formula:

brew install https://raw.github.com/Homebrew/homebrew-dupes/master/apple-gcc42.rb

Upgrading from Xcode 4.1 doesn’t drop existing GCC support, so this formula is only useful if you’re working with a fresh 4.2+ installation.

Solution no. 4:

I would suggest building it yourself (Adam details how to do so). This will give you fine control on where to install and all the options you want to select. My experience from having multiple versions of gcc is that, if care is not taken apple’s version of gcc can be damaged.

To speed up gcc installation you might want to look at –enable-languages option. If there are languages you don’t need installed with the new gcc then you may not want to select them.

Hope this helps!