example.h:Việc đóng gói một lớp C++ bằng Python sử dụng SWIG
#ifndef EXAMPLE_H
#define EXAMPLE_H
class Math {
public:
int pi() const;
void pi(int pi);
private:
int _pi;
};
#endif
example.cpp:
#include "example.h"
int Math::pi() const {
return this->_pi;
}
void Math::pi(int pi) {
this->_pi = pi;
}
example.swig:
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "example.h"
Tôi là n tạo ra những gói "example.py" và "example_wrap.c" sử dụng:
swig -python example.swig
Khi tôi cố gắng biên dịch lớp wrapper sử dụng:
g++ -fPIC -c example.cpp example_wrap.c -I/usr/local/include/python2.6/
tôi nhận được lỗi sau:
example_wrap.cpp: In function "PyObject* Swig_var_Math_get()":
example_wrap.cpp:2725: error: expected primary-expression before "void"
example_wrap.cpp:2725: error: expected ")" before "void"
các Lỗi là tại dòng sau:
pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&Math), SWIGTYPE_p_class, 0);
#define SWIG_as_voidptr(a) (void *)((const void *)(a))
Có đúng cách để tạo lớp trình bao bọc "example_wrap.c" không?
Bạn gặp sự cố về định dạng khá tệ. –
Không tệ đến thế, họ chỉ không biết thụt lề tất cả bốn mã. Đã sửa. – zwol