你好世界用boost python和python 3.2

Dwi*_*ght 8 c++ python boost g++ boost-python

所以我试图使用boost python来连接python 3.2和c ++,并且遇到了很多问题.我终于使用2.7库进行编译了它的工作原理,但我似乎无法使用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);
}
Run Code Online (Sandbox Code Playgroud)

如果我使用2.7库编译它可以正常工作,但是当我使用3.2库时,我从libboost_python.so获得大量未定义的引用

否则我写了一点python来使其工作:

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

setup(name="PackageName",
    ext_modules=[
        Extension("hello", ["testBoost.cpp"],
        libraries = ["boost_python"])
    ])
Run Code Online (Sandbox Code Playgroud)

这将使用python 3.2或2.7构建创建一个,但是当我打开python 3解释器并尝试导入时,它再次从libboost_python.so中给出错误未定义符号PyClass_Type.有任何想法吗?boost 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
Run Code Online (Sandbox Code Playgroud)

而python 3解释器的错误是

File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

dar*_*rak 8

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


vse*_*har 5

上面的c ++代码编译成一个模块

$ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python3 -lpython3.2mu -o hello.so -shared
Run Code Online (Sandbox Code Playgroud)

此编译命令添加了-lboost_python3,以及-sharedpython扩展模块的命名约定.您还应该安装python3-dev软件包,并使用python3配置/ build/install 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!
>>>
Run Code Online (Sandbox Code Playgroud)

那时你应该参加比赛.


小智 2

尽管此讨论已过时,但仅供记录:修改project-config.jam以将python版本更改为您的设置

# Python configuration
using python : 3.4 : /usr ;
Run Code Online (Sandbox Code Playgroud)

然后构建提升:

./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
Run Code Online (Sandbox Code Playgroud)

后面的命令需要超级用户权限。然后移动到包含扩展 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
Run Code Online (Sandbox Code Playgroud)

然后您可以将 hello 导入到您的 python 环境中。