Vì vậy, tôi đang cố gắng để giao diện python 3.2 và C++ bằng cách sử dụng python tăng, và đã gặp rất nhiều vấn đề. Tôi cuối cùng đã nhận được nó để biên dịch bằng cách sử dụng 2,7 thư viện và nó hoạt động, nhưng tôi dường như không thể làm cho nó hoạt động với python 3.2.Xin chào thế giới với python tăng và python 3.2
Đây là mã 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);
}
Nếu tôi biên dịch nó bằng cách sử dụng 2,7 thư viện nó làm việc tốt, nhưng khi tôi sử dụng 3.2 thư viện tôi nhận được tấn tài liệu tham khảo không xác định từ libboost_python.so
Nếu không tôi đã viết một chút trăn để làm cho nó làm việc:
from distutils.core import setup
from distutils.extension import Extension
setup(name="PackageName",
ext_modules=[
Extension("hello", ["testBoost.cpp"],
libraries = ["boost_python"])
])
và điều này sẽ tạo ra một vì vậy sử dụng python 3.2 hoặc 2.7 build, nhưng khi tôi mở interpr python 3 eter và cố gắng để nhập khẩu vì vậy nó cung cấp cho tôi lỗi không xác định biểu tượng PyClass_Type từ libboost_python.so một lần nữa. Bất kỳ ý tưởng? Là tăng python tương thích với python 3.x?
Nếu thông tin rất hữu ích, đây là biên dịch của tôi đã cố gắng sử dụng 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
Và lỗi từ python 3 phiên dịch là
File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type
Nhờ sự giúp đỡ!
Bạn có thể cân nhắc xem xét SWIG thay vì Boost.Python, nếu bạn có thể.Nó đòi hỏi ít mã boilerplate hơn và tôi đã nhận nó để làm việc với Python3 khá dễ dàng trước đây. – Sean
@Sean Tôi không chắc bạn đang nói về mã bản mẫu nào; tăng/python của tôi hoạt động bơi với chỉ 5 dòng mã phụ. – steventrouble