【发布时间】:2019-07-19 10:19:04
【问题描述】:
考虑以下 Fortran 代码,其中模块 Foo_mod 中的 C 互操作子例程 runFoo4C(...) bind(C, name="runFoo") 将 C 回调函数指针 getLogFuncFromC() 作为参数,
module CallbackInterface_mod
abstract interface
function getLogFunc4C_proc(ndim,Point) result(logFunc) ! bind(C)
use, intrinsic :: iso_c_binding, only : c_int32_t, c_double, c_int
integer(c_int32_t), intent(in) :: ndim
real(c_double), intent(in) :: Point(ndim)
real(c_double) :: logFunc
end function getLogFunc4C_proc
end interface
end module CallbackInterface_mod
!***********************************************************************************************************************************
!***********************************************************************************************************************************
module Foo_mod
interface
module subroutine runFoo4C(ndim, getLogFuncFromC, inputString, inputStringLen) bind(C, name="runFoo")
use, intrinsic :: iso_c_binding, only: c_int32_t, c_char, c_funptr, c_f_procpointer, c_size_t
use CallbackInterface_mod, only: getLogFunc4C_proc
implicit none
integer(c_int32_t) , intent(in) :: ndim
character(len=1, kind=c_char), dimension(*), intent(in) :: inputString
integer(c_size_t) , intent(in) :: inputStringLen
type(c_funptr), intent(in), value :: getLogFuncFromC
end subroutine runFoo4C
end interface
contains
subroutine runFoo(ndim, getLogFunc, string)
!use CallbackInterface_mod, only: getLogFunc_proc
use CallbackInterface_mod, only: getLogFunc4C_proc
use, intrinsic :: iso_fortran_env, only: RK => real64
implicit none
integer :: ndim
procedure(getLogFunc4C_proc) :: getLogFunc
character(*), intent(in) :: string
real(RK) :: Point(ndim)
character(:), allocatable :: mystring
Point = [1._RK,1._RK]
write(*,*) "Hi again, this is a call from inside runFoo!"
write(*,*) "getLogFunc(2,[1,1]) = ", getLogFunc(ndim,Point)
write(*,*) "string = ", string
end subroutine
end module Foo_mod
!***********************************************************************************************************************************
!***********************************************************************************************************************************
submodule (Foo_mod) Foo_smod
contains
module subroutine runFoo4C(ndim, getLogFuncFromC, InputString, inputStringLen) bind(C, name="runFoo")
use, intrinsic :: iso_c_binding, only: c_double, c_int32_t, c_char, c_funptr, c_f_procpointer, c_size_t
use CallbackInterface_mod, only: getLogFunc4C_proc
implicit none
integer(c_int32_t) , intent(in) :: ndim
character(len=1, kind=c_char), dimension(*), intent(in) :: InputString
integer(c_size_t) , intent(in) :: inputStringLen
type(c_funptr), intent(in), value :: getLogFuncFromC
procedure(getLogFunc4C_proc), pointer :: getLogFunc
real(c_double) :: Point(ndim)
character(:), allocatable :: inputString4tran
integer :: i
write(*,*) "InputString: ", InputString(1:inputStringLen)
allocate( character(len=inputStringLen) :: inputString4tran )
do i=1,inputStringLen
inputString4tran(i:i) = InputString(i)
end do
write(*,*) "inputString4tran: ", inputString4tran
! associate the input C procedure pointer to a Fortran procedure pointer
call c_f_procpointer(cptr=getLogFuncFromC, fptr=getLogFunc)
Point = [1._c_double, 1._c_double]
write(*,*) "Here we go: "
write(*,*) "getLogFunc(ndim=2, [1._c_double, 1._c_double]): ", getLogFunc( ndim, Point )
call runFoo(ndim, getLogFunc, inputString4tran)
end subroutine runFoo4C
end submodule Foo_smod
这个回调函数的抽象Fortran接口,由上面代码中模块CallbackInterface_mod中的getLogFunc4C_proc()给出。现在的问题:
这个抽象接口是否需要bind(c) 属性才能符合fortran 标准?我自己幼稚的猜测是它不需要bind(c),因为它不会在接口中使用函数的全局标识符来调用,但是抽象接口只是确定了C回调函数的接口,一个指向它被传递给 Fortran 以便稍后从 Fortran 内部调用。
确实,在抽象接口中注释掉这个bind(c) 属性不会导致任何使用ifort(18.0.2 Windows 编译器)的编译或运行时错误。
如果不需要,那么在这个抽象接口中如何声明变量呢?它们是否需要由 iso_c_binding 内在模块中的符合 C 的种类声明?
【问题讨论】:
-
这似乎是一个相当复杂的例子:如果你能简化它可能会使讨论更容易。
-
我同意。它直接取自我编写的一组示例。将尝试简化它。但核心问题是关于最顶部
CallbackInterface_mod模块的抽象接口中的bind(c)属性,以及注释掉bind(c)是否重要。整个代码是独立的,可以使用 Intel ifort 或 gfortran 进行编译。
标签: c fortran intel-fortran fortran-iso-c-binding