F90_CALLS_C
FORTRAN90 code Calls C Function


F90_CALLS_C is a directory which demonstrates how a FORTRAN90 code 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 codes.

Some reference books discuss this topic by showing little isolated pieces of code, which do not form a real code, 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 code and a code of 4 C routines. For comparison, you can also look at a directory where the same calculation is done with the main code and code routines written in the same language. Simply go to the KRONROD directory for the language you are interested in.

A FORTRAN90 code, 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 code 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 codes:

C_CALLS_F77, C codes which call a FORTRAN77 subroutine.

C_CALLS_F90, C codes which call a FORTRAN90 subroutine.

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

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

F77_CALLS_C, FORTRAN77 codes which call a C function.

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

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

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

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

f90_stop_test, FORTRAN90 codes which illustrate how a FORTRAN90 code can print a text message or return a numeric code status value to the calling environment when executing the STOP statement.

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

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

MIXED, C codes which call a function written in another codeming language.

MIXED, C++ codes which call a function written in another codeming language.

MIXED, FORTRAN77 codes which call a function written in another codeming language.

MIXED, FORTRAN90 codes which call a function written in another codeming language.

Reference:

Source Code:

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

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


Last revised on 18 July 2012.