Using C procedures in a Fortran code

Scientific Computing Fortran C/C++


C and Fortran together

Here I described the inter-operability between C and Fortran.

Common terminology The global framework to call methods/functions from one language in other is named: foreign function interface (FFI), and the process binding. Nowadays, this is a commom procedure to use libraries that we need but are not yet developed for our prefered language.

The interoperability is possible in Fortran 2003 as it incorporates an specific module called ISO_C_BINDING that ensures a correct use of the C intrinsic types.

Also we can have a interoperability of derived types:

use iso_c_binding
    type, bind(c) :: my_own_type
        integer (c_init) :: a,b,c
        real (c_float)   :: o,p
    end type my_own_type

with the above code we can inter-operate with a typical C code:

  typedef struct {
    int i,j,k;
    float  r,s;
  } my_c_type;

However, we can also have a procedure inter-operations. For example in Fortran we can make,

use iso_c_binding
  function func(a,b,c,d) bind(c, name='c_func')