2017-06-15 29 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