59

I just noticed that after installing OS X 10.9, the g++ compiler links to the clang compiler. Is there anyway to revert back to gcc/g++?

ryan
  • 725
  • 1
  • 6
  • 7
  • it's strange that i find that on os x 10.10 gcc is actually not links to clang, but something like that. Do you know the exact difference between the two? – Hongxu Chen Nov 06 '14 at 12:42

5 Answers5

72

It has been this way for a long time already. The "GCC" that came with 10.8 was really GCC front-end with LLVM back-end.

The best way to get GCC is via Homebrew. After the one-line homebrew install command on the bottom of the linked page, you just need:

$ brew install gcc49

Unlike macports, Homebrew doesn't clutter your system dirs and it's much better at managing versions and uninstalls. It also doesn't require the crutch known as sudo.

Adam
  • 16,808
  • 7
  • 52
  • 98
  • 14
    Your statements about macports are misleading and off topic. – trojanfoe Oct 23 '13 at 07:59
  • 31
    For anyone wondering, `brew tap homebrew/versions` is a prerequisite for `brew install gcc49` – Bede Constantinides Oct 31 '13 at 19:39
  • 18
    @trojanfoe blunt and a little mean, sure. But how is it misleading? It's certainly on-topic; OP is clearly looking for the best way to get GCC and implying that all package managers are exactly the same would be misleading. – Adam Oct 31 '13 at 21:14
  • 1
    It's misleading as it isn't true; homebrew causes way more system clutter and has many fewer ports available. I suggested macports and you suggested homebrew, which is fine, and that's how it should stand. The point is subjective and the argument we are about to have about it makes it off-topic. – trojanfoe Oct 31 '13 at 21:18
  • Can I still use clang alongside the gnu g++ compiler? – Eddy Feb 13 '14 at 12:51
  • @Adam when I use g++, it uses the clang compiler by default. What I wanted to know is if I install the gcc49 compiler with homebrew, will it mean that when I invoke g++, it will use the gnu compiler? Is there an easy way to switch between the two? – Eddy Feb 16 '14 at 21:40
  • 3
    @Eddy the default name of Homebrew's GCC is `g++-49`. You can change that by adding a Bash alias or a symlink in /usr/local/bin. That directory should already appear first in your `$PATH` so a symlink should override the default `g++` without you having to change any original OSX files. – Adam Feb 24 '14 at 06:35
  • 1
    Why not just `brew install gcc`? – Timothy Zorn Jul 06 '17 at 04:05
11

You'd have to install it from macports:

$ sudo port install gcc49

However I am not certain how you'd integrate gcc into Xcode 5, however you don't mention why you cannot use clang?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • @rholt So did you use the same *version* of gcc as well? I suspect not. – trojanfoe Oct 23 '13 at 07:46
  • @rholt I don't think so; your motivation is that your work compiles during the examination and for that you'd need to use the exact version of everything on both systems (and both systems would need to be the same platform). So you may as well use `clang` if you cannot reproduce it entirely. – trojanfoe Oct 23 '13 at 07:52
  • 3
    i can't use clang because my code depends on GNU gcc extensions like embedded functions... clang throws tons of errors – Michael Dec 08 '13 at 21:00
  • 1
    This does not work for me. I installed gcc48 from macports but gcc still points to clang in terminal. – Justin Jan 17 '14 at 03:37
  • @Justin Sounds like you haven't configured `$PATH`, configuration of which is part of the macports installation process. – trojanfoe Jan 17 '14 at 06:27
  • @trojanfoe macports added export PATH=/opt/local/bin:/opt/local/sbin:$PATH to my '.bash_profile'. Could there be anything else missing? – Justin Jan 18 '14 at 00:08
  • @Justin If you've restarted Terminal.app and `$PATH` is correct (do `$ echo $PATH`) then you probably need to *macports gcc select*; see this answer (not the accepted one, the one with more upvotes): http://stackoverflow.com/questions/8361002/how-to-use-the-gcc-installed-in-macports – trojanfoe Jan 18 '14 at 06:49
  • @trojanfoe Thank you! Thats been driving me insane. – Justin Jan 21 '14 at 07:11
  • After you install gcc in mac ports, you need to run sudo port select --set gcc "gcc version" in order to be able to call the g++ compiler from in mac ports. Otherwise, the system will continue to call clang++ behind the scenes. – Zachary Kraus Jul 27 '15 at 00:43
  • In Xcode as far as I know you cannot replace the clang compiler unless you use a custom build file. I can't remember the exact call sequence but xcode has it's own version of dynamic linker in the apps library. This dynamic linker from a previous issue I had calls the compiler in /usr/bin. Since the gcc compiler in /usr/bin is linked to clang. Clang is consequently always called. To better understand what I am referring to check out https://stackoverflow.com/questions/25961623/c11-compilation-issues-in-codeblocks. – Zachary Kraus Jul 27 '15 at 00:54
4

Accessible and up to date GCC packages are at http://hpc.sourceforge.net/

  • I just realized your answer was the same as the one I was going to post. Anyway let me add some more directions. Download their package and follow their directions. After installing to /usr/local/bin, you might want to edit your PATH variable and append /usr/local/bin to the front of PATH. This will guarantee that when you call gcc or g++ you are actually calling the correct one and not an older mac deprecated compiler. Note: this step is only important if you have a g++ compiler in the /usr/bin directory. I do since this computer had 10.5 originally. – Zachary Kraus Sep 23 '14 at 04:07
3

you can also get a g++ compiler from http://hpc.sourceforge.net/. They have the compiler in a pre-built package. Just download and follow their directions. Note you may need to change your executable PATH and have /usr/local/bin ahead of /usr/bin. This is to insure g++ calls the right compiler.

Zachary Kraus
  • 1,051
  • 10
  • 21
  • 3
    Just out of curiosity why was this down voted? The post explains how to get an alternative version of the g++ compiler and change your system settings so that g++ no longer calls clang's C++ compiler. Isn't that what the original poster was asking? – Zachary Kraus Jul 27 '15 at 00:38
-1

This is by design, and not new in OS X 10.9. Apple deprecated GCC a long time ago. Just use Clang, or if you need fortran/openmp/..., install GCC yourself through homebrew/macports/...

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 44
    You'll find a difference between not supporting GCC and masquerading clang as being gcc. – deadalnix Nov 11 '13 at 21:10
  • 4
    They're quite different -- lots of existing codebases fail to compile in Clang where they worked fine in gcc/g++. – Desty Jan 06 '15 at 10:56
  • 2
    What's the point? It doesn't stop me from using Gcc, doesn't stop me from reverting the only observable difference `alias g++ /path/to/g++`, and since this hardlink doesn't display a message "USE CLANG INSTEAD OF GCC, CAUSE WE HATE FREE SOFTWARE", it doesn't even communicate their intent. It looks like a solution looking for a problem. – Alex Petrosyan Sep 10 '18 at 14:33
  • 3
    Apple isn't entitled to deprecate GCC. – Arne May 02 '19 at 09:30