Installed gcc with Homebrew, now how to use that gcc instead of clang?
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?
Solution 1:
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.
Solution 2:
If which gcc
gives you
> which gcc
/usr/bin/gcc
You have two options:
-
Create an alias.
-
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
.
Solution 3:
you can use gcc-7
instead
reference https://github.com/Homebrew/legacy-homebrew/issues/40374
Solution 4:
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