Calling C++ Modules in Python

Dan Stromberg drsalists at gmail.com
Fri Mar 11 16:59:03 EST 2011


On Fri, Mar 11, 2011 at 1:15 PM, Patrick <zxpatric at gmail.com> wrote:

> Hi,
>
> I saw in the Beginner document that "•Is easily extended by adding new
> modules implemented in a compiled language such as C or C++. ".
>
> While to my investigation, it seems not that easy or did I miss
> something?
>
> boost python (C++ libraries need to be re-compiled with written
> wrappers again?).
> SWIG  (It works by taking the declarations found in C/C++ header and
> using them to generate the wrapper code that scripting languages need
> to access the underlying C/C++ code).
>

I guess it should be asked: "easy for who"?  "easy" is always relative to
some context.

If you're planning to stay with CPython and C++ forevermore, then Cython is
a nice way of gluing the two.   Cython looks like Python code, but it gives
you access to C and C++ code and data as well as CPython code and data.

If you're planning to support your C++ code with more languages than
CPython, you might be better off with SWIG.

If you really do want to do the task the old way, you're probably best off
copying some preexisting module with a maximize-the-developers-rights
license (like Apache, MIT or 3 clause BSD), and modifying that.

If you want to be able to move easily to pypy, I might suggest coming up
with a C wrapper for your C++ code, and then accessing the C code using
ctypes.  That should work in CPython and pypy, and it looks like jython
might support this soon as well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110311/9758a2b7/attachment.html>


More information about the Python-list mailing list