尝试包装一个二进制库,其中头文件定义了一个typedef struct x x_t
,其中struct x未被定义。如何定义一个接口文件,使得Python可以在下面定义的函数中使用类型定义的结构。
myconnection.h
typedef int myConnectionError;
// Note: this struct does not exist
// The typedef is done to strengthen type checking
typedef struct _myConnectionHandle* myConnectionHandle;
myConnectionError myConnectionOpen( const char *x , const char *y , myConnectionHandle *hand);
myconnection.i
% module myconnection
%{
#include "myconnection.h"
%}
%include "myconnection.h"
工作中的C/C++代码示例
myConnectionHandle chandle;
myConnectionError error;
error = myConnectionOpen("foo","bar",&chandle);
预期的Python代码
import myconnection
handle = myconnection.myConnectionHandle
err = myconnection.myConnectionOpen("foo","1080",handle)
Python结果
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myconnection
>>> handle = myconnection.myConnectionHandle()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'myConnectionnHandle'
typedef struct _myConnectionHandle* myConnectionHandle;
的注释很奇怪;它说结构体不存在,但是typedef必须指向一个实际的类/结构体。请问_myConnectionHandle
的声明是什么呢?注意(可能与此无关)句柄类型是指向结构体的指针,但参数是指向句柄的指针,这真的是您的意图吗? - Oliver