tags:

views:

33

answers:

1

Hi,

I have an ema function in fortran 77. How can I call to fortran function from C. I know that in fortran 2003 there is a iso_c_bindings but I need some solution for old 77 standard.

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

integer leninarray, lenoutarray, n, i
double precision inarray(leninarray), outarray(lenoutarray)

do 10 i = n+1, leninarray
    outarray = inarray(i) * (1/n) + outarray(i-1) * (1-(1/n))

continue
end

Thanks for any help regards

+1  A: 

Fortran has pretty much the same calling convention as C, but with the following differences:

  • Everything is passed by reference: you need to pass addresses to the fortran routine
  • The order of the arguments are the same (pushed from right to left)
  • The address of a return value needs to be pushed on the stack after the arguments when calling a fortran function (not fortran subroutines). In the corresponding C prototype, this amounts to declaring the function void, and having an extra first argument which is a pointer to the return value.
  • Sometimes, you need to append an extra underscore to the fortran name. This needs some experimentation.

E.g. if you have a fortran routine

subroutine ema(inarray,leninarray,n,outarray,lenoutarray)

it turns into

void ema(double*, int*, int*, double*, int*)

or

void ema_(double*, int*, int*, double*, int*)

depending on your machine. In C++ you want

extern "C" void ema(double*, int*, int*, double*, int*)

or

extern "C" void ema_(double*, int*, int*, double*, int*)

Don't forget that fortran arrays start as 1 when you pass indices to the routines.

Alexandre C.