OSX - replace gcc version 4.2.1 with 4.9 installed via Homebrew

By default, homebrew places the executables (binaries) for the packages it installs into /usr/local/bin - which is a pretty sensible place for binaries installed by local users when you think about it - compared to /bin which houses standardisded binaries belonging to the core OS. So, your brew command should have installed gcc-4.9 into /usr/local/bin. The question is now how to use it... you have several options.

Option 1

If you just want to compile one or two things today and tomorrow, and then probably not use the compiler again, you may as well just invoke the gcc installed by homebrew with the full path like this:

/usr/local/bin/gcc-4.9 --version

Option 2

If you are going to be using gcc quite a lot, it gets a bit tiresome explicitly typing the full path every time, so you could put the following into your ~/.bash_profile

export PATH=/usr/local/bin:$PATH

and then start a new Terminal and it will know it needs to look in /usr/local/bin, so you will be able to get away with simply typing

gcc-4.9 --version

Option 3

If you just want to use gcc to invoke the compiler, without worrying about the actual version, you can do Option 2 above and additionally create a symbolic link like this

cd /usr/local/bin
ln -s  gcc-4.9  gcc

That will allow you to run the homebrew-installed gcc by simply typing gcc at the command line, like this

gcc --version

Note:

If you later want to install, say gcc-4.13 or somesuch, you would do your brew install as before, then change the symbolic link like this:

cd /usr/local/bin
rm gcc               # remove old link from gcc to gcc-4.9
ln -s gcc-4.13 gcc   # make new link from gcc to gcc-4.13

Note that if you are actually using C++ rather than C, you will need to adapt the above for g++ in place of gcc.


simply updating the order of $PATH in ~/.bash_profile to the brew installed version 'export PATH=/usr/local/Cellar/gcc/5.1.0/bin:$PATH' was not enough to make the switch for me

changing the alias in your ~./bash_profile (alias gcc='gcc-5') works, but can be confusing i.e. which gcc will return the Clang version

what worked for me was to make a symbolic link in the brew gcc directory as well as update the path (point 1 above)

cd /usr/local/Cellar/gcc/5.1.0/bin/gcc
ln -s gcc-5 gcc

now which gcc returns the correct version 5.1.0


OS X does not come with GCC installed (4.2.1 or otherwise). Clang is the default system compiler and has been for some time. It is using the C++ headers from 4.2.1 when invoked as GCC. Have you tried compiling your code with Clang natively, instead of calling "gcc" (which calls Clang)? It has more modern headers and C++ support than the GCC emulation mode.