48

I installed gcc with brew install gcc, but when I type gcc the default behavior is still to use clang. How do I set things so that typing gcc in the terminal automatically uses the gcc installed by Homebrew?

NeutronStar
  • 723
  • 2
  • 7
  • 9
  • 1
    I have gone through this issue and worked for me was this link below. The answer from **Mark Setchell** was the best for this issue. https://stackoverflow.com/questions/28970935/osx-replace-gcc-version-4-2-1-with-4-9-installed-via-homebrew/28982564#28982564 – user1896293 Mar 18 '18 at 13:45

6 Answers6

30

First, examine your $PATH variable.

echo $PATH

The gcc from homebrew should be a symbolic link that resides in /usr/local/bin for Intel and Rosetta 2 installs or /opt/homebrew/bin for Apple Silicon. When this brewed version of gcc shows up in the path listed before the Xcode version of gcc/clang, you’re done - the local compilers will be called unless a package is hard coded to the full path of a different compiler than the one you have in /usr/local

If you change the PATH variable - be sure to log out of the shell or rehash the shell as appropriate.

This answer has an elegant solution using aliases as well - so you don't even have to think or care about path if you have more than one gcc installed. It goes deeper to let you choose which version of gcc to call if you happen to install more than one version.

bmike
  • 226,393
  • 78
  • 398
  • 871
  • Putting the homebrew gcc earlier in the $PATH variable than the Xcode version is sufficient to guarantee that the homebrew gcc will be referenced? I wasn't sure how double definitions in $PATH would work. – NeutronStar Jul 13 '16 at 23:58
  • 2
    Paths are order dependent. Putting /usr/local ahead of /usr is a standard way to override the default application. I've seen people put a ~/bin ahead of that. – Andrew Lazarus Jul 14 '16 at 21:04
  • 4
    Homebrew does not put `gcc` in `/usr/local/bin`. This is for compatibility as everything would then use `gcc` instead of the system `clang` compiler (which is also aliased as `gcc`). What it does do is put a versioned link in `/usr/local/bin` such as `gcc-8`. – Jason May 09 '19 at 21:27
  • 1
    “Everything” is a bit of an overstatement; only things that explicitly call “gcc” _and_ actually depend on “gcc” _not_ referring to `gcc` but to `clang`. (Such code should instead call either “clang” or perhaps “cc”.) The quantity of such broken code perhaps justifies this bit of trickery. (I am _not_ speaking officially for whoever my employer might have been when this decision was made, which I didn’t like.) – Flash Sheridan Apr 13 '20 at 21:29
  • Why do we assume brew installation of gcc is in `/usr/local/bin`? When I do `which gcc` I don't get the newest version that brew installed. `/usr/local/bin/gcc --version` gives `gcc (Homebrew GCC 10.2.0_4) 10.2.0` and this is just clang wrapper, but the version linked by brew should be `/usr/local/Cellar/gcc/11.2.0_3/bin/gcc-` and `/usr/local/Cellar/gcc/11.2.0_3/bin/g++-` – Brian Wiley Dec 09 '21 at 05:15
  • 1
    Thanks @BrianWiley - I’ve updates this for the Apple Silicon guidance from the FAQ as well as call out that a link named `gcc` is what matters for the $PATH variable. You are correct that non standard installs are possible and based on versions the link may point at several eventual cellar locations. – bmike Dec 09 '21 at 09:30
10

If which gcc gives you

> which gcc
/usr/bin/gcc

You have two options:

  1. Create an alias.

  2. Make a new gcc symlink under /usr/local/bin/.

    Homebrew links own gcc under /usr/local/bin/gcc-<version> for compatibility. So, doing

    ln -sf /usr/local/bin/gcc-4.9 /usr/local/bin/gcc

will point a /usr/local/bin/gcc symlink to gcc-4.9 installed by Homebrew which should override the gcc from /usr/bin if your PATH specifies /usr/local/bin before /usr/bin.

Master
  • 103
  • 4
6

you can use gcc-7 instead

reference https://github.com/Homebrew/legacy-homebrew/issues/40374

Qiang Huang
  • 177
  • 1
  • 2
  • 6
1

When you build C/C++/Objective C etc. applications you usually do not run the C compiler from the command line you use a build system - which one of the simplest is a makefile.

The standard Unix way (e.g. from pre gcc being the only compiler) is that you pass information to the build system where your compiler is. Often this is the environment variable CC for C compiler CPP or similar for C++.

this is often done on the command line

e.g.

make CC=/usr/bin/clang  all # for Clang

make CC=/usr/local/bin/gcc-4.9 all # for gcc-4.9 under Homebrew
mmmmmm
  • 28,660
  • 17
  • 84
  • 140
0

To use Homebrew's version, you're best off creating symbolic links to the latest version of GCC installed by Homebrew, and placing them into /usr/local/bin. The example zsh script provided below will do this for you, and will also remove the version number suffixed to the filename by Homebrew's installation formula.

# Choose the directory containing the latest version of GCC
# as indicated by the highest number suffixed to 
# the filepath of the package directory
print -v version /usr/local/opt/gcc@<->(n[-1])
version=${version#*@}
for file in /usr/local/opt/gcc@${version}/bin/*-${version}(*); do
    tail=${file:t}
    ln -sf ${file} /usr/local/bin/${tail%-*}
done

Now you're good to go. You can check that it worked using the command below:

gcc --version

Output

gcc (Homebrew GCC 10.2.0_2) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
chahu418
  • 45
  • 6
0

[Only for m1 Mac]

Homebrew by default installs gcc to /opt/homebrew/bin. There you will be able to find gcc-11. You can also find this path by running which gcc-11.

You can then symlink this file to /usr/local/bin/gcc using this command:

sudo ln -s $(which gcc-11) /usr/local/bin/gcc

Just make sure that /usr/local/bin in your $PATH comes before /usr/bin to override clang

akshatcx
  • 21
  • 1
  • 1
    Ignoring for the moment that this is only valid for M1: why not just put /opt/homebrew/bin first in the PATH? And how does this solve the problem the OP has? – nohillside Oct 18 '21 at 19:38
  • this answer actually coincides with my reply [here](https://apple.stackexchange.com/a/450861/153840) – Lei Zhang Nov 21 '22 at 04:44
  • @nohillside because it doesn't link to that directory either :) – Nek Jan 09 '23 at 12:53