What would be the quickest way to construct a python binding to a C or C++ library?
(using windows if this matters)
What would be the quickest way to construct a python binding to a C or C++ library?
(using windows if this matters)
This paper, claiming python to be all a scientist needs, basically says: first prototype everything in Python. Then when you need to speed a part up, use SWIG and translate this part to C.
One of the official python documents (see link) contains details on extending python using c/c++. Even without the use of SWIG it's quite straightforward and works perfectly well on Windows.
You should have a look at Boost.Python, here is the short introdution taken from their website: """ The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it, making Boost.Python ideal for exposing 3rd-party libraries to Python. The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative interface definition language (IDL). """
I like ctypes a lot, swig always tended to give me problems. Also ctypes has the advantage that you don't need to satisfy any compile time dependency on python, and your binding will work on any python that has ctypes, not just the one it was compiled against.
Suppose you have a simple C++ example class you want to talk to:
#include <iostream>
class Foo{
public:
void bar(){
std::cout << "Hello" << std::endl;
}
};
Since ctypes can only talk to C functions, you need to provide those declaring them as extern "C"
extern "C" {
Foo* Foo_new(){ return new Foo(); }
void Foo_bar(Foo* foo){ foo->bar(); }
}
Next you have to compile this to a shared library
g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o
And finally you have to write your python wrapper
from ctypes import cdll
lib = cdll.LoadLibrary('./libfoo.so')
class Foo(object):
def __init__(self):
self.obj = lib.Foo_new()
def bar(self):
lib.Foo_bar(self.obj)
Once you have that you can use it like
f = Foo()
f.bar() #and you will see "Hello" on the screen
You might find the suggestions in this similar question also helpful -> Prototyping hybrid Python code