31

I want to compile C++ code on MacOS X, using the g++ compiler. How do I install it?

0

6 Answers 6

30

That's the compiler that comes with Apple's XCode tools package. They've hacked on it a little, but basically it's just g++.

You can download XCode for free (well, mostly, you do have to sign up to become an ADC member, but that's free too) here: http://developer.apple.com/technology/xcode.html

Edit 2013-01-25: This answer was correct in 2010. It needs an update.

While XCode tools still has a command-line C++ compiler, In recent versions of OS X (I think 10.7 and later) have switched to clang/llvm (mostly because Apple wants all the benefits of Open Source without having to contribute back and clang is BSD licensed). Secondly, I think all you have to do to install XCode is to download it from the App store. I'm pretty sure it's free there.

So, in order to get g++ you'll have to use something like homebrew (seemingly the current way to install Open Source software on the Mac (though homebrew has a lot of caveats surrounding installing gcc using it)), fink (basically Debian's apt system for OS X/Darwin), or MacPorts (Basically, OpenBSDs ports system for OS X/Darwin) to get it.

Fink definitely has the right packages. On 2016-12-26, it had gcc 5 and gcc 6 packages.

I'm less familiar with how MacPorts works, though some initial cursory investigation indicates they have the relevant packages as well.

13
  • From what I see XCode is not free unless you enroll to iPhone or Mac Development programs, which is quite expensive unless you don't really plan to actually do that development. Do you guys know any free way to install g++ only? Thanks
    – lyuba
    May 8, 2011 at 18:09
  • @lyuba: It's possible to register as a developer for free. At least, last I checked it was. And then it's possible to download for free. May 9, 2011 at 2:29
  • I guess that no anymore, now it's around $4.99. My friend who writes for the iPhone has also confirmed that. For him it's free since he is on the developer program. I've solved my case with installing XCode 3.
    – lyuba
    May 9, 2011 at 18:16
  • 3
    don't forget that the newest version requires you to download the command-line tools in the XCode preferences and to also probably run "xcode-select" to setup symlinks. Apr 1, 2012 at 23:39
  • 6
    this is now a non-answer, g++ is an alias for clang Dec 26, 2016 at 20:52
10

Installing XCode requires:

  • Enrolling on the Apple website (not fun)
  • Downloading a 4.7G installer

To install g++ *WITHOUT* having to download the MASSIVE 4.7G xCode install, try this package:

https://github.com/kennethreitz/osx-gcc-installer

The DMG files linked on that page are ~270M and much quicker to install. This was perfect for me, getting homebrew up and running with a minimum of hassle.

The github project itself is basically a script that repackages just the critical chunks of xCode for distribution. In order to run that script and build the DMG files, you'd need to already have an XCode install, which would kind of defeat the point, so the pre-built DMG files are hosted on the project page.

6

Type g++(or make) on terminal.

This will prompt for you to install the developer tools, if they are missing.

Also the size will be very less when compared to xcode

4

Download Xcode, which is free with an ADC online membership (also free):

http://developer.apple.com/technology/xcode.html

2

Here is how to do it on the newer mac chips and how to switch from clang(default) to g++

  • Install g++ through home-brew.

  • Check out the version you just installed, probably 12th or higher

  • You can make a symbolic link from g++-12 to g++

In order to do it, just type in your terminal:

sudo ln -s $(which g++-12) /usr/local/bin/g++.

Now open a new terminal and check your version again and you should see g++ instead of clang

g++ --version
1

xcode is now available for free from the app store. Just "buy it" (for free) and it will download. To get the command line tools go into preferences/downloads and "install command line compiler tools".

Instead of gcc you are using clang, but it works the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.