'Pure' GNU C++ compilers for Lion

I need to compile simple *.cpp files with a GNU compiler of at least version 4.3.1. As I understand, Apple's Xcode does not install standard GNU compilers but a modified version suited to Apple's needs:

g++ -v

gives me

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

also referred to as

i686-apple-darwin11-llvm-g++-4.2

What is a simple way to install and safe method to use a newer C++ compiler in Mac OS?


MacPorts has several pre-configured gcc packages (from 4.2 to 4.6). Just install MacPorts and run

sudo port install gcc46

I prefer using High Performance Computing's versions of the GNU compilers, as I need a Fortran compiler, which Apple does not offer.

this solution avoids installing anything BUT a GCC compiler, whereas fink and macports will add quite a lot of stuff to your system. The HPC compiler requires having Apple's developer's tools installed already, but since you mention having gcc I guess you do.


There might be a simpler way using Package Managers like Fink, Homebrew, MacPorts but according to http://pdb.finkproject.org/pdb/browse.php?sec=devel Fink doesn't have a g++ package. I don't use Homebrew or MacPorts so you'll have to do your own research.

Another (relatively more work) option is to download g++ version (4.3.1) you want, config it differently (i.e point it away from the default directories for e.g. /usr/bin but instead have the executable symlink to /usr/local) before building and installing it.


Thanks to Kenneth, you can install this pkg made for Lion:

https://github.com/kennethreitz/osx-gcc-installer/downloads

Note, this may be the wrong gcc for your needs as it is just an 'Xcode independent' installation of the Apple provided compilers, so the GCC is the same as that version of Xcode from whence it came. In addition, the project is now been mothballed in favour of Apple's own CLI tools package.

http://www.kennethreitz.com/xcode-gcc-and-homebrew.html