Calling C/C++ from Python? [closed] Calling C/C++ from Python? [closed] python python

Calling C/C++ from Python? [closed]

ctypes module is part of the standard library, and therefore is more stable and widely available than swig, which always tended to give me problems.

With ctypes, you 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 in a file called foo.cpp:

#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.og++ -shared -Wl,-soname, -o  foo.o

And finally you have to write your python wrapper (e.g. in

from ctypes import cdlllib = cdll.LoadLibrary('./')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() #and you will see "Hello" on the screen

You should have a look at Boost.Python. Here is the short introduction 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).

The quickest way to do this is using SWIG.

Example from SWIG tutorial:

/* File : example.c */int fact(int n) {    if (n <= 1) return 1;    else return n*fact(n-1);}

Interface file:

/* example.i */%module example%{/* Put header files here or function declarations like below */extern int fact(int n);%}extern int fact(int n);

Building a Python module on Unix:

swig -python example.igcc -fPIC -c example.c example_wrap.c -I/usr/local/include/python2.7gcc -shared example.o example_wrap.o -o


>>> import example>>> example.fact(5)120

Note that you have to have python-dev. Also in some systems python header files will be in /usr/include/python2.7 based on the way you have installed it.

From the tutorial:

SWIG is a fairly complete C++ compiler with support for nearly every language feature. This includes preprocessing, pointers, classes, inheritance, and even C++ templates. SWIG can also be used to package structures and classes into proxy classes in the target language — exposing the underlying functionality in a very natural manner.