你好世界用boost python和python 3.2

时间:2011-08-25 19:16:45

标签: 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);
}

如果我使用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"])
    ])

这将使用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

python 3解释器的错误是

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

感谢您的帮助!

4 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分: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配置/ 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!
>>>

那时你应该参加比赛。

答案 2 :(得分:1)

虽然这个讨论很老,只是为了记录: 修改project-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

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

答案 3 :(得分:1)

在python库中进行链接(例如linux上的-L / usr / lib / x86_64-linux-gnu -lpython2.7或CMake 1 中的find_package(PythonLibs))将导致此链接器问题走开。

以下是此问题的更详细说明。在命令行上,

$ nm --dynamic <path-to>/libboost_python.so | grep PyString_Size

如果您感到懒惰并假设您的libboost_python链接到python2.7,则只需运行此

$ nm --dynamic `locate libboost_python27.so | awk 'NR==1'` | grep PyString_Size

您应该看到类似

U PyString_Size

因此在libboost_python27.so中未定义PyString_Size(U)。这就是链接器所抱怨的。我们已经确认。现在让我们在libpython中寻找这个符号。

$ nm --dynamic `locate libpython2.7.so | awk 'NR==1'` | grep PyString_Size

在我的机器上,我看到了类似的东西:

00000000000f0530 T PyString_Size

T表示此符号的文本在所示的地址上。因此,这证明了我们除了libboost_python外没有链接libpython。

1 为什么不使用CMake? :)