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

标签 python c++ gcc boost

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

因此,要重现该问题,您需要 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: 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

最佳答案

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

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

关于python - 将两个使用 C++ 流的共享库导入 python 会导致输出损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44577203/

相关文章:

Python(Flask) 用户输入表单

java - python中的子进程调用以使用JAVA_OPTS调用java jar文件

c++ - 我可以迭代一个迭代器范围内但不在另一个迭代器范围内的元素吗?

c++ - QPainter 未激活

c - 通过 gcc 进行 c 编译时出现 undefined reference 错误

python - Django 中的软件即服务 - 许多公司应该能够拥有相同的用户

python - 这段代码效率太低了,如何增加内存和执行效率呢?

c++ - 在C++中是否可以使用容器的大小作为priority_queue的优先级?

python - 如何覆盖 setup.py 默认使用的编译器 (GCC) 标志?

gcc - CMake:如何显示编译器的标准输出