Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) );

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...