Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
1  
It is not a modified gcc but llvm (llvm.org). –  Matteo Nov 20 '11 at 17:05

4 Answers 4

up vote 9 down vote accepted

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

sudo port install gcc46
share|improve this answer
    
Yes!Thank you @Matteo. Easy and simple. –  gentmatt Nov 20 '11 at 19:21
    
@Matt: but not fast, it will take a while to install.. –  Matteo Nov 20 '11 at 19:25
    
Indeed... almost one hour! –  gentmatt Nov 20 '11 at 21:06
2  
For anybody that needs help with the configuration: 1) Install pkg (see like in answer) 2) port select --list gcc3) sudo port select --set gcc mp-gcc46 Now you can call the new compiler via g++ as usual. Notice, that you have to open a new terminal window to verify that this works (e.g. with g++ -v). If you remain in the same terminal window after changing the like, you get an error. –  gentmatt Nov 20 '11 at 21:26
    
2) lists the installed compilers 3) chooses the default compiler –  gentmatt Nov 20 '11 at 21:33

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.