Building Boost for the iPhone

We’re working on very interesting stuff for new products behind the scenes. It’s a special challenge to write cross platform code. For that reason we are creating a very solid code base that we can reuse in all future products. We decided to use C++ and wanted to support Mac OS X, Windows, iPhone OS and potentually Linux. Maybe I’ll write more about our concepts and decisions about that in a future post, but for now I’ll concentrate on Boost.

Boost is a collection of free cross platform and high-quality C++ libraries. Most of the classes can be used “header only”, so you include the appropriate header file in your code and it “just works”. Maybe the most famous class of Boost is the Smart Pointer class shared_ptr. In our case we also needed the compiled libraries of Boost. Therefore we had to compile it amongst others for the iPhone. There are a few difficulties when building it for the iPhone and I had to google a lot until I found out all the details. Now I want to share my solution how I managed to build the Boost libraries for the iPhone – and here it is. I tried it with Boost version 1.42.0:

  1. You need a Mac with Mac OS X and installed iPhone SDK, of course.
  2. Download the Boost sources and extract them somewhere on your hard disc.
  3. In the directory with the Boost sources, open the file tools/build/v2/user-config.jam and fill it with the following content:

    using darwin : 4.2.1~iphone
       :
    /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/g++-4.2
       : <striper>
       : <architecture>arm <target-os>iphone <macosx-version>iphone-3.0
       ;

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

    You have to adjust the paths if you have installed the iPhone SDK at a different location.

  4. Now open the file tools/build/v2/tools/darwin.jam and add the missing line

    iphone-3.1.3 iphonesim-3.1.3

  5. To build the Boost libraries for the iPhone Device, do the following:

    bjam --prefix=/path/for/BoostiPhoneDeviceLibs --layout=versioned toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-3.0 define=_LITTLE_ENDIAN link=static install

    Replace /path/for/BoostiPhoneDeviceLibs with the path were you want to have the final Boost libraries and headers located.

  6. To build the Boost libraries for the Simulator, do the following:

    bjam --prefix=/path/for/BoostiPhoneSimulatorLibs --layout=versioned toolset=darwin architecture=x86 target-os=iphone macosx-version=iphonesim-3.0 link=static install

    Replace /path/for/BoostiPhoneSimulatorLibs with the path were you want to have the final Boost libraries and headers located.

Unfortunately there are a few errors and warnings when building Boost for the iPhone this way. But because we currently do not use the affected classes yet, we did not spend much time into that.

Tags: , , , , , , , , , , , , ,

14 Responses to “Building Boost for the iPhone”

  1. me says:

    Thanks for posting! I was just looking into a similar deployment problem.

  2. jan says:

    Great manual, thanks for sharing.
    But now i have the problem, that i can only include the simulator libs _or_ the ARM libs to link against my app. So i can’t easily switch between the emulator and the real hardware.

    How did you solve that problem? I could a custom build step, that changes a link to the directory with the libs, but thats not the greatest solution. I could also create two targets in XCode, but i would have to keep both targets in sync and i would have to change the target and the SDK whenever i want to switch between emulator and hardware. Any better solutions?

    greetings
    jan

    • mani says:

      You can solve that with appropriate Build Configurations. I created an Xcode project with two Targets: one for the desktop app and one for the iPhone app (that’s funky, yes :-)) and I created the Build Configurations “Debug”, “Release”, “DeviceDebug”, “DeviceRelease”, “DeviceAdHoc” and “DeviceDistribution”. The “Debug” and “Release” Build Configurations are used by the desktop app Target and for the iPhone app Target when it’s launched in the Simulator. The “Device…” Build Configurations are used for the iPhone app Target for the Device. I think I’ll write a blog article about all these advanced Xcode configuration techniques in the near future …

  3. malick says:

    Or you can from CMake 2.6.4, generate a XCode project for boost, then set it to use the iPhone sdk library.

  4. terryc says:

    when i run the build command, i get error as following, so how to deal with it ?
    thanks!

    common.copy /path/for/BoostiPhoneDeviceLibs/include/boost-1_37/boost/serialization/weak_ptr.hpp
    common.copy /path/for/BoostiPhoneDeviceLibs/include/boost-1_37/boost/serialization/wrapper.hpp
    MkDir1-quick-fix-for-unix /path/for/BoostiPhoneDeviceLibs/include/boost-1_37/boost/signals
    common.copy /path/for/BoostiPhoneDeviceLibs/include/boost-1_37/boost/signals/connection.hpp
    darwin.compile.c++ bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/acoshf.o
    /bin/sh: line 1: -ftemplate-depth-128: command not found

    -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall -no-cpp-precomp -gdwarf-2 -arch arm -pthread -DBOOST_ALL_NO_LIB=1 -DNDEBUG -D_LITTLE_ENDIAN -I”.” -c -o “bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/acoshf.o” “libs/math/build/../src/tr1/acoshf.cpp”

    …failed darwin.compile.c++ bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/acoshf.o…
    darwin.compile.c++ bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/asinhf.o
    /bin/sh: line 1: -ftemplate-depth-128: command not found

    -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall -no-cpp-precomp -gdwarf-2 -arch arm -pthread -DBOOST_ALL_NO_LIB=1 -DNDEBUG -D_LITTLE_ENDIAN -I”.” -c -o “bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/asinhf.o” “libs/math/build/../src/tr1/asinhf.cpp”

    …failed darwin.compile.c++ bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/asinhf.o…
    darwin.compile.c++ bin.v2/libs/math/build/darwin-4.0.1~iphone/release/architecture-arm/link-static/macosx-version-iphone-2.1/target-os-iphone/threading-multi/atanhf.o
    /bin/sh: line 1: -ftemplate-depth-128: command not found

  5. […] SDK (yes, you can create iPhone apps with just C or C++), or other alternatives like nui, POCO, Boost or haXe. And apparently, soon you’ll be able to use a Symbian C++ toolkit as well (and who […]

  6. Hey, i’ve been reading this blog for a while and have a question, maybe you can help… it’s how do i add your feed to my rss reader as i want to follow you. Thanks.

  7. I am starting my own blog too, your is really nice!

  8. Steven Jones says:

    Hi, looks like the user-config.jam is no longer needed for boost_1_42_0. Ive managed to compile for the simulator using your command line as a base, but unfortunately I cannot build for the phone successfully. The error I am getting is “error trying to exec ‘/usr/bin/arm-apple-darwin10-g++-4.2.1’: execvp: No such file or directory”. The latest version I have is arm-apple-darwin9-g++-4.2.1, any ideas how to point boost at that?

    • mani says:

      I’ve updated the article so it builds boost 1.42.0. I think you still need to setup your user-config.jam.

      • Steven Jones says:

        Thanks for the fast update, thats great. I havent had time to tweak my user-config.jam just yet but I will report back on how it goes.

  9. Mobius Strip says:

    First of all, thanks a lot for posting this.

    I was able to build for the simulator using only step #6, without doing any of the others.

    Building for the device with the command line in #5 (changing the OS to 3.2) was a major failure. First, it looks for the compiler in /usr/bin, when in fact the default installation location for it is /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin. I set up a symbolic link in /usr/bin for it.

    After that, there were failures galore complaining about the nonexistence of

    bits/c++config.h and others
    arm/_types.h and others

    and indeed I couldn’t find those files anywhere (then again, Spotlight sucks ass). I tried creating an iPhone library Xcode project and building the date_time lib, but this fails because of a reference to stnprintf, which isn’t defined in std in the iPhone SDK. I guess I’ll try CMake next.

  10. Hello, We searched for this blog on Bing and just wanted to state just superb read. I would have to agree with this, thanks again!

  11. Zada Drouse says:

    Super toller Post, sehr interessantes Thema – Freue mich auf mehr solche Artikel. Ach übrigens, oben rechts wird wohl das Banner nicht richtig angezeigt. Grüße

Leave a Reply