将fortran 77函数传递给C/C++

Any*_*orn 4 c c++ fortran fortran77 fortran-iso-c-binding

是否可以将fortran 77函数作为回调函数指针传递给C/C++?如果是这样,怎么样?

我在网上找到的信息与fortran 90及以上相关,但我遗留的代码库是77.

非常感谢

M. *_* B. 6

如果它可以在FORTRAN 77中完成,它将是编译器和平台特定的.Fortran 2003的新ISO C绑定提供了混合Fortran和C的标准方法,以及遵循或遵循C的调用约定的任何语言,例如C++.虽然正式成为Fortran 2003的一部分,虽然极少数Fortran编译器完全支持Fortran 2003的全部版本,但ISO C Binding仍受到众多Fortran 95编译器的支持,包括gfortran,g95,Sun,ifort等.所以我建议使用这些Fortran 95编译器之一和ISO C绑定方法,而不是找出特定方法的某些方法.由于FORTRAN 77是Fortran 95的子集,为什么不用这些编译器中的一个编译遗留代码,使用Fortran 95添加这个新功能?

我使用ISO C Binding从C调用Fortran过程,但是没有将它们作为指针传递.它应该是可能的.步骤是:

1)使用Bind(C)属性声明Fortran函数,

2)使用与C类型匹配的特殊类型(例如整数(c_int))声明所有参数.

步骤1和2使Fortran功能与C 可互操作.

3)使用Fortran内部函数"c_funloc"获取指向此Fortran函数的C指针,将指针值分配给类型为"c_funptr"的指针.

4)在Fortran代码中,您声明要将函数指针传递给接口的C例程,以Fortran术语声明它,但使用Bind(C)属性和可互操作类型,以便Fortran编译器知道使用C调用约定 - 使C例程与Fortran可互操作.

然后,当您在Fortran代码中调用C例程时,可以将其传递给在步骤3中创建的函数指针.

更新:代码示例:Fortran主程序"test_func_pointer"将指向Fortran函数"my_poly"的指针传递给C例程"C_Func_using_Func_ptr",并从该C函数接收结果.

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
Run Code Online (Sandbox Code Playgroud)

float C_Func_using_Func_ptr (

   float x,
   float (*Func_ptr) (float y)

) {

   return ( (*Func_ptr) (x) );

}
Run Code Online (Sandbox Code Playgroud)