Next: , Previous: Derived Types and struct, Up: Interoperability with C


7.1.3 Interoperable Global Variables

Variables can be made accessible from C using the C binding attribute, optionally together with specifying a binding name. Those variables have to be declared in the declaration part of a MODULE, be of interoperable type, and have neither the pointer nor the allocatable attribute.

       MODULE m
         USE myType_module
         USE ISO_C_BINDING
         integer(C_INT), bind(C, name="_MyProject_flags") :: global_flag
         type(myType), bind(C) :: tp
       END MODULE

Here, _MyProject_flags is the case-sensitive name of the variable as seen from C programs while global_flag is the case-insensitive name as seen from Fortran. If no binding name is specified, as for tp, the C binding name is the (lowercase) Fortran binding name. If a binding name is specified, only a single variable may be after the double colon. Note of warning: You cannot use a global variable to access errno of the C library as the C standard allows it to be a macro. Use the IERRNO intrinsic (GNU extension) instead.