Search code examples
c++cfortranfortran77fortran-iso-c-binding

pass fortran 77 function to C/C++


Is it possible to pass fortran 77 function as a callback function pointer to C/C++? if so, how?

information I found on the web relates to fortran 90 and above, but my legacy code base is in 77.

many thanks


Solution

  • If it can be done in FORTRAN 77, it will be compiler and platform specific. The new ISO C Binding of Fortran 2003 provides a standard way of mixing Fortran and C, and any language that follows or can follow the calling conventions of C, such as C++. While formally a part of Fortran 2003, and while there are extremely few Fortran compilers that fully support the entirety of Fortran 2003, the ISO C Binding is supported by numerous Fortran 95 compilers, including gfortran, g95, Sun, ifort, etc. So I recommend using one of these Fortran 95 compilers and the ISO C Binding method rather than figuring out some method for a particular method. Since FORTRAN 77 is a subset of of Fortran 95, why not compile your legacy code with one of these compilers, using Fortran 95 to add this new feature?

    I have called Fortran procedures from C using the ISO C Binding, but haven't passed them as pointers. It should be possible. The steps are:

    1) you declare the Fortran function with the Bind(C) attribute,

    2) you declare all of the arguments using special types, such as integer(c_int), that match the types of C.

    Steps 1 & 2 make the Fortran function interoperable with C.

    3) You obtain a C-pointer to this Fortran function with the Fortran instrinsic function "c_funloc", assigning the pointer value to a pointer of type "c_funptr".

    4) In the Fortran code, you declare the C routine that you want to pass the function pointer to with an Interface, declaring it in in Fortran terms, but using the Bind(C) attribute and interoperable types so that the Fortran compiler knows to use the C-calling convention -- making the C routine interoperable with Fortran.

    Then when you call the C-routine in the Fortran code, you can pass it the function pointer created in step 3.

    UPDATE: Code example: The Fortran main program "test_func_pointer" passes a pointer to the Fortran function "my_poly" to the C routine "C_Func_using_Func_ptr" and receives the result back from that C function.

    module func_pointer_mod
    
       use, intrinsic :: iso_c_binding
    
       implicit none
    
       interface C_func_interface
    
          function C_Func_using_Func_ptr ( x, Func_ptr ) bind (C, name="C_Func_using_Func_ptr")
    
             import
    
             real (c_float) :: C_Func_using_Func_ptr
             real (c_float), VALUE, intent (in) :: x
             type (c_funptr), VALUE, intent (in) :: Func_ptr
    
          end function C_Func_using_Func_ptr
    
       end interface C_func_interface
    
    
    contains
    
       function my_poly (x) bind (C, name="my_poly")
    
          real (c_float) :: my_poly
          real (c_float), VALUE, intent (in) :: x
    
          my_poly = 2.0 * x**2 + 3.0 * x + 5.0
    
          return
    
       end function my_poly
    
    end module func_pointer_mod
    
    
    program test_func_pointer
    
       use, intrinsic :: iso_c_binding
    
       use func_pointer_mod
    
       implicit none
    
       type (c_funptr) :: C_func_ptr
    
       C_func_ptr = c_funloc ( my_poly )
    
       write (*, *) C_Func_using_Func_ptr ( 2.5_c_float, C_func_ptr )
    
       stop
    
    end program test_func_pointer
    

    and

    float C_Func_using_Func_ptr (
    
       float x,
       float (*Func_ptr) (float y)
    
    ) {
    
       return ( (*Func_ptr) (x) );
    
    }