通过FORTRAN 77函数C / C ++ [英] pass fortran 77 function to C/C++
问题描述
是否有可能通过FORTRAN 77函数作为一个回调函数指针,C / C ++?
如果是这样,怎么样?
Is it possible to pass fortran 77 function as a callback function pointer to C/C++? if so, how?
我在网上找到信息涉及FORTRAN 90以上,但我的遗产code基是77。
information I found on the web relates to fortran 90 and above, but my legacy code base is in 77.
非常感谢
推荐答案
如果它可以在FORTRAN 77来完成,这将是编译器和平台具体。新的ISO C 2003 Fortran语言的结合提供混合Fortran和C,以及随后或可以按照C的调用约定,如C ++任何语言的标准方法。虽然2003 Fortran语言的正式的一部分,同时也有极少Fortran编译器完全支持Fortran的2003的全部,在ISO C结合被无数的Fortran编译器95,包括gfortran,G95,阳光,ifort等支持所以我推荐使用这些Fortran 95的编译器和ISO C结合的方法,而不是找出某种方法为特定的方法之一。由于FORTRAN 77是Fortran 95的,为什么不与这些编译器的一个编译遗留code,使用Fortran 95的增加这项新功能的一个子集?
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?
我已经使用ISO C绑定称为从C的Fortran程序,而没有通过他们作为指针。它应该是可能的。的步骤是:
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)你声明Fortran函数与绑定(C)属性,
1) you declare the Fortran function with the Bind(C) attribute,
2)声明的所有使用特殊类型,如整型(c_int的)的参数,配合C型这一点。
2) you declare all of the arguments using special types, such as integer(c_int), that match the types of C.
步骤1安培; 2使Fortran函数的互通的用C
Steps 1 & 2 make the Fortran function interoperable with C.
3)你获得C-指针与Fortran的功能禀c_funloc这Fortran函数,指针值赋给类型c_funptr的指针。
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)在Fortran语言code,声明要函数指针传递到与接口的C例程,在宣布它的Fortran条件,但使用BIND(C)属性,可互操作的类型,以便该Fortran编译知道使用C-调用约定 - 让C例程互操作的Fortran
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.
然后当你调用了Fortran code中的C-程序,你可以通过它在步骤3中创建函数指针。
Then when you call the C-routine in the Fortran code, you can pass it the function pointer created in step 3.
更新:code例子:该Fortran主程序test_func_pointer将指针传递到Fortran函数my_poly给C例程C_Func_using_Func_ptr,并从C函数接收结果回来
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
和
float C_Func_using_Func_ptr (
float x,
float (*Func_ptr) (float y)
) {
return ( (*Func_ptr) (x) );
}
这篇关于通过FORTRAN 77函数C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!