Intel® Fortran Compiler XE 13.1 User and Reference Guides

Global Data Overview

A module variable or a common block can interoperate with a C global variable if the Fortran entity uses the BIND attribute and the members of that entity are also interoperable. For example, consider the entities c_extern, c2, com and single in the following module:

module LINK_TO_C_VARS
   use, intrinsic :: ISO_C_BINDING
   integer(C_INT), BIND(C) :: c_extern
   integer(C_LONG) :: c2
   BIND(C, name=’myVariable’) :: c2
   common /com/ r,s
   real(C_FLOAT) :: r,s,t
   BIND(C) :: /com/, /single/
   common /single/ t
end module LINK_TO_C_VARS

These can interoperate with the following C external variables:

int c_extern;
long myVariable;
struct {float r, s;} com;
float single;

In Fortran, a variable can access a global parameter by using the standard BIND C attribute. For example:

    MODULE Examp
      integer, BIND(C)::idata(20)
      real::rdata(10)
    END MODULE

In the above, two external variables are created (the latter with proper case and decoration): idata and foo_mp_rdata.

Use the BIND(C,name=<string>) attribute to resolve name discrepancies with C.

In C, a variable is declared global with the statement:

  int idata[20]; // declared as global (outside of any function)

Conversely, Fortran can declare the variable global (COMMON) and other languages can reference it as external:

 ! Fortran declaring PI global
  real pi_r
  COMMON /pi/ pi_r ! Common Block and variable names
  BIND(C)::/pi/

Use of BIND(C) above means that the name will be appropriately decorated for the target and made lowercase.

In C, the variable is referenced as an external with the statement:

  //C code with external reference to pi
  extern float pi;

Note that the global name C references is the name of the Fortran common block (pi), not the name of a variable within a common block (pi_r). Therefore, you cannot use blank common (unnamed) to make data accessible between C and Fortran.


Submit feedback on this help topic