Here is a simple example to use SWIG to automatically wrap C function and generate a wrapper and build a shared library for python.
/*** File : example.c ***/
#include
<time.h>
double My_variable = 3.0;
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
int my_mod(int x, int y) {
return (x%y);
}
char *get_time()
{
time_t ltime;
time(<ime);
return ctime(<ime);
}
/*************************/
/*** example.i ***/
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
/******************/
Another example:
double My_variable = 3.0;
int fact(int n) {
if (n <= 1)
return 1;
else
return n*fact(n-1);
}
int my_mod(int n, int m) {
return(n % m);
}
%module example
%{
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
%}
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
What if you have some pointer arguments in C ? How to deal with it in Python?
For example:void add(double a, double b, double *result) {
*result = a + b;
}
Please refer to this document. It will give you the nametype mapping in SWIG.http://www.swig.org/Doc1.3/Arguments.html
No comments:
Post a Comment