How to set gcc 4.8 as default gcc compiler
Assuming you're using bash (it's the default), then you can add /usr/local/bin as your top priority in PATH like this:
echo "PATH=\"/usr/local/bin:$PATH\"" >> ~/.bash_profile
This will ensure that /usr/local/bin is checked before all other areas of your path. Then just start a new terminal session to load the new variable.
Another way to do this:
cd /usr/bin
rm cc gcc c++ g++
ln -s /usr/local/bin/gcc-4.8 cc
ln -s /usr/local/bin/gcc-4.8 gcc
ln -s /usr/local/bin/c++-4.8 c++
ln -s /usr/local/bin/g++-4.8 g++
Thanks to you all for your help. I ended up just creating aliases within ~/.bash_profile
as follows:
alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'
The answer from Lynken is very helpful, but I adapted it with aliases since that's easier for me to undo if necessary.
Specifically, if PATH
is set such that /usr/local/bin
(where brew puts the link to gcc 4.8
) appears before appears /usr/bin
(where gcc
is linked by default), then creating links as Lyken suggested within /usr/local/bin
should theoretically work for me. In practice, it doesn't for some reason -- failing with a linker error and aliases work around that error without me needing to solve that issue, too.
The other benefit of aliases is that I'm not having to link which I want homebrew to handle and not have to compete with that tool for which version of gcc is linked in /usr/local
I use to gcc-4.8:
export CC=/usr/local/bin/gcc
export CXX=/usr/local/bin/g++
export CPP=/usr/local/bin/cpp
export LD=/usr/local/bin/gcc
alias c++=/usr/local/bin/c++
alias g++=/usr/local/bin/g++
alias gcc=/usr/local/bin/gcc
alias cpp=/usr/local/bin/cpp
alias ld=/usr/local/bin/gcc
alias cc=/usr/local/bin/gcc
and back to apple gcc:
export CC=/usr/bin/gcc
export CXX=/usr/bin/g++
export CPP=/usr/bin/cpp
export LD=/usr/bin/ld
alias c++=/usr/bin/c++
alias g++=/usr/bin/g++
alias gcc=/usr/bin/gcc
alias cpp=/usr/bin/cpp
alias cc=/usr/bin/gcc
alias ld=/usr/bin/ld
or put it in file and then: source <file>
Let's assume your actual shell initialization is done through ~/.profile
, then you will have to modify it so as to put /usr/local/bin
ahead of any other PATH component where gcc
and all its associated binary are.
Here is the way to perform this clean modification:
cd _shell_init=`egrep '(^| )PATH' .profile 2>/dev/null` if [ "${_shell_init}" = "" ] ; then # PATH isn't defined in .profile # install there the first relative definition of PATH echo 'PATH=/usr/local/bin:${PATH} export PATH' >>.profile . .profile exec ${SHELL} else # remove all occurences of /usr/local/bin wherever they might be # set into PATH, and insert it ahead of all other components sed -E -e '/(^| )PATH=/s,:/usr/local/bin,,' \ -e '/(^| )PATH=/s,/usr/local/bin:,,' \ -e '/(^| )PATH=/s,,&/usr/local/bin:,' .profile >.profile.new mv .profile.new .profile . .profile exec ${SHELL} fi
Beware: if your ~/.profile
is already structured, this shell script will have to be manually tuned to fit correct PATH definition
in the right place.