Wednesday 10 May 2017

Python interface to C/C++

Calling C/C++ from python

 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.

#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 (e.g. in fooWrapper.py)
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 call it like
f = Foo() 
f.bar() #and you will see "Hello" on the screen
 

No comments:

Post a Comment