Update GCC on OSX

127,313

Solution 1

If you install macports you can install gcc select, and then choose your gcc version.

/opt/local/bin/port install gcc_select

To see your versions use

port select --list gcc

To select a version use

sudo port select --set gcc gcc40

Solution 2

I know it is an old request. But it might still be useful to some. With current versions of MacPorts, you can choose the default gcc version using the port command. To list the available versions of gcc, use:

$ sudo port select --list gcc

Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc46
none (active)

To set gcc to the MacPorts version:

$ sudo port select --set gcc mp-gcc46

Solution 3

I'm just dropping in to say that using a soft link to accomplish this is a terrible, no-good, horrible idea.

One of the key things about writing software is reproduceability - you want to be able to get the same results every time. These systems are so complex that you want to reduce all invisible sources of error.

Having a soft link is an invisible source of error. It's the sort of thing you'll forget in a month, then move to a different machine, and wonder why you are getting different results - or, you'll try to upgrade your system, and you'll get weird errors because it's not expecting a softlink there.

Moreover, this isn't guaranteed to work - in particular, it's not clear that you will get the correct system include files, which have certainly changed between iterations of gcc.

gcc_select is a systematic way of doing the same thing which will work predictably, or in the very worst case you can file a bug report and get an eventual fix or fix it yourself.

Unfortunately :-( gcc_select does not affect which compiler XCode uses so it's not the way to go if you need to work in XCode (which I do). I still don't know what that way might be.

Solution 4

The following recipe using Homebrew worked for me to update to gcc/g++ 4.7:

$ brew tap SynthiNet/synthinet
$ brew install gcc47

Found it on a post here.

Solution 5

You can have multiple versions of GCC on your box, to select the one you want to use call it with full path, e.g. instead of g++ use full path /usr/bin/g++ on command line (depends where your gcc lives).

For compiling projects it depends what system do you use, I'm not sure about Xcode (I'm happy with default atm) but when you use Makefiles you can set GXX=/usr/bin/g++ and so on.

EDIT

There's now a xcrun script that can be queried to select appropriate version of build tools on mac. Apart from man xcrun I've googled this explanation about xcode and command line tools which pretty much summarizes how to use it.

Share:
127,313
Karan Bhamra
Author by

Karan Bhamra

A student learning programming.

Updated on October 15, 2020

Comments

  • Karan Bhamra
    Karan Bhamra over 3 years

    So I am a new programmer and I just installed XCode on my Macbook to get the GCC. I think Xcode is the only way for getting GCC on OSX. Now when I run my Hello World application, in C++, g++ comes up saying it is version 4.0.1 but when I look for commands starting with g I also see g++-4.2. Is there any way of making 4.2 default rather than 4.0.1, and also is there a way to updating gcc to the latest version 4.4.0?

    EDIT: Ok, so I installed macports and installed gcc4.4 and it shows up on terminal as gcc-mp-4.4 and how do I make it default with gcc_select, like what are the commands and stuff. Thanks.

  • user2584401
    user2584401 about 15 years
    I don't have that port installed, but I still have gcc_select. This is an older Xcode with OS X 10.4 though.
  • user2584401
    user2584401 about 15 years
    If you need more info on MacPorts, they are here (macports.org) and they have ports for gcc up to 4.5.
  • philgo20
    philgo20 over 13 years
    I've done that, but when I start the python interpreter, i still see gcc 4.0.1
  • philgo20
    philgo20 over 13 years
    I am guessing I would have to reinstall python2.6 once the gcc_select command has been passed.... Is that it ?
  • tmandry
    tmandry almost 13 years
    Now it seems that the command is port select --list gcc to list and port select --set gcc mp-gcc45 to set (replace mp-gcc45 with your version.) You still have to have gcc_select installed.
  • John Zwinck
    John Zwinck about 12 years
    ...a year and a half later, GCC 4.2 is still the standard in XCode on the latest Mac OS. Sad but true.
  • Frank Osterfeld
    Frank Osterfeld about 12 years
    John Zwinck: Actually, /usr/bin/gcc is llvm-gcc-4.2 and the overall compiler strategy is clearly llvm/clang. So I wouldn't expect much to happen on the gcc/OS X front anymore.
  • ulidtko
    ulidtko almost 12 years
    Be sure to do sudo port install gcc_select first. Or else you'll get Error: The 'list' command failed: The specified group 'gcc' does not exist.
  • Frank Osterfeld
    Frank Osterfeld over 11 years
    XCode 4.6 release notes: "Xcode 4.6 is the last release to include the LLVM-GCC compiler and the GDB debugger." developer.apple.com/library/mac/#releasenotes/DeveloperTools‌​/…
  • 11684
    11684 almost 10 years
    You can set which compiler XCode uses, in the target or project settings (I'm afraid I don't recall where exactly) is a path to compiler. I needed to change it to compile some library.
  • user5779223
    user5779223 about 8 years
    Why I got this warning and this error: Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'. Error: Insufficient privileges to write to MacPorts install prefix. ?
  • user124384
    user124384 over 6 years
    When I ran port select --list gcc it returned Available versions for gcc: none (active) even though I know I have two versions of gcc installed, so this was no help at all.
  • Guy
    Guy almost 5 years
    creating links in your /usr/bin/ folder is a big no-no. you'll end up having to reinstall the whole OS when things go sour