Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Windows 10 x64
  • Anacondas build 4.4.0 with Python 3.6
  • Visual Studios 2015 Professional (c++ installed)

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

like image 825
code base 5000 Avatar asked Sep 07 '17 10:09

code base 5000


2 Answers

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.

like image 177
Roman Miroshnychenko Avatar answered Nov 10 '22 15:11

Roman Miroshnychenko


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.

like image 30
kostyfisik Avatar answered Nov 10 '22 13:11

kostyfisik