Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interfacing Python with C/C++ is not an easy task.</p> <p>Here I copy/paste a <a href="https://stackoverflow.com/questions/456884/extending-python-to-swig-or-not-to-swig/456949#456949">previous answer</a> on a previous question for the different methods to write a python extension. Featuring Boost.Python, SWIG, Pybindgen...</p> <ul> <li><p>You can write an extension yourself in C or C++ with the <a href="http://docs.python.org/extending/index.html" rel="noreferrer">Python C-API</a>.</p> <p>In a word: don't do that except for learning how to do it. It's very difficult to do it correctly. You will have to increment and decrement references by hand and write a lot of code just to expose one function, with very few benefits.</p></li> <li><p><a href="http://www.swig.org/" rel="noreferrer">Swig</a>:</p> <p>pro: you can generate bindings for many scripting languages.</p> <p>cons: I don't like the way the parser works. I don't know if they've made some progress but two years ago the C++ parser was quite limited. Most of the time I had to copy/paste my .h headers to add some <code>%</code> characters and to give extra hints to the swig parser.</p> <p>I also needed to deal with the Python C-API from time to time for (not so) complicated type conversions. </p> <p>I'm not using it anymore. </p></li> <li><p><a href="http://www.boost.org/doc/libs/1_39_0/libs/python/doc/index.html" rel="noreferrer">Boost.Python</a>:</p> <p>pro: It's a very complete library. It allows you to do almost everything that is possible with the C-API, but in C++. I never had to write a C-API code with this library. I also never encountered a bug due to the library. Code for bindings either works like a charm or refuses to compile.</p> <p>It's probably one of the best solutions currently available if you already have some C++ library to bind. But if you only have a small C function to rewrite, I would probably try with Cython.</p> <p>cons: if you don't have a precompiled Boost.Python library you're going to use Bjam (sort of a replacement of make). I really hate Bjam and its syntax.</p> <p>Python libraries created with B.P tend to become obese. It also takes a <strong>lot</strong> of time to compile them.</p></li> <li><p><a href="http://sourceforge.net/projects/pygccxml/?source=dlp" rel="noreferrer">Py++</a>: it's Boost.Python made easy. Py++ uses a C++ parser to read your code and then generates Boost.Python code automatically. You also have a great support from its author (no it's not me ;-) ).</p> <p>cons: only the problems due to Boost.Python itself.</p> <p><strong>Edit</strong> this project looks discontinued. While probably still working it may be better to consider switching.</p></li> <li><p><a href="http://code.google.com/p/pybindgen/" rel="noreferrer">Pybindgen</a>:</p> <p>It generates the code dealing with the C-API. You can either describe functions and classes in a Python file, or let Pybindgen read your headers and generate bindings automatically (for this it uses pygccxml, a python library wrote by the author of Py++).</p> <p>cons: it's a young project, with a smaller team than Boost.Python. There are still some limitations: you cannot expose your own C++ exceptions, you cannot use multiple inheritance for your C++ classes. </p> <p>Anyway it's worth trying!</p></li> <li><p>Pyrex and <a href="http://www.cython.org/" rel="noreferrer">Cython</a>:</p> <p>Here you don't write real C/C++ but a mix between Python and C. This intermediate code will generate a regular Python module. </p></li> </ul> <p><strong>Edit Jul 22 2013:</strong> Now Py++ looks discontinued, I'm now looking for a good alternative. I'm currently experimenting with Cython for my C++ library. This language is a mix between Python and C. Within a Cython function you can use either Python or C/C++ entities (functions, variables, objects, ...). </p> <p>Cython is quite easy to learn, has very good performance, and you can even avoid C/C++ completely if you don't have to interface legacy C++ libraries.</p> <p>However for C++ it comes with some problems. It is less "automagic" than Py++ was, so it's probably better for stable C++ API (which is now the case of my library). The biggest problem I see with Cython is with C++ polymorphism. With Py++/boost:python I was able to define a virtual method in C++, override it in Python, and have the Python version called within C++. With Cython it's still doable but you need to explicitly use the C-Python API.</p> <p><strong>Edit 2017-10-06:</strong></p> <p>There is a new one, <a href="https://github.com/pybind/pybind11" rel="noreferrer">pybind11</a>, similar to Boost.Python but with some potential advantages. For example it uses C++11 language features to make it simpler to create new bindings. Also it is a header-only library, so there is nothing to compile before using it, and no library to link. </p> <p>I played with it a little bit and it was indeed quite simple and pleasant to use. My only fear is that like Boot.Python it could lead to long compilation time and large libraries. I haven't done any benchmark yet.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload