简体   繁体   English

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

[英]Manually build a shared object using boost::python

I'm trying to create a shared object using boost::python (installed through homebrew) that's loadable in Python on OS X using the Python 2.7 that ships with the OS. 我正在尝试使用boost::python (通过自制程序安装)创建共享库,该共享库可使用OS随附的Python 2.7在OS X上的Python中加载。 What libraries do I have to link in to get a usable shared object? 我必须链接到哪些库才能获得可用的共享库?

Here's hello_ext.cpp , taken from the tutorial 这是来自本教程的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);
}

I can compile the example like this, although it takes a while. 我可以像这样编译示例,尽管需要一些时间。

$ clang++ -fPIC -c -I/usr/include/python2.7 hello_ext.cpp

However, when I attempt to link it and produce an so I get a bunch of undefined symbols: 但是,当我尝试将其链接并产生a时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

Some of them clearly come from the Python interpreter, and indeed -lpython solves some of the unresolved symbol errors: 其中一些显然来自Python解释器,实际上-lpython解决了一些未解决的符号错误:

$ 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

The documentation here for boost::python goes into some detail about how to use the library in conjunction with cmake , but doesn't say much about what libraries are required at link time. 这里boost::python文档详细介绍了如何将库与cmake结合使用,但并没有过多说明链接时需要哪些库。

boost::python is not a header-only library, it includes a binary component. boost::python不是仅标头的库,它包含一个二进制组件。 You need to link with it, for example 您需要与之链接,例如

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

The library is apparently installed by the homebrew package boost-python , not boost . 该库显然是由Homebrew软件包boost-python安装的,而不是boost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM