所以我试图使用boost python来连接python 3.2和C++,并且遇到了很多很多问题。我终于得到它使用2.7库编译,它的工作原理,但我似乎无法使其与Python 3.2的工作。Hello world with boost python and 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
否则我写了蟒蛇的一点点,使其工作:
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解释中eter并尝试导入,因此它再次给我提供来自libboost_python.so的错误未定义符号PyClass_Type。有任何想法吗?增强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
@Sean我不确定你在说什么样的样板代码;我的boost/python只有5行额外的代码才能正常工作。 – steventrouble