Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Boost.python导出函数中使用std::string作为返回类型 < >我有一个非常简单的C++函数示例,我通过Pox.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); }_Python_C++_Boost_Boost Python - Fatal编程技术网

在Boost.python导出函数中使用std::string作为返回类型 < >我有一个非常简单的C++函数示例,我通过Pox.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); }

在Boost.python导出函数中使用std::string作为返回类型 < >我有一个非常简单的C++函数示例,我通过Pox.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); },python,c++,boost,boost-python,Python,C++,Boost,Boost Python,它正确地生成了libhello.so,然后我可以在python中使用它 import libhello as lh lh.hello_world() # correctly returns the string "hello, world" 但是,当我将C++中的 > GRUET < /C> >的返回类型更改为 STD::String < /C> >,即 std::string greet(){...} 运行lh.hello\u world()后,我在python中遇到以下崩溃: pyhto

它正确地生成了
libhello.so
,然后我可以在python中使用它

import libhello as lh
lh.hello_world() # correctly returns the string "hello, world"
<>但是,当我将C++中的<代码> > GRUET < /C> >的返回类型更改为<代码> STD::String < /C> >,即

std::string greet(){...}
运行
lh.hello\u world()
后,我在python中遇到以下崩溃:


pyhton崩溃了。知道为什么吗?它是否与非POD返回类型有关?

问题似乎与默认使用的链接器
libpython
有关,该链接器是位于
/System/Library/Frameworks/Python.framework
中的OSX原始链接器。当我从位于
Library/Frameworks/python.framework
中的
python
安装中链接agains
libpython
时,一切都很顺利。我使用的整个命令行发布在下面,以防将来有人碰到这个问题:

g++ -I /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/\
 -I $HOME/boost -fpic export.cpp -shared -lboost_python -lpython2.7 -olibhello.so\
 -L /Library/Frameworks/Python.framework/Versions/2.7/lib\
 -L $HOME/boost/stage/lib

我可以确认
std::string greet(){…}
Ubuntu 17.4/g++-6.3.0/boost 1.62上运行良好,所以这一定与您在类UNIX机器上安装的东西有关。@doqtor Ok,谢谢!那我来看看我这边发生了什么。
Fatal Python error: PyEval_SaveThread: NULL tstate
Abort trap: 6
g++ -I /Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/\
 -I $HOME/boost -fpic export.cpp -shared -lboost_python -lpython2.7 -olibhello.so\
 -L /Library/Frameworks/Python.framework/Versions/2.7/lib\
 -L $HOME/boost/stage/lib