Hello world 使用 boost python 和 python 3.2
所以我尝试使用 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 构建创建一个 so,但是当我打开 python 3 解释器并尝试导入 so 时,它会给我错误 undefined symbol PyClass_Type from libboost_python.so 再次。有什么想法吗? 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
感谢您的帮助!
So I'm trying to interface python 3.2 and c++ using boost python, and have come across many many issues. 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.
Here's the c++ code
#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
Otherwise I wrote a little bit of python to make it work:
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. Any ideas? Is boost python compatible with python 3.x?
If the information is useful, here is my attempted compile using 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
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在 Ubuntu 12.04 上遇到了完全相同的问题。我安装了 1.48 版本的库,并且必须链接到
libboost_python-py32.so
而不是libboost_python.so
在此之后,链接器错误消失了。I had the exact same problem, with Ubuntu 12.04. I installed the 1.48 version of the library and had to link with
libboost_python-py32.so
instead oflibboost_python.so
After this the linker errors was gone.上面的 C++ 代码编译成一个模块,
此编译命令添加了
-lboost_python3
和-shared
,以及 python 扩展模块的命名约定。您还应该安装 python3-dev 包,并使用 python3 配置/构建/安装 boost(如果还没有的话)。在 python 3 中,我可以执行以下操作: 此时
您应该开始比赛了。
The above c++ code compiles into a module with
This compile command adds
-lboost_python3
, and-shared
, and also the naming convention for python extension modules. You should also install thepython3-dev
package, and configure/build/install boost with python3, if you haven't already.In python 3, I can then do the following:
You should be off to the races at that point.
虽然这个讨论很老了,但仅供记录:
修改project-config.jam以将python版本更改为您的设置
然后构建boost:
后面的命令需要超级用户权限。然后移动到包含扩展的 C++ 代码的文件夹:
然后您可以将 hello 导入到您的 python 环境中。
Although this discussion old, just for the record:
Modify project-config.jam to change the python version to your setup
Then build boost:
The later command requires super user privileges. Then move to the folder containing C++ code for the extension:
You can then import hello into your python environment.
在 python 库中链接(例如 linux 上的 -L/usr/lib/x86_64-linux-gnu -lpython2.7 或 CMake1 中的 find_package(PythonLibs))将使此链接器问题消失。
下面是对该问题的更详细解释。在命令行上,
如果您感到懒惰并假设您的 libboost_python 链接到 python2.7,只需运行此命令
您应该会在 libboost_python27.so 中看到类似
So PyString_Size is undefined (
U
) 的内容。这就是链接器所抱怨的。我们已经证实了这一点。现在让我们在 libpython 中查找这个符号。在我的机器上,我看到类似这样的内容:
T
表示该符号的文本位于指示的地址处。所以这证明我们除了 libboost_python 之外还没有链接 libpython。1 为什么不使用 CMake? :)
Linking in the python libraries (for e.g. -L/usr/lib/x86_64-linux-gnu -lpython2.7 on linux or find_package(PythonLibs) in CMake1) will make this linker issue go away.
Here is a more detailed explanation of the issue below. On the command line,
If you are feeling lazy and assuming your libboost_python is linking to python2.7, just run this
You should see something like
So PyString_Size is undefined (
U
) in libboost_python27.so. This is what the linker was complaining about. We've confirmed that. Now let's look for this symbol in libpython.On my machine, I saw something like this:
The
T
indicates that the text for this symbol is at the address indicated. So this is proof that we were not linking in libpython in addition to libboost_python.1 Why aren't you using CMake? :)