Monthly Archives: October 2013

Boost.Python & Xcode

I’ve been playing around with boost.python lately, trying to integrate it into my projects in Xcode5. Integration itself wasn’t too difficult. It really just came down to:

  1. Install python (I used Homebrew to do this, though the standard OS X python installation should work just fine.)
  2. Install boost (again, Homebrew), being careful to compile against libc++ using brew install boost --with-c++11 .  Hey, I like the C++11 features.
  3. Set up the search paths and library paths in Xcode.  My project is a dynamic library written in C++.
  4. Build, run, and profit.

The tricky part was how to set up the library so that python would automatically find the .dylib file and be able to import it.  The Homebrew python install provides a central location to put additional import modules at /usr/local/lib/pythonX.Y/site-packages, where X and Y are the python version and sub-version numbers.  But, for some reason, python would not recognize the my project’s output .dylib file.  After some struggles, I found out that the python was able to import my project’s dynamic library if the library file had a .so suffix.  So, I ended up creating a Run Script phase in Xcode like the one below:

The reason for the multiple symbolic links is because I had multiple BOOST_PYTHON_MODULE(SubpackageX) within my library in the hopes of being able to organize the resulting python library a little better. The Xcode project ends up with one .dylib file, but it turns out that it still contains all the information needed for the various sub-packages, so all I had to do was to create various symbolic links to simulate a python package structure. I’m not sure if this is the proper or the optimal way of doing this though… but it works for me for the time being.

Setting up boost in Xcode

Here’s how I have set up boost in Xcode 5:

  1. Build and install boost
  2. Set the search paths in Xcode
  3. Add the .dylib file to the project
  4. Make sure the project is linking with the .dylib file
  5. Create a copy phase to copy the .dylib to the appropriate place


1. Build and Install Boost

You can more or less follow the instructions on boost website. I chose to compile with clang and libc++ because, well, let’s face it, c++11 is kinda cool.

Also, note that I’m building the debug libraries, as this library will be used mostly for development. For release mode, you can change the variant to “release.”

2. Set the search paths in Xcode

Let’s assume that you have already set up an Xcode project.
To add boost headers and dylib files to your project’s search path, go to your project build settings:
And add /path/to/boost/include to “Header Search Paths” and /path/to/boost/lib to “Library Search Paths.”

2. Add the .dylib file to the project

Add the dynamic (or static) lib file to the Xcode project.
Here, I’ve added the boost serialization library.

4. Link with the new .dylib

Make sure that you are actually linking to the boost library that you just added.
Note, this step may have happened automatically when you added the .dylib file to the project.

5. Create a copy phase

Add a new copy files build phase, and make sure that the boost .dylib files are copied to the appropriate locations.

That’s about it!