Skip to content

New Python/C++ interface #2750

Closed
Closed
@wangkuiyi

Description

@wangkuiyi

I read and followed this article http://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html, which compares the following interfacing technology:

  1. manual wrapping. I followed this official Python document for more details: https://docs.python.org/2/extending/extending.html for some example programs. There includes some complex boilerplate code -- parsing argument in each C function, build and return Python object in each C function, and the method list.

  2. SWIG. It seems a general method that can generate bindings for various client languages, but not "native" enough. Also, it takes some time to learn the interfacing language (*.i files).

  3. ctypes. This requires us to respecify the return type and other meta-data about each C function at the Python side, again.

  4. SIP. This is the Qt community's version of SWIG. We also need to learn an interfacing language.

  5. Boost.Python. This is some C++ templates that simplify the manual wrapping. We no longer need to write a C wrapper function for each C++ function. Only a few extra lines in addition to the original C++ code are required to build a .so that can be called from Python.

I personally prefer Boost.Python. Here is an example for your reference:

Suppose that we already have C++ functions like:

char const* greet() {
   return "hello, world";
}

only the following few lines is required to build the Python-callable .so file:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext) {
  boost::python::def("greet", greet);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions