2011-08-25 57 views
7

所以我试图使用boost python来连接python 3.2和C++,并且遇到了很多很多问题。我终于得到它使用2.7库编译,它的工作原理,但我似乎无法使其与Python 3.2的工作。Hello world with boost python and python 3.2

这里的C++代码

#include <iostream> 

using namespace std; 

void say_hello(const char* name) { 
    cout << "Hello " << name << "!\n"; 
} 

int main(){return 0;} 

#include <boost/python/module.hpp> 
#include <boost/python/def.hpp> 
using namespace boost::python; 

BOOST_PYTHON_MODULE(hello) 
{ 
    def("say_hello", say_hello); 
} 

如果我编译使用2.7库,它工作得很好,但是当我使用3.2库我得到吨未定义的引用从libboost_python.so

否则我写了蟒蛇的一点点,使其工作:

from distutils.core import setup 
from distutils.extension import Extension 

setup(name="PackageName", 
    ext_modules=[ 
     Extension("hello", ["testBoost.cpp"], 
     libraries = ["boost_python"]) 
    ]) 

,这将创建一个这样使用Python 3.2或2.7的身材,但是当我打开了Python 3解释中eter并尝试导入,因此它再次给我提供来自libboost_python.so的错误未定义符号PyClass_Type。有任何想法吗?增强python兼容python 3.x?

如果信息是有用的,这里是一个使用3.2我试图编译:

$ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python -lpython3.2mu 
    /tmp/ccdmU1Yu.o: In function `PyInit_hello': 
    testBoost.cpp:(.text+0xc2): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_Size' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyFile_FromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromStringAndSize' 
    /usr/local/lib/libboost_python.so: undefined reference to `Py_InitModule4_64' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromFormat' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyNumber_Divide' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyNumber_InPlaceDivide' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_AsLong' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_InternFromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyClass_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_AsString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_FromLong' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyFile_AsFile' 
    collect2: ld returned 1 exit status 

和错误从Python 3的解释是

File "<stdin>", line 1, in <module> 
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type 

感谢您的帮助!

+1

如果可以,您可能需要考虑查看SWIG而不是Boost.Python。它需要更少的样板代码,我已经很容易地在Python3中使用它。 – Sean

+2

@Sean我不确定你在说什么样的样板代码;我的boost/python只有5行额外的代码才能正常工作。 – steventrouble

回答

5

上述C++代码编译成一个模块与

$ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python3 -lpython3.2mu -o hello.so -shared 

此编译命令添加-lboost_python3,并-shared,并且还用于Python扩展模块的命名约定。你还应该安装python3-dev软件包,并且如果你还没有安装,使用python3来配置/编译/安装boost。

在Python 3,然后我就可以做到以下几点:

$ python3 
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) 
[GCC 4.5.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import hello 
>>> hello.say_hello('bill') 
Hello bill! 
>>> 

你应该关闭在该点的比赛。

+1

我尝试使用-lboost_python3,但仍然得到相同的错误,并且我已经将我的jam文件更改为指向python 3 – Dwight

+1

好吧,所以我编译了你的指令,它告诉我我需要用-fpic重新编译, ,而我最终以.so结尾。但是现在,当我尝试导入python时,出现如下错误:ImportError:/usr/local/lib/libboost_python3.so.1.47.0:undefined symbol:PyClass_Type – Dwight

8

我有与Ubuntu 12.04完全相同的问题。我安装了库的1.48版本,并且必须链接libboost_python-py32.so而不是libboost_python.so之后,链接器错误消失了。

1

虽然这次讨论的时候,仅仅是为了记录: 修改项目config.jam中改变Python版本您的设置

# Python configuration 
using python : 3.4 : /usr ; 

然后建立升压:

./b2 clean 
./b2 --with-python link=static cxxflags="-std=c++11 -fPIC" variant=release stage 
./b2 --with-python link=static cxxflags="-std=c++11 -fPIC" variant=release install 

后来的命令需要超级用户权限。然后移动到包含文件夹C++代码的扩展:

g++ -std=c++11 hellopy.cpp -I/usr/include/python3.4 -I/usr/local/include/boost/python -lboost_python3 -o hello.so -shared -fPIC 

然后,您可以导入招呼到您的Python环境。