python - 这个 boost.python 程序有什么错误?

标签 python c++ boost g++ boost-python

#include <iostream>
using namespace std; 
char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

我收到以下错误:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function _start':
(.text+0x20): undefined reference to `main'
/tmp/ccg2v42f.o: In function `inithello_ext':
hello_world.cpp:(.text+0x1a): undefined reference to `boost::python::detail::init_module(char const*, void (*)())'
/tmp/ccg2v42f.o: In function `boost::python::type_info::name() const':
hello_world.cpp:(.text._ZNK5boost6python9type_info4nameEv[_ZNK5boost6python9type_info4nameEv]+0x1f): undefined reference to `boost::python::detail::gcc_demangle(char const*)'
/tmp/ccg2v42f.o: In function `boost::python::to_python_value<char const* const&>::operator()(char const* const&) const':
hello_world.cpp:(.text._ZNK5boost6python15to_python_valueIRKPKcEclES5_[_ZNK5boost6python15to_python_valueIRKPKcEclES5_]+0x1b): undefined reference to `boost::python::converter::do_return_to_python(char const*)'
/tmp/ccg2v42f.o: In function `boost::python::to_python_value<char const* const&>::get_pytype() const':
hello_world.cpp:(.text._ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv[_ZNK5boost6python15to_python_valueIRKPKcE10get_pytypeEv]+0x9): undefined reference to `PyString_Type'
/tmp/ccg2v42f.o: In function `boost::python::api::object::object()':
hello_world.cpp:(.text._ZN5boost6python3api6objectC2Ev[_ZN5boost6python3api6objectC5Ev]+0xd): undefined reference to `_Py_NoneStruct'
/tmp/ccg2v42f.o: In function `void boost::python::def<char const* (*)()>(char const*, char const* (*)())':
hello_world.cpp:(.text._ZN5boost6python3defIPFPKcvEEEvS3_T_[_ZN5boost6python3defIPFPKcvEEEvS3_T_]+0x50): undefined reference to `boost::python::detail::scope_setattr_doc(char const*, boost::python::api::object const&, char const*)'
/tmp/ccg2v42f.o: In function `boost::python::api::object boost::python::detail::make_function_aux<char const* (*)(), boost::python::default_call_policies, boost::mpl::vector1<char const*> >(char const* (*)(), boost::python::default_call_policies const&, boost::mpl::vector1<char const*> const&)':
hello_world.cpp:(.text._ZN5boost6python6detail17make_function_auxIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS4_EEEENS0_3api6objectET_RKT0_RKT1_[_ZN5boost6python6detail17make_function_auxIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS4_EEEENS0_3api6objectET_RKT0_RKT1_]+0x67): undefined reference to `boost::python::objects::function_object(boost::python::objects::py_function const&)'
/tmp/ccg2v42f.o: In function `boost::python::objects::py_function_impl_base::py_function_impl_base()':
hello_world.cpp:(.text._ZN5boost6python7objects21py_function_impl_baseC2Ev[_ZN5boost6python7objects21py_function_impl_baseC5Ev]+0x9): undefined reference to `vtable for boost::python::objects::py_function_impl_base'
/tmp/ccg2v42f.o:(.rodata._ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE]+0x30): undefined reference to `boost::python::objects::py_function_impl_base::max_arity() const'
/tmp/ccg2v42f.o: In function `boost::python::objects::caller_py_function_impl<boost::python::detail::caller<char const* (*)(), boost::python::default_call_policies, boost::mpl::vector1<char const*> > >::~caller_py_function_impl()':
hello_world.cpp:(.text._ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED2Ev[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEED5Ev]+0x20): undefined reference to `boost::python::objects::py_function_impl_base::~py_function_impl_base()'
/tmp/ccg2v42f.o:(.rodata._ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE[_ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFPKcvENS0_21default_call_policiesENS_3mpl7vector1IS6_EEEEEE]+0x10): undefined reference to `typeinfo for boost::python::objects::py_function_impl_base'
/tmp/ccg2v42f.o: In function `boost::python::converter::expected_pytype_for_arg<char const*>::get_pytype()':
hello_world.cpp:(.text._ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x1b): undefined reference to `boost::python::converter::registry::query(boost::python::type_info)'
hello_world.cpp:(.text._ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIPKcE10get_pytypeEv]+0x32): undefined reference to `boost::python::converter::registration::expected_from_python_type() const'
collect2: error: ld returned 1 exit status

最佳答案

using namespace boost::python;
// Change to
using namespace; 
boost::python;

您已经使用了 namespace std; 函数两次。您应该只在程序的开头或放置它的任何地方添加 using namespace std;,尽管您应该只使用它一次。

这里的问题好像是你用了那个函数两次。该程序似乎与这两个功能发生冲突。

您还会在存在 undefined reference 的地方收到其中两个错误。

(.text+0x20): undefined reference to `main'

hello_world.cpp:(.text+0x1a): undefined reference to `boost::python::detail::init_module(char const*, void (*)())'

要么您打算在程序中使用 int main() 函数,因为这允许您声明一组函数,要么存在另一个问题。

请试一试,让我知道结果如何。

关于python - 这个 boost.python 程序有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42795456/

相关文章:

c++ - 为什么我不能在 C++14 的 lambda 中 move std::unique_ptr?

c++ - 如何使用从 shell 脚本调用的 gdb 运行程序,该脚本又通过 boost::process 从 c++ 调用

c++ - 使用 boost 检查端口可用性

c++ - Boost Asio,异步 UDP 客户端 - 关机时崩溃

python - python3 中的 byte 与 str.encode

python - 使用pyplot的线性回归中的奇怪图形

python - 使用正则表达式从字符串中提取 float

c++ - 为什么 deques 默认用作堆栈的底层容器,而 vectors 可以做到这一点?

c++ - linux ascii 到 utf-16(然后是 sha1 和 base64)编码

Python 多处理 : Process object not callable