čtvrtek 27. dubna 2017

mixing C and Fortran code

To call the FORTRAN subroutine from C code:

      SUBROUTINE GETF(N,T,Y,F,PAR)
      IMPLICIT NONE
      INTEGER N
      DOUBLE PRECISION T, F(N), Y(N), PAR(*)
...
      RETURN
      END 

add declaration in C code:
 
void getf_(const int *N, double *t, double Y[], double F[], double PAR[]);

(keep in mind, that all parameters in FORTRAN are passed by reference)

The C call statement is then

int main (int nargin, char **args)
{
 const int N=6;
 double t, y[N], f[N], p[2];
 p[0]=3.14;p[1] =2.71828;
 t=0.1;
 for (i=0;i<N;i++) y[i]=0.0; 
 
 getf_(&N, &t, y, f, p); 
 
 for (i=0;i<N;i++) printf(" %.5e", y[i]);
 return ;
}

Compilation:
gforftan -c funcf.f
gcc main.c funcf.o -o a.out