C回调函数的抽象Fortran接口是否需要bind(C)属性?

问题描述 投票:2回答:2

考虑以下Fortran代码,其中模块runFoo4C(...) bind(C, name="runFoo")中的C-可互操作子例程Foo_mod将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接口由getLogFunc4C_proc()在上面的代码中的模块CallbackInterface_mod中给出。现在的问题是:

这个抽象接口是否需要bind(c)属性才能符合fortran标准?我自己的天真猜测是它不需要bind(c),因为它不会被接口中的函数的全局标识符调用,但抽象接口只是确定C回调函数的接口,指向的是传递给Fortran稍后将从Fortran内部调用。

实际上,在抽象接口中注释掉这个bind(c)属性不会导致使用ifort(18.0.2 Windows编译器)的任何编译或运行时错误。

如果不需要,那么这个抽象接口中的变量声明怎么样?它们是否需要通过iso_c_binding内在模块中符合C的类型声明?

c fortran intel-fortran fortran-iso-c-binding
2个回答
2
投票

抽象接口中BIND(C)的存在(或不存在)会改变过程指针的特征,但是会以此程序不显示的方式进行更改。因为您通过从C_FUNPTR转换的指针调用getLogFunc,所以如果在抽象接口中省略BIND(C),则会阻止编译器注意到不匹配。例如,如果过程具有字符(*)参数,则会出现许多不匹配的错误。

BIND(C)本身在抽象接口中很好,只要你不说NAME =。由于它更改了过程的调用方式,因此必须在被调用过程可互操作时指定它。

关于“如果不需要,那么这个抽象接口中的变量声明怎么样?它们是否需要通过iso_c_binding内部模块中的符合C的类型来声明?”,你做出了在内部模块ISO_C_BINDING中将定义混为一谈的常见错误可互操作。该模块中的类型常量只是数字,没有什么神奇的。您需要在类型,种类和等级中匹配实际和伪参数(有一些例外。)


1
投票

Qazxswpoi的规范在Fortran 2008和Fortran 2018中有所不同。无论如何,让我们来看看声明

c_f_procpointer

在Fortran 2008下,call c_f_procpointer(cptr=getLogFuncFromC, fptr=getLogFunc) 参数的接口必须与fptr参数的目标互操作。要实现互操作,接口必须具有cptr属性。

在Fortran 2018下,这个要求是放宽的,但只有当bind参数是引用fptr的结果时,才允许我们为cptr参数建立一个非互操作的接口。有可能发生这种情况,这取决于c_funloc的调用方式。

在任何一种情况下,编译器都不需要诊断任何违反要求的行为(在最后的情况下,很容易看出它有多么棘手)。

© www.soinside.com 2019 - 2024. All rights reserved.