【问题标题】:Problems compiling Boost::Python编译 Boost::Python 的问题
【发布时间】:2013-10-24 22:40:13
【问题描述】:

我想在我的 python 脚本上使用 c++ 数值配方,但是我在编译 Boost Python 库中的一些东西时遇到了一些问题。具体来说,我想将变形虫函数公开给 python。我使用 Make 而不是 BJam。这是我尝试编译时得到的结果:

costantinoagnesi@costantino-HP-Pavilion-dv5-Notebook-PC:~/Desktop/Boost Python Test$  make
g++ -I/usr/include/python2.7 -I/usr/include -fPIC -c amoeba_py.C
In file included from /usr/local/include/boost/python/object/make_instance.hpp:10:0,
               from /usr/local/include/boost/python/object/make_ptr_instance.hpp:8,
               from /usr/local/include/boost/python/to_python_indirect.hpp:11,
               from /usr/local/include/boost/python/converter/arg_to_python.hpp:10,
               from /usr/local/include/boost/python/call.hpp:15,
               from /usr/local/include/boost/python/object_core.hpp:14,
               from /usr/local/include/boost/python/args.hpp:25,
               from /usr/local/include/boost/python.hpp:11,
               from amoeba_py.C:73:
/usr/local/include/boost/python/converter/registered.hpp: In function ‘const boost::python::converter::registration&
boost::python::converter::detail::registry_lookup2(T& (*)()) [with T = double(const NRVec<double>&)]’:
/usr/local/include/boost/python/converter/registered.hpp:94:40:   instantiated from ‘const >boost::python::converter::registration& boost::python::converter::detail::registry_lookup1(boost::type<T>) [with T = double (&)(const NRVec<double>&)]’
/usr/local/include/boost/python/converter/registered.hpp:105:23:   instantiated from const boost::python::converter::registration& boost::python::converter::detail::registered_base<double (&)(const NRVec<double>&)>::converters’
/usr/local/include/boost/python/converter/arg_from_python.hpp:269:99:   instantiated from  ‘boost::python::converter::pointer_arg_from_python<T>::pointer_arg_from_python(PyObject*) [with T = double (*)(const NRVec<double>&), PyObject = _object]’
/usr/local/include/boost/python/arg_from_python.hpp:70:18:   instantiated from ‘boost::python::arg_from_python<T>::arg_from_python(PyObject*) [with T = double (*)(const NRVec<double>&), PyObject = _object]’
/usr/local/include/boost/preprocessor/iteration/detail/local.hpp:43:1:   instantiated from >‘PyObject* boost::python::detail::caller_arity<5u>::impl<F, Policies, Sig>::operator( (PyObject*, PyObject*) [with F = void (*)(NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&), Policies = boost::python::default_call_policies, Sig = boost::mpl::vector6<void, NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&>, PyObject = _object]’
/usr/local/include/boost/python/object/py_function.hpp:38:33:   instantiated from ‘PyObject* boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller<void (*)(NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&), boost::python::default_call_policies, boost::mpl::vector6<void, NRMat<double>&, NRVec<double>&, double, double (*)(const NRVec<double>&), int&> >, PyObject = _object]’ amoeba_py.C:79:1:   instantiated from here
/usr/local/include/boost/python/converter/registered.hpp:86:7: error: no matching function >for call to ‘register_shared_ptr1(double (*)(const NRVec<double>&))’
/usr/local/include/boost/python/converter/registered.hpp:86:7: note: candidate is:
/usr/local/include/boost/python/converter/registered.hpp:77:3: note: template<class T> void boost::python::converter::detail::register_shared_ptr1(const volatile T*)
make: *** [amoeba_py.o] Error 1

谁能帮我破译这个错误的含义,也许能给我一些有用的提示来完成我的项目。值得注意的是,经典的 Boost Python 示例编译得很好。 谢谢!

这是有问题的文字:(第 73-79 行)

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(amoeba)
{
     def("amoeba", NR::amoeba);
}

【问题讨论】:

  • 您能否发布来自 amoeba_py.C 的违规代码部分?我看到错误消息中提到的第 73 和 79 行,这些行是什么?
  • @Praetorian 好的!我添加了有问题的代码。感谢 ps,如果您还需要数字配方功能,您可以在这里找到它:[link]www2.units.it/ipl/students_area/imm2/files/…)。
  • 看起来下面答案中发布的链接解释了问题。您的 amoeba 函数需要一个函数指针,要将其公开给 python,您需要为它创建一个包装器。

标签: c++ python boost compiler-errors boost-python


【解决方案1】:

我怀疑您遇到了与提问 this question 的人类似的问题。您是否在 C++ 代码中将函数指针作为参数传递?如果是这样,您无法在 Python 中执行此操作——请查看原因的答案。

【讨论】:

    【解决方案2】:

    您忘记了引用运算符。因此,def 正在获取参数类型 double (*)(const NRVec&lt;double&gt;&amp;) 而不是它所期望的 const volatile T*

    您的代码应该如下所示:

    BOOST_PYTHON_MODULE(amoeba)
    {
         def("amoeba", &NR::amoeba);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 2015-12-01
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      相关资源
      最近更新 更多