Boost.Python & Xcode

By | October 15, 2013

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.

One thought on “Boost.Python & Xcode

  1. vp

    Hey, I was wondering how you used Boost.Python (i.e. extending or embedding?). I’m attempting to extend and see a libboost_python.dylib in my debug/ folder. Is this the .dylib file you are referring to (MyLib = libboost_python)?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *