I am learning swig to use C codes from Python.
Here is the swig file mvf.i:
%module mvf
%include "carrays.i"
%array_class(double, doubleArray);
%typemap(out) double, float "$result = PyFloat_FromDouble($1);"
%include mvf.h
Here is the mvf.c file
#include <stdio.h>
double SumSquares(int n, double x[])
{
double sum;
int i;
for (sum = 0.0, i = 0; i < n; i++) sum += x[i]*x[i];
printf("DEBUG: %f\n", sum);
return sum;
}
The mvf.h file has the single line:
double SumSquares(int n, double x[]);
Here is a script mvf.sh to create the _mvf.so file:
swig -python mvf.i
gcc -fpic -c mvf.c mvf_wrap.c -I/usr/local/include/python2.3
gcc -shared mvf_wrap.o mvf.o -lm -O3 -o _mvf.so
and finally the Python test:
Python 2.3.3 (#1, Feb 4 2004, 13:34:29)
[GCC 3.3.1 (Mandrake Linux 9.2 3.3.1-2mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import mvf
a = mvf.doubleArray(2)
a[0] = 1.0; a[1] = 2.0;
mvf.SumSquares(2, a) DEBUGGING: 5.000000 # C code ok!
0.0 # !!!??? ... expecting a 5.0
What could have gone wrong?
TIA,
Ernie