将两个使用C++流的共享库导入 python 会导致输出损坏

Importing two shared libraries which uses C++ streams into python results in corrupted output

本文关键字:python 导入 共享 损坏 输出 两个 C++      更新时间:2023-10-16

我一直在研究一个特别讨厌的错误 - 想从社区中找出这只是我愚蠢(完全可能)还是发生了一些奇怪的事情。

因此,要复制该问题,您需要GCC 5.3和boost 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

(忽略 boost 库的.pic扩展,它只是一个静态库,其中包含使用-fPIC编译的对象 - 使用相同的编译器。

现在,我只需将它们导入 python,并调用 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: 10n'
>>> 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: 10n'
>>> libpyto.hello2("Hello") 
'Hello: Hello, here is a number: '  <<<!!! What???

如您所见,无论导入顺序如何,第二个 hello 函数都无法正确生成输出。所以我的问题是,为什么第二次调用整数值的流式传输失败?

编辑:再有一个数据点,在流上启用异常,导致在第二次调用时引发std::bad_cast

您不能在同一进程中混合多个 Boost.Python 副本。如果这样做,则会获得两个类型的注册表,并且只会找到并使用一个。

解决方案:使用 Boost.Python 作为共享库,在两个 Python 模块共享对象之间共享。

好的 - 所以问题最终很容易解决。问题源于-static-libstdc++ -static-libgcc.看起来你不能将多个模块导入到python中,这些模块具有libstdc++和libgcc静态链接。