[英]Pass numpy array to C++ using Boost.Python
I am trying to pass a numpy array to C++ using Boost.Python. 我正在尝试使用Boost.Python将numpy数组传递给C ++。
The C++ code is: C ++代码是:
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
void f(boost::python::numpy::ndarray& x){}
BOOST_PYTHON_MODULE(libtest)
{
boost::python::def("f", f);
}
The Python code is: Python代码是:
import libtest
import numpy
x=numpy.array(range(3))
libtest.f(x)
This gives a segmentation fault. 这给出了分段错误。 This happens when passing the variable by value and by reference.
当通过值和引用传递变量时,会发生这种情况。
I have found a way to do what I needed. 我找到了一种做我所需要的方法。 However the purpose of using Boost.Python was to be able to simply call the functions from the module without having to write a wrapper on the Python side as is the case with ctypes where certain types or return values have to be dealt with.
但是,使用Boost.Python的目的是能够简单地从模块中调用函数,而不必像必须处理某些类型或返回值的ctypes那样在Python端编写包装器。
Is is possible to simply pass a reference to a numpy array? 是否可以简单地将引用传递给numpy数组?
Thanks! 谢谢!
I had the same problem and apparently solved it by putting 我有同样的问题,显然可以解决
boost::python::numpy::initialize();
at top of my BOOST_PYHON_MODULE definition. 在我的BOOST_PYHON_MODULE定义的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.