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.

boost-1.42:
Since Boost has been updated, I thought I’d extend this article to explain how to compile boost-1.42 for the iPhone. Boost themselves have gone a long way to do everything for you now, so there’s much less that needs to be done. You now need to do this:

~/user-config.jam
using darwin : 4.2.1~iphone
   : /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 -arch armv7 -mthumb -fvisibility=hidden -fvisibility-inlines-hidden
   : <striper>
   : <architecture>arm <target-os>iphone <macosx-version>iphone-3.1.3
   ;

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

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

tools/build/v2/tools/darwin.jam
## The MacOSX versions we can target.
.macosx-versions =
    10.6 10.5 10.4 10.3 10.2 10.1
    iphone-3.2 iphonesim-3.2
    iphone-3.1.3 iphonesim-3.1.3
    iphone-3.1.2 iphonesim-3.1.2
    iphone-3.1 iphonesim-3.1
    iphone-3.0 iphonesim-3.0
    iphone-2.2.1 iphonesim-2.2.1
    iphone-2.2 iphonesim-2.2
    iphone-2.1 iphonesim-2.1
    iphone-2.0 iphonesim-2.0
    iphone-1.x
    ;
# <grab source from boost.org>
tar xzf boost_1_42_0.tar.gz
cd boost_1_42_0
./bootstrap.sh

# Install for device:
./bjam --prefix=${HOME}/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.3.sdk/usr toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-3.1.3 define=_LITTLE_ENDIAN link=static install

# Install for simulator
./bjam –-prefix=${HOME}/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/usr toolset=darwin architecture=x86 target-os=iphone macosx-version=iphonesim-3.1.3 link=static install
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Twitter

14 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.

  6. Steven Jones says:

    Great guide but unfortunately im using boost 1.42. Will you be updating this guide soon? That would be super helpful.

  7. @Steven – in Boost >1.40 they added some things to the bjam to enable easy compiling for iPhone.

    I haven’t looked at 1.42 yet, but I’ll try to do so and give a quick update on what has changed.

  8. Paul Solt says:

    Thank you for the post. I am using Boost so that I can use the unofficial Boost Logging Utility on the iPhone/iPad.

    You didn’t included the updated command for the iPhone Simulator and Boost 1.42, so I updated the command to the following:

    ./bjam –prefix=${HOME}/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr toolset=darwin architecture=x86 target-os=iphone macosx-version=iphonesim-3.2 link=static install

  9. @Paul Solt – thanks for that! Ironically I just today compiled it for the simulator for 3.1.3. I’ve updated the post to show how to compile for simulator. I’ve shown it for 3.1.3 but it can also be used for 3.2 like you’ve done.

  10. Gabriel says:

    Thank you so much for your instruction! I’ve compiled boost successfully for iPhone SDK 3.1.3.

    By the way, it may not be a good idea to copy the missing headers (bzlib.h and crt_externs.h) to compile for the device. Some guys in the imagemagick forum have got their app rejected for using these API:

    http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=16043&start=0

    Last time I checked, the missing headers are required only by boost::programoptions and boost::iostreams. Not a big deal, I guess :)

  11. @Gabriel – Thanks for the heads up with regard to the private API calls.

  12. foogy says:

    Did anyone successfully build boost 1.43 for iPhone?
    The modifications to .macosx-version in Darwin.jam doesnt seem to apply for 1.43.

  13. @foogy – Yep I’ve compiled 1.43. It should “just work” without needing to do anything to Darwin.jam I am pretty sure. I.e. all you need to do is run the `bjam’ lines from the boost-1.42 section.

  14. foogy says:

    Thanks Matt, actually I also had to modify user-config.jam.
    I removed the tags from the file. (Thanks to grafikrobot on the boost irc channel for that)

    Also, some part of 1.42 and 1.43 clash with objective-c because of the use of “id” as a variable name in basic_regex_creator.hpp and perl_matcher_common.hpp.
    https://svn.boost.org/trac/boost/ticket/4132

    This issue is fixed in the trunk.

Leave a Reply