我一直在寻找到一个特别讨厌的错误两个共享库 - 想从社会上找出来,如果这只是我太傻了(完全有可能),或者是有什么奇怪的事情。导入它使用C++流入损坏的输出蟒蛇结果
所以,复制的问题,你需要GCC 5.3和1.60提高。
首先pyt.cpp - >其编译成libpyt.so
/*
* This inclusion should be put at the beginning. It will include <Python.h>.
*/
#include <boost/python.hpp>
#include <string>
#include <sstream>
/*
* This is the C++ function we write and want to expose to Python.
*/
const std::string hello1(const std::string& name) {
std::ostringstream str;
str << "Hello: " << name << ", here is a number: " << 10 << std::endl;
return str.str();
}
/*
* This is a macro Boost.Python provides to signify a Python extension module.
*/
BOOST_PYTHON_MODULE(libpyt) {
// An established convention for using boost.python.
using namespace boost::python;
// Expose the function hello2().
def("hello1", hello1);
}
二pyto.cpp - >其编译使用到libpyto.so
/*
* This inclusion should be put at the beginning. It will include <Python.h>.
*/
#include <boost/python.hpp>
#include <string>
#include <sstream>
/*
* This is the C++ function we write and want to expose to Python.
*/
const std::string hello2(const std::string& name) {
std::ostringstream str;
str << "Hello: " << name << ", here is a number: " << 10 << std::endl;
return str.str();
}
/*
* This is a macro Boost.Python provides to signify a Python extension module.
*/
BOOST_PYTHON_MODULE(libpyto) {
// An established convention for using boost.python.
using namespace boost::python;
// Expose the function hello2().
def("hello2", hello2);
}
我编译以下:
/usr/local/gcc5_3_0/bin/g++ -std=c++14 pyt.cpp -fPIC -shared -o libpyt.so -I /usr/local/boost1_60_0_gcc5_3_0/include/ -I /usr/include/python2.7/ -L /usr/local/boost1_60_0_gcc5_3_0/lib64/ -Wl,-Bstatic -l boost_python.pic -Wl,-Bdynamic -lpthread -lpython2.7 -ldl -lrt -static-libstdc++ -static-libgcc
/usr/local/gcc5_3_0/bin/g++ -std=c++14 pyto.cpp -fPIC -shared -o libpyto.so -I /usr/local/boost1_60_0_gcc5_3_0/include/ -I /usr/include/python2.7/ -L /usr/local/boost1_60_0_gcc5_3_0/lib64/ -Wl,-Bstatic -l boost_python.pic -Wl,-Bdynamic -lpthread -lpython2.7 -ldl -lrt -static-libstdc++ -static-libgcc
(忽略增强库的.pic
扩展名,它只是一个静态库智慧。这是编译-fPIC
^h对象 - 使用相同的编译器)
现在,我只是将它们导入到蟒蛇,并调用hello1/2功能:
bash-4.2$ python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libpyt
>>> import libpyto
>>> libpyto.hello2("hello");
'Hello: hello, here is a number: 10\n'
>>> libpyt.hello1("hello");
'Hello: hello, here is a number: ' <<<!!! What???
>>>
bash-4.2$ python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libpyto
>>> import libpyt
>>> libpyt.hello1("Hello")
'Hello: Hello, here is a number: 10\n'
>>> libpyto.hello2("Hello")
'Hello: Hello, here is a number: ' <<<!!! What???
正如你所看到的,不论导入顺序,第二个hello函数无法正确生成输出。所以我的问题是,为什么流出的整数值失败的第二个电话?
编辑:另一个数据点,启用流的例外,导致std::bad_cast
被抛出的第二个电话。
输出错误具体如何?它不明确或明显 – MrJLP
@MrJLP,它缺少'10 \ n'。这就是我知道这个流是如何被破坏的,它不是流出整数10. – Nim
我很好奇,如果分配'int val = 10'会发生什么情况,如果问题仍然存在,则打印出val。可能是一个提升错误。顺便说一句,我会建议清楚地说明问题顶部的实际问题。 – MrJLP