2016-08-04 31 views
0

升壓:1.60.0 蟒:3.5.0.1(從水蟒)boost_python與來自水蟒蟒3.5 - (少數)未定義的引用

示例代碼:

#include <iostream> 
#include <boost/python.hpp> 
#include <Python.h> 

namespace py = boost::python; 

int main() 
{ 
    // Must be called before any boost::python functions 
    Py_Initialize(); 
    // import the main module 
    py::object main_module = py::import("__main__"); 
    // load the dictionary object out of the main module 
    py::object main_namespace = main_module.attr("__dict__"); 
    // run simple code within the main namespace using the boost::python::exec 
    // function 
    py::exec("print ('Hello, world')", main_namespace); 
    // any valid Python will execute 
    py::exec("print ('Hello, world')[3:5]", main_namespace); 
} 

編譯爲: g++ -std=c++11 test.cpp -o test.o -I/home/user/anaconda3/pkgs/python-3.5.1-0/include/python3.5m/ -L/home/user/anaconda3/pkgs/python-3.5.1-0/lib/python3.5/config-3.5m -lpython3.5m -pthread -lutil -ldl -lboost_python3

輸出:

//usr/local/lib/libboost_python3.so: undefined reference to `PyString_AsString' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_Type' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromStringAndSize' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromString' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_FromFormat' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_FromLong' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyInt_AsLong' 
//usr/local/lib/libboost_python3.so: undefined reference to `PyString_Type' 

在此之前,我也遇到了其他鏈接器錯誤,我正在使用2.7版本的python進行鏈接。我用3.5版本構建了boost_python

我知道這些函數確實不存在3.5;不知道他們在哪裏使用。

相反,PL下面找到:

:~/$ nm /usr/local/lib/libboost_python3.so |grep "PyString" 
       U PyString_AsString 
       U PyString_FromFormat 
       U PyString_FromString 
       U PyString_FromStringAndSize 
       U PyString_Type 
:~/$ nm /usr/local/lib/libboost_python3.so |grep "PyInt" 
       U PyInt_AsLong 
       U PyInt_FromLong 
       U PyInt_Type 

我怎麼能簡單地 '幹吧'?

我其實已經試過在C++文件裏面定義這些函數,因爲我完全知道我不會被調用這些,但它不起作用。我知道這是一個令人愉快的方式......但爲了給你一個想法,我想「繼續前進」,我提到了它。

+0

已經嘗試過了,給了相同的輸出 – Adorn

回答

3

我認爲你的libboost_python3.so適用於python 2.7或python 3.1以前的版本,因爲它指的是例如PyString_AsString,它在Python 3.1,3.2,3.3,3.4或3.5中不可用(基於debian庫中的符號包)。

nm -D /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0|grep PyString_AsString 
0000000000112120 T PyString_AsString 
0000000000111fd0 T PyString_AsStringAndSize 

和Python 3.4(同3.5)

nm -D /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0 |grep AsString 
000000000017ea10 T PyByteArray_AsString 
000000000017f680 T PyBytes_AsString 
0000000000180cf0 T PyBytes_AsStringAndSize