【发布时间】:2011-11-03 23:43:19
【问题描述】:
所以我正在尝试使用 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时,它再次给我来自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
感谢您的帮助!
【问题讨论】:
-
如果可以的话,您可能需要考虑研究 SWIG 而不是 Boost.Python。它需要的样板代码要少得多,而且我以前很容易让它与 Python3 一起工作。
-
@Sean 我不确定你在说什么样板代码;我的 boost/python 只需 5 行额外代码即可流畅运行。
标签: c++ python boost g++ boost-python