带有Boost Python的Wrap C 功能 - Numpy数组类型
Wrap C++ function with Boost Python -- numpy array types
从我收集的东西中,做到这一点的好方法是使用boot.python库,就像这个简单的示例一样;请不要推荐诸如Cython之类的替代方案作为解决方案。但是,当我尝试使用boost::python
数据类型时,我的CPP文件将不会构建。
example_boost.cpp:
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <iostream>
namespace bpy = boost::python;
namespace bnp = boost::python::numpy;
void do_stuff(const bnp::ndarray& input_array) {
...
};
/*
* This is a macro Boost.Python provides to signify a Python extension module. This enables me to import example_boost.cpp and call do_stuff() within a Python file.
*/
BOOST_PYTHON_MODULE(crf) {
// Expose the functions
boost::python::def("compute_factor_out_msgs", compute_factor_out_msgs);
}
运行make
...
Undefined symbols for architecture x86_64:
"boost::python::converter::object_manager_traits<boost::python::numpy::ndarray>::get_pytype()", referenced from:
boost::python::detail::caller_arity<1u>::impl<OutMessages (*)(boost::python::numpy::ndarray const&), boost::python::default_call_policies, boost::mpl::vector2<OutMessages, boost::python::numpy::ndarray const&> >::operator()(_object*, _object*) in example_boost.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example_boost.so] Error 1
注意:
-
make
如果我不使用bpy
和bnp
类型,则成功,所以我的makefile是正确的。 - 我已经安装了通过Homebrew安装的Boost V 1.63.0,在Mac OSX El Capitan上
- 使用C 11和Python 2.7
已发布示例代码的两个问题:
-
要解决制造问题,makefile也必须链接
-lboost_numpy
。 -
即使它会编译,结果也将是SEG故障(堆栈溢出),因为我们需要首先使用
初始化py_initialize();BNP :: initialize();
如下所述。
相关文章:
- C++返回 Numpy 数组的 Python 扩展模块
- 在 Cython 中将C++向量转换为 numpy 数组,而无需复制
- 如何通过 cython 将 numpy 数组列表传递给 C++
- Pybind11 默认参数 numpy 数组或 None
- Pybind11:在C++中使用Pybind11转换numpy数组的问题
- 使用SWIG将numpy数组元素(int)传递给c++int
- 从 numpy 数组获取指针以将图像发送到C++
- 从结构的 C 数组到带有 ctypes 的 NumPy 数组的高效转换
- 使用 Boost.Python 将 numpy 数组传递给C++
- 如何使用pybind传递numpy数组列表
- 通过 Pybind11 返回 numpy 数组
- 如何在C 中创建类似于Python的Numpy数组的数组
- Xtensor将numpy数组传递到XT :: XTENSOR参数类型
- Cython:从参考获得时缺少两个前元素的 Numpy 数组
- 将 numpy 数组公开为C++向量
- 带有Boost Python的Wrap C 功能 - Numpy数组类型
- 如何向armadillo(C++)发送一个numpy数组,并从armadillo返回一个numdy数组
- 如何将 numpy 数组正确传递给 Cython 函数
- 从NumPy数组到Mat(OpenCV)的C++转换
- 如何使用指针将 numpy 数组传递给 C++/SWIG,而无需依赖 numpy.i