Main content

Installing GCC on Mac

Suppose you want to use the latest and greatest version of GCC on the Mac because you want to use features in C++11 that are not yet available in XCode. There are probably a few ways to do this, but one way is via MacPorts which provides precompiled binaries for tons of packages.

You can follow the following steps to install GCC 4.7 via MacPorts. Others versions (including GCC 4.8) follow a pretty similar procedure.

  1. Browse to the MacPorts website. Download the DMG image for your operating system
  2. Install the package
  3. Open up a terminal Applications > Utilities > Terminal
  4. Make sure that you have the latest version by running the self update via the terminal command
sudo port selfupdate
  1. Install GCC via the terminal command
sudo port install gcc47

Now sit back and wait while MacPorts does its thing. One MacPorts is done, the next thing is to compile a simple program. Running gcc or g++ at the command line invokes the compiler installed with XCode. The new executables are installed to

/opt/local/bin/gcc-mp-4.7

and

/opt/local/bin/g++-mp-4.7
  1. Depending on your build environment, you may want to access GCC by running gcc or g++ at the command line. Because the MacPorts are installed to another location, you either need to either specify the full path, or set the compiler to use on your system. For the second option, you can use MacPorts to select the default compiler. First, identify which versions are installed on your system.
port select --list gcc

In my case, this returns

Available versions for gcc:
   gcc42
   llvm-gcc42
   mp-gcc47
   none (active)

To select GCC 4.7, run the command

sudo port select --set gcc mp-gcc47
Password:
Selecting 'mp-gcc47' for 'gcc' succeeded. 'mp-gcc47' is now active.
1 1 1 1 1 Rating 0.00 (0 Votes)

Comments  

Shidong
+3 # Shidong 2012-09-10 09:29
thanks!!! this helps me a lot.
finally I can compile my code without Xcode. :P
Bei
+3 # Bei 2012-10-05 17:55
This post is so so helpful.
Fer
+7 # Fer 2012-10-21 18:43
"'mp-gcc47' is now active." means that Xcode will use gcc47 too?
Mike
+2 # Mike 2012-12-05 01:12
I prefer coding in Sublime Text 2 and compiling via command line... Great post :)
Scott
+1 # Scott 2013-05-23 15:28
Supremely helpful. Now if I can just figure out how to get Eclipse to find gcc 4.7 ...
ilciavo
0 # ilciavo 2013-06-12 11:14
Thanks for this post. However, I think you should mention some differences between gcc47, gcc47 +universal, and gcc47 +universal +fortran.
pawan
0 # pawan 2013-06-22 21:59
thanks a lot
loufoque
+1 # loufoque 2013-07-07 07:56
You can already use the compilers provided by Xcode from the command line...
Installing other compilers is only if you want a more recent version of GCC.
breathe
0 # breathe 2013-08-28 17:52
Or if you need 32 bit libraries in 10.8... :)
Jared Burrows
+1 # Jared Burrows 2013-07-17 03:15
Thanks for the help!
Satish Singhal
0 # Satish Singhal 2013-08-22 13:59
Thanks. This really works.
Jae Yoo
+1 # Jae Yoo 2013-08-30 00:47
I am getting error when compiling
"
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
"

Do you have any idea?
Garret Fick
+1 # Garret Fick 2013-09-08 14:29
Linker errors are usually hard to decipher. In this case, if you are using C++, you need to link with the C++ library by adding the-lstdc++ to the command line.
Umar
0 # Umar 2013-09-06 18:03
Hi, I have installed the macports on my machine but I get the following errors when I run self update command. Can any one guide me how to fix this issue ?

"UMARs-MacBook-Pro:~ umar909$ sudo port selfupdate
Password:
---> Updating MacPorts base sources using rsync
Error: Error synchronizing MacPorts sources: command execution failed
Please run `port -v selfupdate' for details.
Error: /opt/local/bin/ port: port selfupdate failed: Error synchronizing MacPorts sources: command execution failed
Garret Fick
+1 # Garret Fick 2013-09-08 14:31
Did you try running with the -v switch? I expect that should give more details about the problem.
ilciavo
0 # ilciavo 2013-09-12 12:54
Hello guys, do you know how to install gcc49?
I tried:
sudo port install gcc49
Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'.
---> Computing dependencies for gcc49
Error: Unable to execute port: Can't install libgcc-devel because conflicting ports are installed: libgcc
Garret Fick
-1 # Garret Fick 2013-09-12 15:30

I haven't tried this, but it make sense to me. Also, this may break other ports you have. You have been warned. :-)

GCC 4.9 is not yet released, so it depends on the development version of libgcc-devel. You must uninstall then current version of libgcc first.

sudo port uninstall --follow-dependencies libgcc

Then

sudo port install gcc49

You can find details on the port commands on the MacPorts website

David Walker
+1 # David Walker 2013-10-10 13:52
Hi

Everything worked OK until I got to the final step and tried to use gcc, which gave me the following message:

gcc-mp-4.7: error trying to exec '/opt/local/bin /i686-apple-dar win10-gcc-4.2.1 ': execvp: No such file or directory

Do you know how to fix this problem?
Wolfgang Mayer
0 # Wolfgang Mayer 2013-11-04 11:35
This is simply the handiest and most straightforward instruction to get the latest gcc-compiler-se t working on your Mac. In my case gcc48 on my MacBook Pro running Mavericks.
Cla
0 # Cla 2013-11-06 13:07
for update your version of gcc you should do this:
sudo port --set gcc mp-gcc49
Orhan
+1 # Orhan 2014-03-12 10:37
When I try using gcc-mp-4.7 as the compiler I get a fatal error as to stdio.h was not found, when I use XCode's gcc there are no problems. Would you know the reason?

Thanks!

You have no rights to post comments