code base 5000
code base 5000

Reputation: 4112

pybind11 wrapping existing code

I am trying to wrap a c++ library using pybind11 so I can use it with Python 3.x.

I tried wrapping the code using swig, but I ran into an issue where SWIG would generate the cxx file, but would not read the headers I was referencing, so it was suggested that I use pybind11 because it's better than swig (this is opinion I know), but I am having trouble finding resources on how I can reference/build the project.

My environment is:

When I create my interface file for Swig, I can do something easy like:

```

%module filegdbapi

%{
#include "FileGDBAPI.h"
%}

%include "FileGDBAPI.h"

```

Then on the swig build, I can reference the -I to the location of the .h files.

How do I do something like this in pybind11? Is it that simple?

The documentation for pybind11 always shows building wrappers when you have the .cpp files. Can I use pybind11 in a ways that I can build a wrapper with swig? If so, how do you setup the files?

Can someone point me to a project that just generates a python wrapper from existing c++ code?

Thank you

Upvotes: 9

Views: 2470

Answers (3)

kostyfisik
kostyfisik

Reputation: 179

How do I do something like this in pybind11? Is it that simple?

Can someone point me to a project that just generates a python wrapper from existing c++ code?

You can check Binder project http://cppbinder.readthedocs.io

Binder is a tool for automatic generation of Python bindings for C++11 projects using Pybind11 and Clang LibTooling libraries. That is, Binder, takes a C++ project and compiles it into objects and functions that are all usable within Python. Binder is different from prior tools in that it handles special features new in C++11.

The basic usage seems to be very easy, similar to your description for SWIG

1) Gather data about what classes/functions are available and acquire in-depth information of class heritage, member functions and standalone functions type signatures.

2) Generate bindings code

3) Compile code into shared library

Binder is tool that aims to automate steps 1 and 2.

The sad news is that it seems to be Linux only, so to use it under Windows you will need to install virtual Linux or use a Docker container with build tools.

Upvotes: 1

norok2
norok2

Reputation: 26956

You may want to look into cffi for this.

http://cffi.readthedocs.io/en/latest/

and this for a sample project using it:

https://github.com/wolever/python-cffi-example

Incidentally, you will be able to ship the code with pypy too, which some people may consider as a plus.

Upvotes: 0

Roman Miroshnychenko
Roman Miroshnychenko

Reputation: 1564

Despite serving the same purpose, SWIG and Pybind11 are different tools.

As the name implies, SWIG (Simplified Wrapper and Interface Generator) is a generator tool that create Python binding for existing C++ code, using definitions written in a special language.

Pybind11, on the other hand, is a header-only C++ library that wraps raw Python-C API (that is old-style C and has steep learning curve) and allows to write Python bindings in modern C++. But you write those binding yourself by hand, using whatever C++ entities (functions, classes, templates etc.) that pybind11:: namespace provides.

Upvotes: 5

Related Questions