简体   繁体   English

你好世界用boost python和python 3.2

[英]Hello world with boost python and python 3.2

So I'm trying to interface python 3.2 and c++ using boost python, and have come across many many issues. 所以我试图使用boost python来连接python 3.2和c ++,并且遇到了很多问题。 I've finally gotten it to compile using the 2.7 libraries and it works, but I can't seem to make it work with python 3.2. 我终于使用2.7库进行编译了它的工作原理,但我似乎无法使用python 3.2。

Here's the c++ code 这是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);
}

If I compile it using the 2.7 libraries it works just fine, but when I use the 3.2 libraries I get tons of undefined references from libboost_python.so 如果我使用2.7库编译它可以正常工作,但是当我使用3.2库时,我从libboost_python.so获得大量未定义的引用

Otherwise I wrote a little bit of python to make it work: 否则我写了一点python来使其工作:

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

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

and this will create an so using python 3.2 or 2.7 build, but when I open the python 3 interpreter and attempt to import the so it give me the error undefined symbol PyClass_Type from libboost_python.so again. 这将使用python 3.2或2.7版本创建一个,但是当我打开python 3解释器并尝试导入时,它再次从libboost_python.so中给出错误未定义符号PyClass_Type。 Any ideas? 有任何想法吗? Is boost python compatible with python 3.x? boost python是否与python 3.x兼容?

If the information is useful, here is my attempted compile using 3.2: 如果信息有用,这是我尝试使用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

And the error from the python 3 interpreter is 而python 3解释器的错误是

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

Thanks for any help! 谢谢你的帮助!

I had the exact same problem, with Ubuntu 12.04. 我有完全相同的问题,使用Ubuntu 12.04。 I installed the 1.48 version of the library and had to link with libboost_python-py32.so instead of libboost_python.so After this the linker errors was gone. 我安装了1.48版本的库,不得不与libboost_python-py32.so而不是libboost_python.so链接。之后,链接器错误消失了。

The above c++ code compiles into a module with 上面的c ++代码编译成一个模块

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

This compile command adds -lboost_python3 , and -shared , and also the naming convention for python extension modules. 该编译命令添加-lboost_python3 ,和-shared ,也为Python扩展模块的命名约定。 You should also install the python3-dev package, and configure/build/install boost with python3, if you haven't already. 您还应该安装python3-dev软件包,并使用python3配置/ build / install boost(如果您还没有)。

In python 3, I can then do the following: 在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!
>>>

You should be off to the races at that point. 那时你应该参加比赛。

Although this discussion old, just for the record: Modify project-config.jam to change the python version to your setup 虽然这个讨论很老,仅供记录:修改project-config.jam以将python版本更改为您的设置

# Python configuration
using python : 3.4 : /usr ;

Then build boost: 然后构建提升:

./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

The later command requires super user privileges. 后面的命令需要超级用户权限。 Then move to the folder containing C++ code for the extension: 然后转到包含扩展名的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

You can then import hello into your python environment. 然后,您可以将hello导入到python环境中。

Linking in the python libraries (for eg -L/usr/lib/x86_64-linux-gnu -lpython2.7 on linux or find_package(PythonLibs) in CMake 1 ) will make this linker issue go away. 链接到python库(例如Linux上的-L / usr / lib / x86_64-linux-gnu -lpython2.7或CMake 1中的 find_package(PythonLibs))会使这个链接器问题消失。

Here is a more detailed explanation of the issue below. 以下是对此问题的更详细说明。 On the command line, 在命令行上,

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

If you are feeling lazy and assuming your libboost_python is linking to python2.7, just run this 如果你感到懒惰并假设你的libboost_python链接到python2.7,那么就运行它

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

You should see something like 你应该看到类似的东西

U PyString_Size

So PyString_Size is undefined ( U ) in libboost_python27.so. 所以PyString_Size在libboost_python27.so中是未定义的( U )。 This is what the linker was complaining about. 这就是链接器抱怨的内容。 We've confirmed that. 我们已经证实了这一点。 Now let's look for this symbol in libpython. 现在让我们在libpython中查找这个符号。

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

On my machine, I saw something like this: 在我的机器上,我看到这样的事情:

00000000000f0530 T PyString_Size

The T indicates that the text for this symbol is at the address indicated. T表示该符号的文本位于指示的地址。 So this is proof that we were not linking in libpython in addition to libboost_python. 所以这证明除了libboost_python之外我们还没有在libpython中进行链接。

1 Why aren't you using CMake? 1你为什么不使用CMake? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM