Use, intrinsic :: iso_c_binding, only : c_int ! so we need to use iso_c_binding to get c_int definition ! Interface blocks don't know about their context, Integer(c_int) function how_many_geese(flock_num) bind(C, 'howManyGeese') Geese.h // Count how many geese are in a given flock The bind attribute is used to tell the compiler the name of the function in C, which may be different to the Fortran name. This is essentially equivalent to the C function prototype, and lets the compiler know about the number and type of the arguments, etc. To actually call a C function from Fortran, first the interface must be declared. Interoperability of C char and Fortran characters is probably a topic for itself and so not discussed here Use of C kind type parameters guarantees that the data can be transferred between C and Fortran programs. Real(c_float) :: bar ! equivalent of 'float bar' in C ![]() Iso_c_binding gives access to interoperable kind type parameters: integer(c_int) :: foo ! equivalent of 'int foo' in C The intrinsic keyword here ensures the correct module is used, and not a user created module of the same name. These features are mostly accessed through the intrinsic module iso_c_binding: use, intrinsic :: iso_c_binding Fortran 2003 introduced language features which can guarantee interoperability between C and Fortran (and to more languages by using C as an intermediary).
0 Comments
Leave a Reply. |