Change GCC Version to 4.7 on Mac OS X

2020-04-04 05:32发布

问题:

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.

回答1:

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.



回答2:

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



回答3:

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



标签: macos gcc g++