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.