8

On OS X, I currently have a couple versions of gcc installed. Whenever I use gcc -v or g++ -v, it tells me: gcc version 4.2.1. I have installed gcc 4.7, though, in the interest of taking advantage of C++11. How can I change it so that gcc/g++ points to 4.7 instead of 4.2.1?

EDIT: I have homebrew, not macports.

muttley91
  • 12,278
  • 33
  • 106
  • 160

3 Answers3

7

I think you can do something like this. Go to /usr/bin

Assume you have install the gcc-4.7 using home brew in mavericks. then type:

cd /usr/bin
sudo mv gcc gcc_mavs
sudo ln -s /usr/local/Cellar/gcc48/4.7.0/bin/gcc-4.7 gcc

After this type to verify if you have select the right gcc compiler.

gcc -v

Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102
BITTNT
  • 91
  • 1
  • 1
6

Tricky question if we don’t know the full path of the other installs. But basically you could change the $PATH order in your local user .profile settings. So let’s say your 4.7 install is in /usr/local/bin/ & we know the Apple default version is in /usr/bin/ then edit your .profile so /usr/local/bin/ comes before /usr/bin/ in $PATH order.

Default should be something like this:

export PATH=/usr/bin:/usr/local/bin:[etc, etc, etc]

Adjusted should be something like this:

export PATH=/usr/local/bin:/usr/bin:[etc, etc, etc]

There is a way to force this change systemwide for all users, but I do not recommend that. Don’t muck around with the deeper—and often non-standard—ways Apple implements a *nix environment. Keep it local to your user.

EDIT: Check the discussion here to see if any solutions offered can help you. It does seem like installing gcc_select via MacPorts would be the cleanest solution.

Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • I think 4.7 is in `/usr/bin/` , so it's not different than the default you specify. Furthermore, I used unlink on the gcc that was being used before and it disappeared from `/usr/bin/` folder so I think that might support this? So how can I now link gcc-4.7 to gcc? – muttley91 Jan 26 '13 at 06:55
  • Like I said, before without knowing the file system topography of your setup, not too sure what to say or do to help. – Giacomo1968 Jan 26 '13 at 06:58
  • PS: Check the edit I just made to my answer above. Another discussion seems to offer a cleaner solution. – Giacomo1968 Jan 26 '13 at 07:01
  • As I'm currently on homebrew, I feel like switching to macports could be a big pain. But I'm looking into it as I'm not overly attached to homebrew. Also, all installs seem to be located right in /usr/bin/, does this help? – muttley91 Jan 26 '13 at 07:05
  • So gcc and g++ currently exist as links to gcc-4.7 and g++-4.7 respectively, and this is in /usr/bin. Also, /usr/bin is at the start of $PATH. What do I need to do so I can type gcc or g++ anywhere to be able to use it? – muttley91 Jan 26 '13 at 15:08
0

I found a very good tutorial online. Although, it is a little bit older (describes the process for gcc 3.2) the same general approach can still be used http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/install_gcc.html