[Numpy-discussion] Numpy and C++ integration...

Damian Eads eads@soe.ucsc....
Tue Feb 5 12:57:37 CST 2008

Dear Vince,

You probably have heard better solutions but I think what I do works and 
is simple to learn. When I need to call C++ code from Python, I write a 
wrapper extern "C" function that calls the C++ function that returns the 
result. Then I just use ctypes to call the extern "C" function from Python.

extern "C" {
     double *get_result(double *input, int n) {
         return CPlusPlusFunction::GetResult(input, n);

import ctypes
mylib = ctypes.CDLL('libmylib')

def get_result(A):
    return mylib.get_result(input.ctypes.data)

I hope this helps.


> Vince Fulco <vfulco1 <at> gmail.com> writes:
> Dear Numpy Experts-  I find myself working with Numpy arrays and
> wanting to access *simple* C++ functions for time series returning the
> results to Numpy.  As I am a relatively new user of Python/Numpy, the
> number of paths to use in incorporating C++ code into one's scripts is
> daunting.  I've attempted the Weave app but can not get past the
> examples.  I've also looked at all the other choices out there such as
> Boost, SIP, PyInline, etc.  Any trailheads for the simplest approach
> (assuming a very minimal understanding of C++) would be much
> appreciated.  At this point, I can't release the code however for
> review.  Thank you.

More information about the Numpy-discussion mailing list