带有Boost Python的Wrap C 功能 - Numpy数组类型



从我收集的东西中,做到这一点的好方法是使用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如果我不使用 bpybnp类型,则成功,所以我的makefile是正确的。
  • 我已经安装了通过Homebrew安装的Boost V 1.63.0,在Mac OSX El Capitan上
  • 使用C 11和Python 2.7

已发布示例代码的两个问题:

  1. 要解决制造问题,makefile也必须链接 -lboost_numpy

  2. 即使它会编译,结果也将是SEG故障(堆栈溢出),因为我们需要首先使用

    初始化

    py_initialize();BNP :: initialize();

如下所述。

最新更新