c - Access struct in Python using SWIG -
do have redefine given struct (given in .c file, included in compilation) in interface file make accessible via python?
edit: if defined in header file, have include header file in interface file, right?
i think don't have to, except want add member functions c structures.
/* file : vector.h */ ... typedef struct { double x,y,z; } vector; // file : vector.i %module mymodule %{ #include "vector.h" %} %include "vector.h" // grab original c header file
adding member functions c structures
/* file : vector.h */ ... typedef struct { double x,y,z; } vector; // file : vector.i %module mymodule %{ #include "vector.h" %} %extend vector { // attach these functions struct vector vector(double x, double y, double z) { vector *v; v = (vector *) malloc(sizeof(vector)); v->x = x; v->y = y; v->z = z; return v; } ~vector() { free($self); } double magnitude() { return sqrt($self->x*$self->x+$self->y*$self->y+$self->z*$self->z); } void print() { printf("vector [%g, %g, %g]\n", $self->x,$self->y,$self->z); } };
Comments
Post a Comment