Intel® Fortran Compiler 16.0 User and Reference Guide

Standard Fortran and C Interoperability

The Intel® Fortran Compiler supports the Fortran standardized mechanism for allowing Fortran code to reliably communicate (or interoperate) with C code.

This mechanism includes a group of features for C interoperability, enabling mixed-language programming in a more portable manner.

The Fortran standard discusses interoperability in terms of a "companion C processor." Each Fortran implementation is free to choose which C is its companion. Although the standard explicitly specifies a companion C (not C++) processor, you can use C++, as long as you use features that are compatible with C when interoperating with Fortran.

For Intel® Fortran, the supported C companion is the Intel® C++ Compiler or the Microsoft* Visual C++* Compiler on Windows*, and the Intel® C++ Compiler or gcc* on Linux* and OS X*.

The core principle of interoperability is that something should work the same way in Fortran as it does in C. In terms of interoperability, the following applies:

The following sections describe interoperability requirements for types, variables, procedures, and global data.

Example of Fortran Calling C

The following example calls a C function.

C Function Prototype Example

int C_Library_Function(void* sendbuf, int sendcount, int *recvcounts);

Fortran Module Example

module ftn_C_2
   interface 
     integer (C_INT) function C_Library_Function & 
     (sendbuf, sendcount, recvcounts) & 
        BIND(C, name='C_Library_Function’) 
        use, intrinsic :: ISO_C_BINDING  
        implicit none 
        type (C_PTR), value :: sendbuf 
        integer (C_INT), value :: sendcount 
        type (C_PTR), value :: recvcounts 
     end function C_Library_Function 
   end interface 
end module ftn_C_2

Fortran Calling Sequence Example

use, intrinsic :: ISO_C_BINDING, only: C_INT, C_FLOAT, C_LOC 
use ftn_C_2 
...
real (C_FLOAT), target :: send(100) 
integer (C_INT) :: sendcount 
integer (C_INT), ALLOCATABLE, target :: recvcounts(100) 
... 
ALLOCATE( recvcounts(100) ) 
... 
call C_Library_Function(C_LOC(send), sendcount, & 
C_LOC(recvcounts)) 
...

Example of C Calling Fortran

The following example calls a Fortran subroutine called Simulation. This subroutine corresponds to the C void function simulation.

Fortran Code Example

subroutine Simulation(alpha, beta, gamma, delta, arrays) BIND(C) 
   use, intrinsic :: ISO_C_BINDING 
   implicit none 
   integer (C_LONG), value :: alpha 
   real (C_DOUBLE), intent(inout) :: beta 
   integer (C_LONG), intent(out) :: gamma 
   real (C_DOUBLE),dimension(*),intent(in) :: delta
   type, BIND(C) :: pass 
      integer (C_INT) :: lenc, lenf
      type (C_PTR) :: c, f 
   end type pass 
   type (pass), intent(inout) :: arrays
   real (C_FLOAT), ALLOCATABLE, target, save :: eta(:)
   real (C_FLOAT), pointer :: c_array(:) 
   ... 
   ! Associate c_array with an array allocated in C 
   call C_F_POINTER (arrays%c, c_array, (/arrays%lenc/) ) 
   ... 
   ! Allocate an array and make it available in C 
   arrays%lenf = 100 
   ALLOCATE (eta(arrays%lenf)) 
   arrays%f = c_loc(eta) 
   ... 
end subroutine Simulation

C Struct declaration Example

struct pass {int lenc, lenf; float *c, *f;};

C Function Prototype Example

void simulation(long alpha, double *beta, long *gamma, double delta[], struct pass *arrays);

C Calling sequence Example

simulation(alpha, &beta, &gamma, delta, &arrays);