I installed gcc 4.6. from macports (for support of C++0x). But when I check the 'gcc --version` it is showing older version. How to use the newer gcc installed by macports?
Asked
Active
Viewed 4.8k times
46
-
possible duplicate of [Update GCC on OSX](http://stackoverflow.com/questions/837992/update-gcc-on-osx) – wkl Dec 02 '11 at 18:43
-
Also see [Using the Right Compiler](https://trac.macports.org/wiki/UsingTheRightCompiler) on the MacPorts wiki. – jww Jul 06 '15 at 14:55
3 Answers
115
You can control the symlink in /opt/local/bin/gcc
by using port select
. You can see available version using port select --list gcc
. Anything listed with mp- as prefix refers to MacPorts' own port, gcc42 and llvm-gcc42 refer to the compilers shipped with Xcode by Apple.
Example from my system:
$ port select --list gcc
Available versions for gcc:
gcc42
llvm-gcc42
mp-gcc45
none (active)
$ sudo port select --set gcc mp-gcc45
Password:
Selecting 'mp-gcc45' for 'gcc' succeeded. 'mp-gcc45' is now active.
After that, either open a new terminal window or issue hash -r
to make bash recognize the change.

raimue
- 4,322
- 1
- 21
- 31
-
I've done this, and when I type `port select --list gcc`, it says that mp-gcc47 is active. However, when I do `gcc --version`, it still says it's using version 4.2... I've tried opening a new terminal and typing `hash -r`. Any idea why? – FrancesKR Jul 18 '13 at 23:12
-
Check your PATH, maybe there is some other gcc (or a symlink) before /opt/local/bin. Could also be some alias or function in your shell. Try 'type -a gcc' to see what bash uses for the lookup. – raimue Aug 01 '13 at 17:17
-
When I do `type -a gcc`, I get two lines: "gcc is /usr/bin/gcc gcc is /opt/local/bin/gcc". When I print my PATH, I get ".:/opt/local/var/macports:/usr/local/bin:/sw/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/hallamsoft:/opt/local/bin:/usr/local/git/bin:/usr/texbin:/usr/X11/bin". Sorry, I'm not totally sure what either of these mean, but I did put Macports first in my path. – FrancesKR Aug 01 '13 at 17:25
-
2Sorry, but you certainly did not put MacPorts first. Watch the paths closely, you have /opt/local/var/macports near the front, but that will not actually contain any binaries. /opt/local/bin follows later in the list, after /usr/bin. That is why bash is picking up gcc from there. (Also you should never list "." in your PATH, it's a harmful vulnerability to do so). – raimue Aug 02 '13 at 00:03
9
I remember it being something like g++-mp-4.6
. I believe it's enough to set the environment variable CXX
to that.

Nikolai Fetissov
- 82,306
- 11
- 110
- 171
-
2that worked. is there a way to use it by default so that I dont have to change all make files. – Nemo Dec 02 '11 at 18:51
-
4Why is this accept as best answer while Raim's answer is the correct one? – R. van Twisk Mar 10 '14 at 18:27
2
Just make sure macports' path comes first in your $PATH
. Or use gcc-mp-4.6
or something like that.

Michael Krelin - hacker
- 138,757
- 24
- 193
- 173
-
3@jli, the one from `/usr/bin`? Doesn't sound like a good idea :) – Michael Krelin - hacker Dec 02 '11 at 18:45
-
@MichaelKrelin-hacker you still need to set the active gcc as stated above. – ipatch Feb 24 '13 at 18:52
-
This was my problem, and a good possible answer to the question. I had already used port select and was puzzled when the gcc --version always reported the old version. It was totally a $PATH problem. – mikewoz Feb 25 '13 at 04:54