Compiling Boost for the iPhone

November 10th, 2009

For a recent project I’ve had to compile the Boost C++ library for the iPhone. Much of the Boost library is header files so they are fine as nothing needs to be done, they just get copied into place, but the bits which do need compiling are a bit trickier. So I thought I’d share my experiences here.

First you need to download Boost (I went for version 1.40) from http://www.boost.org. Then you need to edit some of the Boost.Jam configuration. This involves creating a user-config.jam file in your home directory like so:

~/user-config.jam:
using darwin : 4.2.1~iphone
   : /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv6
   : <striper>
   : <architecture>arm <target-os>iphone <macosx-version>iphone-3.0
   ;

using darwin : 4.2.1~iphonesim
   : /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 -arch i386
   : <striper>
   : <architecture>x86 <target-os>iphone <macosx-version>iphonesim-3.0
   ;

Then edit tools/build/v2/tools/darwin.jam and add in the following under the macosx-versions variable:

.macosx-versions =
    10.6 10.5 10.4 10.3 10.2 10.1
    iphone-3.1 iphonesim-3.1
    iphone-3.0 iphonesim-3.0
    iphone-2.3 iphonesim-2.3
    iphone-2.2 iphonesim-2.2
    iphone-2.1 iphonesim-2.1
    iphone-2.0 iphonesim-2.0
    iphone-1.x
    ;

Then, you need to use the following lines to build Boost for iPhoneOS and iPhoneSimulator respectively:

./bjam --prefix=~/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-3.0 define=_LITTLE_ENDIAN link=static include=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/include/c++/4.2.1/armv6-apple-darwin9 install
./bjam --prefix=~/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr toolset=darwin architecture=x86 target-os=iphone macosx-version=iphonesim-3.0 link=static include=/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/include/c++/4.2.1/i686-apple-darwin9 install

It’s worth noting that at this stage everything will compile for the simulator but not for the device. This is because the device is missing two header files – ‘bzlib.h’ and ‘crt_externs.h’. To make it compile for the device simply copy these files from ‘/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/include/’ into your Boost folder and then everything should compile fine. I have no idea why Apple have omitted these header files from the device. The libraries are there, just the header files missing. I’ve created a bug on Radar for this, so please also do the same if you encounter this problem such that Apple will listen and include them.

That installs the files in ‘~/Developer/Platforms/iPhone(OS|Simulator).platform/Developer/SDKs/iPhone(OS|Simulator)3.0.sdk/usr’ which was where I wanted mine to go, but feel free to change that to wherever you want the libraries to end up. I did it this way because I created a custom SDK and then include that from iPhone projects within XCode.

I’ll try to update this with more information, this is merely a brain dump at the moment but hopefully it will be helpful for other people.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Twitter

5 Responses to “Compiling Boost for the iPhone”

  1. Tomek C says:

    Interesting… I tried to compile only one library (Graph), and in my case bjam ended up trying to copy compiled libraries to:

    ../../../libs/graph/build/~/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk/usr/lib/libboost_graph.a

    And there had been actually directory named ‘~’ (tilde) created, but guess what’s inside when accessing from shell? :-)

  2. Hi Tomek.

    That’s a tad confusing, to be honest. I’m guessing that the the Jam file in that library’s case is doing something strange and adding your prefix to what it wants (../../../libs/graph/build). Either that or something else strange is going on. Did you definitely do –prefix=~/… rather than –prefix=/~/… ?

    Also, what do you mean by what’s inside there when accessing from shell? If you are in “../../../libs/graph/build” and then do ‘cd ~’ then of course, it will cd to your home directory, because that’s what ~ is.

  3. Tomek,

    I just had the same thing happen to me actually. I think it’s best to specify exactly where you want the files to end up rather than using ‘~’ to hope to put it in your home directory.

    So you’d use something like:

    --prefix=/home/matthewg/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr

    Does that make sense?

  4. Carles says:

    Funny that at least with Boost v1.41, there does not seem to be the need to add the include=Developer/Platforms/… for the library to build correctly. In fact, I built it twice with and without the include option and the resulting .o files were identical (although not the .a files as they seem to contain a timestamp)

  5. Hi Carles,

    Yes it seems in 1.41 Boost has become much easier to compile for the iPhone. I’m leaving this here for reference but will endeavour to update it with some 1.41 specifics soon.

Leave a Reply