37

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?

1
29

First, examine your $PATH variable.

echo $PATH

The gcc from homebrew should reside in /usr/local/bin and when that is 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.

4
  • 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 9 '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
6

you can use gcc-7 instead

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

6

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.

1

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.
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

You must log in to answer this question.

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