2017-06-15 86 views
1

我一直在寻找到一个特别讨厌的错误两个共享库 - 想从社会上找出来,如果这只是我太傻了(完全有可能),或者是有什么奇怪的事情。导入它使用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被抛出的第二个电话。

+0

输出错误具体如何?它不明确或明显 – MrJLP

+0

@MrJLP,它缺少'10 \ n'。这就是我知道这个流是如何被破坏的,它不是流出整数10. – Nim

+0

我很好奇,如果分配'int val = 10'会发生什么情况,如果问题仍然存在,则打印出val。可能是一个提升错误。顺便说一句,我会建议清楚地说明问题顶部的实际问题。 – MrJLP

回答

1

好了 - 这样的问题很简单在年底前解决。该问题源于-static-libstdc++ -static-libgcc。看起来你不能将多个模块导入到静态链接了libstdC++和libgcc的python中。

1

在同一过程中你不能混用的Boost.Python的多个副本。如果你这样做,你会得到两个类型的注册表,只有一个会被发现和使用。

解决方法:使用Boost.Python的共享库,你的两个Python模块共享对象之间共享。

+0

不幸的是,这不是问题。 – Nim