将两个使用C++流的共享库导入 python 会导致输出损坏
Importing two shared libraries which uses C++ streams into python results in corrupted output
我一直在研究一个特别讨厌的错误 - 想从社区中找出这只是我愚蠢(完全可能)还是发生了一些奇怪的事情。
因此,要复制该问题,您需要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静态链接。
相关文章:
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- 在 python 模块中导入子模块时PyImport_Import失败
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 编译boost_python扩展无法在 python 2.7 中导入
- Python 3.8:运行外部C++代码:无法导入模块
- C++ #include<XXX.h>相当于 Python 导入的 XXX 作为 X
- 如何将 Cython 生成的模块从 python 导入到 C/C++ 主文件?(C/C++编程)
- Python - 导入 c++ 模块接口 - 无法打开共享对象文件
- 提升 Python 导入失败,未定义包装类的符号
- 如何处理python导入模块中c++断言导致的Celery WorkerLostError
- Boost Python :导入模块时遇到问题
- 从 boost python 导入依赖的 python 库
- 无法使用boost.python导入模块
- 从自定义构建的Python导入自定义模块失败
- Boost Python导入一个带有std::vectors作为参数的c++函数
- c++ Python导入类;调用方法
- 由python导入的python错误
- Python 3.x - 导入密码化 C++ 代码时未定义的符号
- MAC雪豹端口上的Boost python导入错误
- 从您无法控制且无权访问的线程中释放 Python 导入锁