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.


5 Answers 5


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++
  • 1
    For users on Mac os Catalina or higher, if you want to replace the symbolic links your need to do these steps before replace them, and also your will need to do sudo rm -rf not just rm and sudo ln not simply ln
    – BSO
    Jan 19, 2020 at 22:48
  • Finally manage to do that. I also had to run csrutil disable in Restart mode on Mac.
    – Jack
    May 18, 2020 at 21:02

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>

  • This doesn't work anymore. We have to append the version to the end
    – rosshjb
    Nov 15, 2021 at 15:39

On Mac M1, if you use brew install gcc, gcc will be installed on "/opt/homebrew/bin/".

I am facing the same issue. Specifically, when I issue gcc --version, the terminal shows:

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Further looking into this, I found that in $PATH the path /opt/homebrew/bin/ was actually before /usr/bin/. But in the former location there is only gcc-12 in the folder, which causes gcc points to the later location (/usr/bin/).

In order for the GNU gcc to be found, I used this: ln -s gcc-12 gcc.

Restart the terminal and it's been solved:

gcc --version
gcc (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

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, 2013 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, 2013 at 13:14

You must log in to answer this question.

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