F90_CALLS_C
FORTRAN90 Program Calls C Function


F90_CALLS_C is a directory which demonstrates how a FORTRAN90 program can call a C function in a way that is "guaranteed" to work; in other words, the procedure follows rules laid down by the FORTRAN standard, and does not depend on any special or peculiar features of the FORTRAN and C compilers used to compile the programs.

Some reference books discuss this topic by showing little isolated pieces of code, which do not form a real program, and cannot actually be compiled or used. Since EVERYTHING has to be correctly written and working properly together to make this delicate operation happen, it is very important to have an entire calculation in mind, and to be able to examine the full FORTRAN and C source code, as well as the compile and load statements used.

The KRONROD example presented here involves a FORTRAN90 main program and a library of 4 C routines. For comparison, you can also look at a directory where the same calculation is done with the main program and library routines written in the same language. Simply go to the KRONROD directory for the language you are interested in.

A FORTRAN90 program, subroutine, or function that will call a C function might try using the ISO C binding module. This was actually introduced as part of FORTRAN 2003, but your compiler may be willing to let your FORTRAN90 program access it. (If not, you might consider moving to FORTRAN 2003!). The ISO C bindings are made available by the statement:

        use iso_c_binding
      
You can also use fussier versions of this statement, such as
        use, intrinsic :: iso_c_binding
      
or
        use, intrinsic :: iso_c_binding, only : C_CHAR, C_NULL_CHAR
      
(Thanks to Alan Richardson for pointing out that the ISO C bindings were only added to the language in the 2003 definition of FORTRAN!)

Once you have the C bindings, you need to define an interface to your C function, which might read:

  interface
    subroutine kronrod ( n, eps, x, w1, w2 ) bind ( c )
      use iso_c_binding
      integer ( c_int ), VALUE :: n
      real ( c_double ), VALUE :: eps
      real ( c_double ) :: x(*)
      real ( c_double ) :: w1(*)
      real ( c_double ) :: w2(*)
    end subroutine kronrod
  end interface
      

Finally, to guarantee that FORTRAN and C agree on data types, you should declare any FORTRAN90 variables that will be passed through the C interface with statements like this, which essentially specify the appropriate KIND parameter to guarantee compatibility:

  integer ( c_int ), parameter :: n = 3
  real ( c_double ) eps
  real ( c_double ) x(n+1)
  real ( c_double ) w1(n+1)
  real ( c_double ) w2(n+1)
      

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

F90_CALLS_C is available in a FORTRAN90 version

Related Data and Programs:

C_CALLS_F77, C programs which call a FORTRAN77 subroutine.

C_CALLS_F90, C programs which call a FORTRAN90 subroutine.

C++_CALLS_F77, C++ programs which call a FORTRAN77 subroutine.

C++_CALLS_F90, C++ programs which call a FORTRAN90 subroutine.

F77_CALLS_C, FORTRAN77 programs which call a C function.

F77_CALLS_C++, FORTRAN77 programs which call a C++ function.

F90_CALLS_C_AND_MPI, FORTRAN90 programs which call a C function while executing under the MPI parallel programming environment.

F90_CALLS_C++, FORTRAN90 programs which call a C++ function.

F90_CALLS_MATLAB, FORTRAN90 programs which issue a call to MATLAB to carry out an auxillary calculation.

MATLAB_CALLS_C, MATLAB programs which illustrate how C functions can be written, compiled, and called from MATLAB using the MEX facility;

MATLAB_CALLS_F77, MATLAB programs which call a FORTRAN77 function, using MATLAB's MEX facility.

MIXED, C programs which call a function written in another programming language.

MIXED, C++ programs which call a function written in another programming language.

MIXED, FORTRAN77 programs which call a function written in another programming language.

MIXED, FORTRAN90 programs which call a function written in another programming language.

Reference:

Source Code:

The HELLO example involves a FORTRAN90 main program which calls directly the C function "print_C". The C function prints a string which is passed from the FORTRAN90 program. Notice that, in the output file, the output from the C function appears BEFORE the output from the FORTRAN main program.

The KRONROD example involves a FORTRAN90 main program which calls directly the C functions "kronrod" and "timestamp".

You can go up one level to the FORTRAN90 source codes.


Last revised on 18 July 2012.