使用boost::python手动构建共享对象

2024-05-14 11:14:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用boost::python(通过自制程序安装)创建一个共享对象,该对象可以在操作系统附带的python2.7的osx上的Python中加载。我需要链接哪些库才能获得可用的共享对象?在

这是hello_ext.cpp,取自教程

// hello_ext.cpp
char const* greet() {
  return "hello, world";
}

#include <boost/python.hpp>

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

我可以像这样编译这个示例,尽管这需要一段时间。在

^{pr2}$

但是,当我试图链接它并生成一个so时,我得到了一堆未定义的符号:

$ clang++ -shared -o hello_ext.so hello_ext.o | & head -n 7
Undefined symbols for architecture x86_64:
  "_PyString_Type", referenced from:
      boost::python::to_python_value<char const* const&>::get_pytype() const in hello_ext.o
  "__Py_NoneStruct", referenced from:
      boost::python::api::object::object() in hello_ext.o
  "boost::python::detail::init_module(char const*, void (*)())", referenced from:
      _inithello_ext in hello_ext.o

其中一些显然来自Python解释器,实际上{}解决了一些未解决的符号错误:

$ clang++ -shared -o hello_ext.so hello_ext.o -lpython | & head -n 7
Undefined symbols for architecture x86_64:
  "boost::python::detail::init_module(char const*, void (*)())", referenced from:
      _inithello_ext in hello_ext.o
  "boost::python::detail::gcc_demangle(char const*)", referenced from:
      boost::python::type_info::name() const in hello_ext.o
  "boost::python::detail::scope_setattr_doc(char const*, boost::python::api::object const&, char const*)", referenced from:
      void boost::python::def<char const* (*)()>(char const*, char const* (*)()) in hello_ext.o

关于boost::python的文档here详细介绍了如何与cmake一起使用该库,但没有详细说明在链接时需要哪些库。在


Tags: 对象infromhellosoobject链接ext
1条回答
网友
1楼 · 发布于 2024-05-14 11:14:04

boost::python不是只包含头的库,它包含一个二进制组件。例如,你需要和它联系起来

clang++ ... -I/usr/include/python2.7 -lboost_python -lpython2.7  

该库显然是由自制程序包boost-python安装的,而不是{}。在

相关问题 更多 >

    热门问题