Wednesday 18 April 2012

Using QuantLib on the iPad: Part II - Build QuantLib


I have found most discussions about building QuantLib for the iPhone or iPad recommending to include the source code directly in an Xcode project and building it as part of the application.

Whilst this enables development to start quickly, it is restrictive in that you need to do this for every project that uses QuantLib and it makes it more difficult to upgrade versions. Building the QuantLib library as a framework enables better reuse and different versions can be built and swapped in and out of projects more quickly. Also, using the build system supplied with QuantLib (configure and autogen) guarantees that the libraries will be built correctly and include all the relevant dependancies.

The steps to build the framework to build the static libraries for each architecture, merge them and build the framework.

To build the static libraries:

  • "configure" the system to build the Armv6 libraries
  • "make" the Armv6 library using the Xcode arm-apple-darwin10-llvm-g++-4.2 compiler
  • "configure" the system to build the Armv7 libraries
  • "make" the Armv7 library using the Xcode arm-apple-darwin10-llvm-g++-4.2 compiler
  • "configure" the system to build the i386 libraries
  • "make" the i386 library using the default Xcode compiler

Between each build a "make clean" is performed to clear down the previously built objects (this ensures that we don't have any problem with dependancies in the next build).

Once the libraries are built, they are merged together into a fat (multi-architecture) library containing the Arm and i386 libraries in using lipo.

The framework directory structure and manifest is then created and the fat library and header files are copied.

To simplify this, I created a script that is available on github under the quantlib-on-iOS project.

To use it:

  • Download the QuantLib source code and unpack it
  • Copy the script buildql.sh into the QuantLib source code root directory
  • Configure the boost directory variable in the script
  • Configure the number of concurrent jobs to run in the script (usually 1+number of cores)
  • Run ./buildql.sh

At this point it may take between 30 minutes to over 1 hour to build, depending on the performance of your machine as QuantLib is built 3 times to create each architecture.

The QuantLib framework is created in the "framework" subdirectory which can then be included in iOS applications link libraries.



The previously built Boost framework also needs to be included as it is a QuantLib dependancy.

I have tested the framework on both the simulator and on an iPad and the application runs fine. I have not yet validated it against an App Store submission.

This is an example of a simple bond calculation being called from the QuantLib framework which I will go into more detail in the next post.


Sunday 15 April 2012

Using QuantLib on the iPad: Part I - Build Boost


Using QuantLib on iOS is done in three steps:

  • Build Boost for iOS
  • Build QuantLib for iOS
  • Interface QuantLib (written in C++) to Objective-C

So, to use QuantLib I first need to build the boost libraries. Fortunately there are a lot of blog posts around which describe what needs to be done to get this working on under iOS - I need to build it for the iOS simulator under i386 and on the iPad/iPhone under the arm chipset.

The best way to do this is to bundle the Boost libraries and header files in single package via a frameworkPete Goodliffe has an excellent guide to on how to do this and has provided the scripts on gitorious. However, the current version of Boost does not work correctly with Objective-C. Fortunately, Arne has provided a fix and has updated the scripts to work using the latest version of xcode (4.3) as Apple have completely changed where all the development executables live to /Application/Xcode.app/Contents/Developer. This version of the script is available on github.

It's not hard to obtain and build the Boost libraries using the script. This creates a framework (library) that can be included in an IOS application that makes development very simple.

Once built, the framework is then included in the build, as below.


There is no need to setup include paths and link libraries as the framework takes care of all this.

One thing to note is that the script builds the basic Boost library. If you want to build anything else, such as the boost unit test framework (UTF), you'll need to alter the script. It's not a configuration change in the script and requires some extra bjam commands to be run and the object files linked into the library. I'll do this at some point as QuantLib uses the UTF for it's unit testing, but at the moment it is not a priority for me.

Saturday 14 April 2012

First Post

I created an option strategy risk calculator application last year on the iPad for several reasons. Partly to brush up on my programming skills which had been on the back-burner of late I also wanted to expand my skills and apply the knowledge I'd accumulated at LIFFE/NYSE. Having risen through the ranks as an analyst, developer, then architect, I am now hands off and manage software development teams for a living, so I do this in my own time.

I am currently working on a second application and this time I wanted to use it to expand my knowledge of finance. As part of this I am working on using the QuantLib quantitative finance library on the iPad.