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
.
gcc version showing 4.2.1 even after installing 4.9 (Homebrew)
homebrew installs gcc with version specific suffixes, so when you install gcc49
, it gets called gcc-4.9
on the command line. This is intended to prevent interference with the gcc
stub (which is actually clang) which is provided by the OSX command line development toolchain, as well as to allow you to install gcc-4.8
, gcc-4.7
, etc. alongside each other.
Generally defining the environment variables CC=gcc-4.9
and CXX=g++-4.9
should allow you to compile autoconf based packages, as well as standard makefile based projects using the gcc-4.9
compiler, rather than using the default cc/gcc
.
How can I install gcc 4.2.1 on OSX Sierra?
You can install previous version of gcc pretty easily using homebrew.
If you have homebrew installed you can get gcc 4.9 by running
brew install gcc@4.9
After it is installed gcc
will still map to the clang that came with your mac. The newly installed gcc
will be installed at /usr/local/bin
and be called something like gcc-4
.
You can find the exact executable name using
ls /usr/local/bin | grep gcc
Hopefully 4.9 is close enough to 4.6 for your purposes.
Mac OS X: Installed and linked gcc 6.2 with Homebrew, but gcc --version still says 4.9.2
You created the symlink with the proper target name (gcc
) but forgot to set the PATH
to your newly installed location (or set it after the system path).
What's confusing is that your system already has a version of gcc installed. You have to override the default path so your gcc
command comes first.
edit your
~/.profile
file and check PATH adjustment. Add a line in the end which contains:export PATH=/usr/local/Cellar/gcc/6.2.0/bin:$PATH
Then open a new terminal and ensure that which gcc
prints
/usr/local/Cellar/gcc/6.2.0/bin/gcc
Using Homebrew with alternate GCC
These answers are all fairly old now. It seems that recent versions of homebrew have a '--cc' option that enables you to select the c compiler to use. For example
brew install --cc=gcc-6 <package-name>
will install using the brew version of gcc
GCC on Mac OSX--multiple versions of gcc
By default, your gcc
installations should be in /usr/bin/
. So, for example, to use gcc 4.2, you'd use /usr/bin/gcc-4.2
to compile your files. You can do ls /usr/bin/gcc*
to see which ones you have installed.
EDIT:
If gcc
isn't installed in the default path (i.e., /usr/bin/
), then you can execute locate gcc
in the terminal to find where it is located.
gcc installed via homebrew doesn't have manpages
Homebrew doesn't like using the names gcc
and g++
, since they're already in use by the operating system, and will refuse to link to gcc
and g++
. Instead it links gcc-4.9
and g++-4.9
.
In the same way, you can access the manpages with man gcc-4.9
rather than man gcc
and man g++-4.9
rather than man g++
.
Mac OSX is using gcc 6.2.0 but g++ 4.9.2
I had to do this:
cd /usr/local/Cellar/gcc/6.2.0/bin/gcc
ln -s g++-6 g++
which I guess I had previously done for gcc.
Related Topics
Equivalent C++ to Python Generator Pattern
Handling Header Files Dependencies with Cmake
What Is the Past-The-End Iterator in Stl C++
How to Store a 64 Bit Integer in Two 32 Bit Integers and Convert Back Again
Std::Unique_Ptr for C Functions That Need Free
Should We Generally Use Float Literals for Floats Instead of the Simpler Double Literals
Why Is There No Reallocation Functionality in C++ Allocators
Why Do Lambda Functions in C++11 Not Have Function<> Types
Can Std::Launder Be Used to Convert an Object Pointer to Its Enclosing Array Pointer
C++ - Decimal to Binary Converting
How to Typically/Always Use Std::Forward Instead of Std::Move
Can Google Mock a Method with a Smart Pointer Return Type
How to Create My Own Ostream/Streambuf
Multiple Definition Error Including C++ Header File with Inline Code from Multiple Sources
What Are Unevaluated Contexts in C++