45

This has been plaguing me for awhile now. I am trying to compile a huge C++ file (I know it works as I it works fine on my Arch Linux computer at work). When I checked my GCC version on my mac It returns the following

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix

I have also installed the most recent GCC version using Homebrew with

brew install gcc49

My question now is how do I apply that newly installed GCC version to be the default version that the terminal uses?

I am also aware that when you use homebrew to isntall gcc it names it gcc-49 so that there is no confusion between packages.

I have no idea how to replace the 4.2.1 version that comes with XCode with the 4.9 version I have installed.

Thanks

Edit: Switched to my mac to get the full return statement of gcc --version

Edit 2: My end game here is to be able to navigate to the directory and be able to type

make
sudo make install

to install the daemon that has been made. Right now that returns tons of errors with random packages and the Standard Library

Dacotah
  • 581
  • 1
  • 5
  • 8
  • 1
    Unchecked, but I guess you just run `/usr/local/bin/gcc` and make sure `/usr/local/bin` is near the start of your PATH. – Mark Setchell Mar 10 '15 at 18:29
  • Not exactly sure what that means. I am extremely new with the mac terminal and how to do anything with it. I added this if that means anything to you echo "PATH=\"/usr/local/bin:$PATH\"" > ~/.bash_profile – Dacotah Mar 10 '15 at 18:31
  • Try `/usr/local/bin/gcc --version` – Mark Setchell Mar 10 '15 at 18:34
  • That exact line doesn't work, however /usr/local/bin/gcc-4.9 --version returns gcc-4.9 (Homebrew gcc49 4.9.2_1) 4.9.2 Copyright (C) 2014 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. – Dacotah Mar 10 '15 at 18:35
  • So edit your `~/.bash_profile` so it says `export PATH=/usr/local/bin:$PATH` and then start a new Terminal and try again like this `gcc-4.9 --version` – Mark Setchell Mar 10 '15 at 18:45
  • I get the same errors and gcc --version still returns 4.2. I also have PATH="/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" in my ~/.bash_profile though. Should that be removed? – Dacotah Mar 10 '15 at 18:48
  • 2
    Try `gcc-4.9 --version` – Mark Setchell Mar 10 '15 at 18:50
  • That works with the long PATH that is currently in there, as well as with your export – Dacotah Mar 10 '15 at 18:52
  • 1
    Good so you are all working now. If you want the simple command `gcc` to invoke your gcc-4.9, do this `cd /usr/local/bin; ls gcc` if nothing shows up you can do `ln -s gcc-4.9 gcc` and that will become your default gcc. – Mark Setchell Mar 10 '15 at 18:57
  • Thanks! running make in the directory doesn't work so I will look into that next. Could you post that the export and the last comment you made as an answer and I will mark it as correct – Dacotah Mar 10 '15 at 19:02

4 Answers4

72

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    After setting a symbolic link I would recommend closing the terminal and open a new, then write gcc -v. Mac osx shew in the old terminal window that nothing has changed. – Peter S. Mar 23 '20 at 19:39
  • Is it not `.zshrc` or `.zprofile` on MacOS Catalina? – PatrickT May 18 '20 at 17:00
  • I had to do the same for g++ to fix my problem – Mathy Oct 14 '20 at 10:11
  • 1
    @Mathy Good thinking - I originally answered for `gcc` and `C` but I guess people could equally be looking for `g++` and `C++` so I added a note at the end. Thanks. – Mark Setchell Oct 14 '20 at 10:16
9

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

eeny
  • 111
  • 1
  • 3
1

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.

Variable Length Coder
  • 7,958
  • 2
  • 25
  • 29
  • 1
    I did not, simply because the Makefile was created before me and was setup for GCC. The solution to the problem ended up being to `cd /usr/local/bin ln -s gcc-4.9 gcc` as stated by @MarkSetchell – Dacotah Mar 10 '15 at 19:07
1

Download the gcc binaries untar and copy the bin, lib include share and libexec files to your /usr directory then type gcc --version this is what i expect you to see

gcc --version gcc (GCC) 4.9.2 20141029 (prerelease) Copyright (C) 2014 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.

  • 3
    This is a really dangerous suggestion, you shouldn't be dropping random stuff in /usr, and while I've never installed gcc and friends from binaries (always from source), I'd expect some sort of installer that puts things in the right places and sets up the various symlinks correctly, etc. – blm Nov 18 '15 at 18:08