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?
-
1I have gone through this issue and worked for me was this link below. The answer from Mark Setchell was the best for this issue. stackoverflow.com/questions/28970935/…– user1896293Mar 18, 2018 at 13:45
6 Answers
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.
-
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. Jul 13, 2016 at 23:58
-
2Paths 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. Jul 14, 2016 at 21:04
-
6Homebrew does not put
gcc
in/usr/local/bin
. This is for compatibility as everything would then usegcc
instead of the systemclang
compiler (which is also aliased asgcc
). What it does do is put a versioned link in/usr/local/bin
such asgcc-8
.– JasonMay 9, 2019 at 21:27 -
2“Everything” is a bit of an overstatement; only things that explicitly call “gcc” and actually depend on “gcc” not referring to
gcc
but toclang
. (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.) Apr 13, 2020 at 21:29 -
1Thanks @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 9, 2021 at 9:30
If which gcc
gives you
> which gcc
/usr/bin/gcc
You have two options:
Make a new
gcc
symlink under/usr/local/bin/
.Homebrew links own gcc under
/usr/local/bin/gcc-<version>
for compatibility. So, doingln -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
.
-
Something to add, need to restart the shell for this one to take effect Oct 8, 2021 at 18:40
-
Homebrew needs to make a stable intermediate symlink that updates with upgrades (
homebrew-gcc
or something). Then you can symlink to that and not have to manually update your symlink. Jul 10 at 15:46
you can use gcc-7
instead
reference https://github.com/Homebrew/legacy-homebrew/issues/40374
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
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.
[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
-
1Ignoring 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, 2021 at 19:38
-
-