3

I have a C++ library that I need to be able to interface with python. I read this question to understand the choice I need to adapt.

I saw SWIG and Cython and wanted to go with SWIG, mainly because my python programming experience is very minimal. However, I realise that with Swig I have to write an interface (.i extensions) for every class. Now, my C++ project is huge and I feel it will take me a lot of time to get the wrappers in place (or maybe I am wrong).

So right now since my application is large I need to make a choice. In the quoted thread I came across Boost Python. Now I can no longer decide and want input from people who can tell me the pros and cons of one over the other. Note my preference is on easy of use and how quickly can it be done. I am willing to compromise system performance for this. I would appreciate immensely if someone could provide me a SWIG implemented project or Boost Python implemented project link (a complete module instead of a sample tutorial would be much better !)

Community
  • 1
  • 1
rockstar
  • 3,512
  • 6
  • 40
  • 63
  • This should be everything you need for installing Boost and verifying that everything works: http://www.boost.org/doc/libs/1_39_0/libs/python/doc/building.html – paulsm4 Dec 20 '12 at 04:00
  • Also, this wiki page is full of some really good tips that boost tutorial misses http://wiki.python.org/moin/boost.python?action=show&redirect=BoostPython – Aleksey Vitebskiy Dec 20 '12 at 04:29

1 Answers1

4

Boost::python provides a nearly wrapper-less interface between C++ and Python. It also allows you to write custom converters and other neat things that make the Python interfaces much nicer. The interfaces are pure C++, but they rely on templates and clever design patterns to make it look all nice and declarative. You also get the benefit of your connector code being checked by the compiler directly.

With Swig, you write interface declarations in Swig's own DSL, which takes a few days to get a hang of. In addition, it always inserts a wrapper layer, so it could be a bit slower. However, it does have the nice feature of automatically converting many things for ya without having to declare anything extra. The wrappers it generates are quite hard to debug though.

IMHO boost::python is the better choice, because you're working pretty directly with CPython's native C interfaces. I use Swig for Java and C++ interaction, because JNI is a bear, Python's C interface is actually quite usable all by itself.

If you already have a bunch of Swig wrappers, I would keep those because you'd have to redo all that work. However, starting a new project, or if you require maximum performance, boost::python all the way!

Aleksey Vitebskiy
  • 2,087
  • 1
  • 15
  • 14
  • thanks . ! so i deep dive into boost::python to get a prototype running . Thanks for the link to the documentation as well . – rockstar Dec 20 '12 at 11:15
  • try pybind11 which allows automatic conversion for many types.( and doesn't use the whole boost library) – Harindu Dilshan Jan 02 '18 at 23:32