1

I am trying to import a C++ function in Python. I know there are already some post about this. However, I haven't found anything about functions which take vectors as arguments. My C++ function is something like this:

double many_body_pot(
    std::vector< std::vector<double> > &par,
    std::vector< std::vector<double> > &geometry,
    double x, double y, double z
)
{
    // ...
}

As you can see it takes 2 vectors and 3 doubles as arguments, and returns a double as result. I have also read this post about extending Python with C++.

But I am still completely lost, the only thing I got clear is that I have to include in my C++ code:

#include <Python.h>

So I want to import this function in Python, and use lists or arrays as arguments for the C++ vectors. How can this be done?

rocambille
  • 15,398
  • 12
  • 50
  • 68
Kilian A.G.
  • 123
  • 1
  • 6
  • Maybe [Boost Python](http://www.boost.org/doc/libs/1_62_0/libs/python/doc/html/index.html) might be helpful? – Some programmer dude Oct 24 '16 at 14:20
  • 1
    might be related: http://stackoverflow.com/questions/16885344/how-to-handle-c-return-type-stdvectorint-in-python-ctypes http://stackoverflow.com/questions/16693178/c-vector-to-python-3-3?noredirect=1&lq=1 http://stackoverflow.com/questions/2923272/how-to-convert-vector-to-array-c?noredirect=1&lq=1 – dnalow Oct 24 '16 at 14:23
  • For starters, you need to create a module and put your function in it. It will also need a C (not C++) API. This is often do by writing a wrapper function in C that calls the C++ function. Doing this will let you import the function from the module and call it from Python. – martineau Oct 24 '16 at 14:50
  • I have something similar with python lists and/or dicts being passed to a boost::python C api function which constructs the std::vector or whatever is needed, calls the actual c++ function, and returns the result. – Kenny Ostrom Oct 24 '16 at 16:33

0 Answers0