I recently installed gcc 4.8 using brew on OSX 10.7.5 (Lion). I can now compile using gcc 4.8 by using

g++-4.8 some_file.c

or using the default gcc 4.2 by using

g++ some_file.c

I want to use gcc 4.8 as the default compiler for Xcode and if I type gcc at the terminal. I suppose I must alter gcc-related links within dirname $(which gcc).

When I do

ls -al $(dirname $(which gcc)) | grep 'gcc\|g++\|c++'

I get the following:

lrwxr-xr-x     1 root   wheel         7 Jul 31 12:17 c++ -> clang++
-rwxr-xr-x     1 root   wheel    909360 Nov 18  2011 c++filt
lrwxr-xr-x     1 root   wheel         5 Jul 31 12:17 clang++ -> clang
lrwxr-xr-x     1 root   wheel        12 Jul 31 12:17 g++ -> llvm-g++-4.2
lrwxr-xr-x     1 root   wheel        12 Jul 31 12:17 gcc -> llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        28 Jul 31 12:17 gcov-4.2 -> ../llvm-gcc-4.2/bin/gcov-4.2
lrwxr-xr-x     1 root   wheel        52 Jul 31 12:17 i686-apple-darwin11-llvm-g++-4.2 -> ../llvm-gcc-4.2/bin/i686-apple-darwin11-llvm-g++-4.2
lrwxr-xr-x     1 root   wheel        52 Jul 31 12:17 i686-apple-darwin11-llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/i686-apple-darwin11-llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        32 Jul 31 12:17 llvm-cpp-4.2 -> ../llvm-gcc-4.2/bin/llvm-cpp-4.2
lrwxr-xr-x     1 root   wheel        32 Jul 31 12:17 llvm-g++ -> ../llvm-gcc-4.2/bin/llvm-g++-4.2
lrwxr-xr-x     1 root   wheel        32 Jul 31 12:17 llvm-g++-4.2 -> ../llvm-gcc-4.2/bin/llvm-g++-4.2
lrwxr-xr-x     1 root   wheel        32 Jul 31 12:17 llvm-gcc -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2
lrwxr-xr-x     1 root   wheel        32 Jul 31 12:17 llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2

When I run: which gcc-4.8 I get /usr/local/bin/gcc-4.8.

Steps to get this done would be very helpful.

Please and thank you.

  • 1
    Any reason not to use the superior llvm clang? – Max Ried Aug 16 '13 at 4:58
  • Not specifically. What's the benefit of llvm clang compared to vanilla gcc ? – quine Aug 16 '13 at 7:22
  • 3
    While being faster it also offers meaningful warnings, a really precious feature gcc lacks entirely. – Max Ried Aug 16 '13 at 10:15
  • I see. Is clang 3.3 the equivalent to gcc 4.8? I believe I can brew install it as well. – quine Aug 16 '13 at 17:29
  • First question to ask is re clang why can't you use the clang supplied with Xcode rather than gcc-4.8 - then we could answer of clang3.3 would work – user151019 Aug 16 '13 at 20:13

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++
  • My think path already contains usr/local/bin. echo $PATH reveals: /opt/local/bin:/opt/local/sbin:/opt/local/include:/usr/local/mysql/bin:/usr/bin/bjam:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/go/bin:/usr/texbin – quine Aug 16 '13 at 4:43
  • Added another option – Digitalchild Aug 16 '13 at 4:49
  • 1
    Yes it contains it but it is after /usr/bin so it doesn't get read first. PATH is sequential. – Digitalchild Aug 16 '13 at 5:01
  • 1
    Both options seem to not work for me. I moved /usr/local/bin to appear 1st in PATH -- which had no effect -- calling gcc still looks at gcc 4.2.I then manually created the symlinks, but when I open a new terminal window and g++ or (cc|c++|gcc) all are not found. Strange. Thanks for your help – quine Aug 16 '13 at 19:07
  • 3
    I wouldn't advise to play within /usr/bin of any Unix (which MacOS X is)… unless you're a specialist working on sources of a new version of the next Unix version ☺. – dan Aug 16 '13 at 20:52

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

  • You might have to change this for any new binary you will install within /usr/local/bin. You may also have other binaries installed and hidden because of the bad positionning of /usr/local/bin within your PATH. Please consider once more my answer,it is attacking your real problem once, correctly and forever. – dan Aug 16 '13 at 20:56
  • Right. I did mention earlier to Lyken that I altered PATH and put /usr/local/bin before all. I concede that your statement is correct & that I needed to alter PATH, but (in my case -- i.e default brew install of gcc 4.8) I still will not call gcc 4.8 by default when I call gcc because there is no symlink between the two by default. I had to manually create it as I did above. – quine Aug 17 '13 at 0:13
  • I'd say its different enough to be its own answer, I would mark your answer as solved though. – Digitalchild Aug 17 '13 at 6:36
  • Ok -- thanks Lyken. I will as soon as the system allows me to do so. – quine Aug 17 '13 at 7:18
  • 1
    I tried alias too but it only works properly if you run gcc directly from terminal. If you use a makefile, clang is still used, I only found this issue when using a compiler option that only available in gcc. – Tien Do Sep 20 '18 at 1:31

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:


_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}
    # 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}

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.

  • This is certainly perfect for modifying PATH so that /usr/local/bin is seen first. This will likely save me headaches in the future. I wish I could +1 you. My issue though is that gcc is still linked to gcc 4.2 and not gcc 4.8 because there is no symlink for gcc -> gcc-4.8 in /usr/local/bin. I would still need to either alias gcc as gcc-4.8 within ~/.profile or symlink (ln -s gcc-4.8 gcc) in /usr/local/bin after updating PATH with your script. Ye? – quine Aug 17 '13 at 0:04
  • I'm surprised brew didn't add the right symbolic links. Did you read this answer on the same subject: apple.stackexchange.com/a/38247/22003 ? – dan Aug 18 '13 at 13:14

It is assumed the making alias or linking to different gcc binaries will make them use their own include before the default system include folder.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .