导入两个使用C++流到Python的共享库导致损坏的输出

2024-06-25 06:27:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究一个特别讨厌的bug——想从社区里找出这是不是我太愚蠢(完全可能),或者是有什么奇怪的事情发生了。你知道吗

所以,要复制这个问题,需要gcc5.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。你知道吗


Tags: thetohellostringincludeisusrthis
2条回答

好吧-所以问题最终很容易解决。这个问题源于-static-libstdc++ -static-libgcc。似乎无法将多个模块导入到具有libstdc++和libgcc静态链接的python中。你知道吗

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

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

相关问题 更多 >