<p>我一直在研究一个特别讨厌的bug——想从社区里找出这是不是我太愚蠢(完全可能),或者是有什么奇怪的事情发生了。你知道吗</p>
<p>所以,要复制这个问题,需要gcc5.3和boost 1.60。你知道吗</p>
<p>第一pyt.cpp公司->;编译为libpyt.so公司你知道吗</p>
<pre><code>/*
* 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);
}
</code></pre>
<p>第二pyto.cpp文件->;编译为libpyto.so公司你知道吗</p>
<pre><code>/*
* 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);
}
</code></pre>
<p>我使用以下方法编译:</p>
<pre><code>/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
</code></pre>
<p>(忽略boost库的<code>.pic</code>扩展,它只是一个静态库,包含使用<code>-fPIC</code>编译的对象-使用相同的编译器)</p>
<p>现在,我只需将它们导入python,并调用hello1/2函数:</p>
<pre><code>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???
</code></pre>
<p>如您所见,无论导入顺序如何,第二个hello函数都无法正确生成输出。所以我的问题是,为什么第二次调用的整数值流失败了?你知道吗</p>
<p><strong>编辑:</strong>再多一个数据点,启用流上的异常,结果在第二次调用时抛出<code>std::bad_cast</code>。你知道吗</p>