Boost with GCC on OS X

I am writing this post in case anybody else wants to build the Boost libraries using the Homebrew version of g++-4.8 on OS X (not Apple's clang++ masquerading as g++). This is useful if you happen to build cross-platform projects, prefer GCC (Clang is great, really it is, but I don't use it on Linux), and do not want to build and link specifically for OS X.

Preliminaries

First off, I am assuming you have downloaded Boost Version 1.55.0, if not, go grab it and extract it. You should also have installed GCC via brew install gcc48 (if you have yet to install Homebrew, go do that, it is pretty self-explanatory).

Now you should know that g++ is still not GCC, since Homebrew does not install it as a symlink by default (this is a good thing as most Homebrew projects except Apple's 'GCC'). GCC is available at g++-4.8. Go ahead and specify that in your configure.ac if you want (I do).

user-config.jam

The Boost configuration process is governed by a file, user-config.jam that is located (by default) strangely at ./tools/build/v2/user-config.jam (where . is of course the root of the Boost source code). Inspecting this file, we see the following section:

# ------------------
# GCC configuration.
# ------------------

# Configure gcc (default version).
# using gcc ;

# Configure specific gcc version, giving alternative name to use.
# using gcc : 3.2 : g++-3.2 ;

This hints pretty strongly at what we might need to do: specify the compiler!

But hold up, you should know something about Boost, being a cross-platform project itself, it has many different toolchains (AKA toolsets) for which it can build. Contrary to what it may seem, using GCC on OS X does not mean using the GCC build toolset; we still need to use the Darwin (Apple) toolset. Specifically, we need to get the Darwin toolchain to use g++-4.8 as its compiler.

We can do that pretty easily (but believe me, figuring it out in the first place was... fun). Add this line to user-config.jam:

using darwin : 4.8 : g++-4.8 ;  

I really do not know if the middle part (the version) is necessary, but hey, this worked. Also, you can of course adjust this for any other version of GCC you want (or really any compiler).

Bootstrapping

With our "hack" applied, now we just need to bootstrap Boost. Easy enough: ./bootstrap. If you want to be really specific, you can do this:

./bootstrap.sh --with-libraries=program_options --with-toolset=darwin --prefix=/usr/local

This says that I want to build only the program_options library (which is what I use Boost for), that I definitely want to use the darwin toolset (being on OS X, the bootstrapper does figure this out automatically), and that when I install the libraries, I want them in /usr/local (which again is probably automatic, at least it is for Boost on Linux).

Building

With Boost bootstrapped, building is actually pretty easy, just run ./b2. If it fails, well, you can find me on Freenode as 'andschwa'. All these tools have a --help though, and I found the debug options particularly useful.

Installing

If it built, installation is even easier: run ./b2 install. This will copy the built libraries to the prefix you gave to the bootstrapper. After installing Homebrew on OS X, /usr/local should be user-writable, and thus installing there should not require sudo.

TL;DR

  • Add using darwin : 4.8 : g++-4.8 ; to ./tools/build/v2/user-config.jam to specify the compiler

  • Bootstrap with ./bootstrap

  • Build and install with ./b2 install