我有一个接受int指针的C函数。

void setCoords(int *coords)


坐标是数组。

在Fortran中,我尝试使用以下方法调用此函数:

integer, dimension(10) :: coords
call setCoords(coords)


我收到一个编译错误,指出虚拟参数和实际参数的形状匹配规则已被违反。请注意,当我使用GNU编译器时,该代码可以毫无问题地进行编译。切换到Intel编译器后,出现此问题。

最佳答案

您需要定义一个interface,该参数将参数声明为C_PTR。英特尔Fortran 19.0 manual给出了以下使用原型调用C函数的示例

int C_Library_Function(void* sendbuf, int sendcount, int *recvcounts);


首先,定义一个模块,例如:

module ftn_C_2
       interface
         integer (C_INT) function C_Library_Function &
         (sendbuf, sendcount, recvcounts) &
            BIND(C, name='C_Library_Function’)
            use, intrinsic :: ISO_C_BINDING
            implicit none
            type (C_PTR), value :: sendbuf
            integer (C_INT), value :: sendcount
            type (C_PTR), value :: recvcounts
         end function C_Library_Function
       end interface
    end module ftn_C_2


然后,在调用函数中,如下使用它:

use, intrinsic :: ISO_C_BINDING, only: C_INT, C_FLOAT, C_LOC
    use ftn_C_2
    ...
    real (C_FLOAT), target :: send(100)
    integer (C_INT) :: sendcount
    integer (C_INT), ALLOCATABLE, target :: recvcounts(100)
    ...
    ALLOCATE( recvcounts(100) )
    ...
    call C_Library_Function(C_LOC(send), sendcount, &
    C_LOC(recvcounts))
    ...

关于c - 在参数为指针的fortran中调用C函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49827756/

10-11 22:09
查看更多