【发布时间】:2017-12-03 07:43:09
【问题描述】:
我有一个非常简单的 C++ 函数示例,我通过 Boost.Python 导出到 python (2.7.13)。代码如下:
#include <string>
#include <boost/python.hpp>
const char* greet()
{
return "hello, world!";
}
BOOST_PYTHON_MODULE(libhello)
{
using namespace boost::python;
def("hello_world", greet);
}
我在 OSX 10.10.5 机器上编译
g++ -I ~/boost -L ~/boost/stage/lib\
-L /System/Library/Frameworks/Python.framework/Versions/Current\
-fpic export.cpp -shared -lboost_python -lPython -olibhello.so
它会正确生成 libhello.so,然后我可以在 python 中使用它
import libhello as lh
lh.hello_world() # correctly returns the string "hello, world"
但是,当我将 C++ 文件中greet 的返回类型更改为std::string 时,即
std::string greet(){...}
在运行 lh.hello_world() 后,我在 python 中遇到以下崩溃:
Fatal Python error: PyEval_SaveThread: NULL tstate
Abort trap: 6
和 python 崩溃。知道为什么吗?和非POD返回类型有关系吗?
【问题讨论】:
-
我可以确认
std::string greet(){...}在Ubuntu 17.4 / g++-6.3.0 / boost 1.62上对我来说工作正常,所以这一定与您在类 UNIX 机器上安装的内容有关。 -
@doqtor 好的,谢谢!那我看看我这边是怎么回事。
标签: python c++ boost boost-python