【问题标题】:Passing a two dimentional array from Fortran to C将二维数组从 Fortran 传递到 C
【发布时间】:2014-12-20 17:43:09
【问题描述】:

我在将二维数组从 Fortran 传递到 C 时遇到问题。以下是我的 C 函数,它只在屏幕上显示数组元素。

#include <stdio.h>
void print2(double *arr , int *n)
{
    int y = *n;
    printf("\n y = %d", y);
    for(int i =0; i<y; i++)
    {
          for (int j = 0; j < y; j++)
              printf("%.6g", *((arr + i*y) + j));
          printf("\n");
    }
}

到目前为止,我的 Fortran 代码如下:

program linkFwithC
    use, intrinsic :: iso_c_binding
    implicit none
    real, dimension(3,3)::a
    a(1,1)=1
    a(1,2)=2
    a(1,3)=3
    a(2,1)=4
    a(2,2)=5
    a(2,3)=6
    a(3,1)=7
    a(3,2)=8
    a(3,3)=9

    interface
        subroutine print2(a,n) bind( c )
        use, intrinsic :: iso_c_binding
        type(c_ptr)::a
        integer(C_INT)::n
        end subroutine print2   
    end interface

    call print2(c_loc(a),3)
end program linkFwithC

我链接这两个文件的方式是为 C 函数创建一个静态库并构建 .lib 文件。构建 .lib 文件后,我将其添加到 fortran 项目并运行 fortran 项目。代码运行无误,n值显示正确;但是,显示的数组值都是错误的。

请帮忙!

谢谢, 阿纳斯

【问题讨论】:

  • 你在哪里调用 print2(不是 fortran 专家)。同样最好先在你的c函数中打印出n,如果它是0那么就没有输出
  • 是真的,我刚加了调用就报了如下两个错误:
  • 我没有看到任何调用 print2(a,3) ?
  • 在 fortran 4 字节中不是“真实”,在 C 8 字节中不是“双”吗?
  • 你需要在你的接口块中声明 print2 的参数。

标签: c arrays fortran fortran-iso-c-binding


【解决方案1】:

[当前]显示的代码中存在一些问题。

  • print2 的 Fortran 接口中的 n 参数没有 VALUE 属性,但 C 函数中的相应参数是按值获取的。考虑将 VALUE 添加到 Fortran 声明中。

  • 指向数组的指针也会出现同样的问题。 Fortran 接口传递一个没有值的指针,C 函数需要一个“按值指针”(而不是指向指针的指针)。请注意,此处无需显式使用 C_PTR - 您可以使用数组的实际类型构造可互操作的接口。

  • 在大多数平台上,Fortran 默认 REAL 与 C double 不同 - 考虑使用 ISO_C_BINDING 中的种类常量,以确保 Fortran 端 REAL 的种类与 C 的种类相匹配。

  • C_LOC 要求其参数具有 TARGET 属性。将该属性添加到主程序中 a 变量的声明中。

【讨论】:

  • 您能否详细说明您关于 C_LOC 的最后一点。我在代码中的意图是将数组 a 的第一个元素的地址传递给 C 函数。
【解决方案2】:

经过进一步研究,我找到了解决此问题的方法,如下所示:

以下是我的 C 函数:

#include <stdio.h>
void print2(void *p, int n) {
  printf("Array from C is \n");
  double *dptr;
  dptr = (double *)p;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j<n; j++)
      printf("%.6g \t",dptr[i*n+j]);
    printf("\n");
  }
}

以下是我的 Fortran 代码:

program linkFwithC
use iso_c_binding
implicit none 
interface
  subroutine my_routine(p,r) bind(c,name='print2')
    import :: c_ptr
    import :: c_int
    type(c_ptr), value :: p
    integer(c_int), value :: r
  end subroutine
end interface


integer,parameter ::n=3
real (c_double), allocatable, target :: xyz(:,:)
real (c_double), target :: abc(3,3)
type(c_ptr) :: cptr
allocate(xyz(n,n))
cptr = c_loc(xyz(1,1))

!Inputing array valyes

xyz(1,1)= 1
xyz(1,2)= 2
xyz(1,3)= 3
xyz(2,1)= 4
xyz(2,2)= 5
xyz(2,3)= 6
xyz(3,1)= 7
xyz(3,2)= 8
xyz(3,3)= 9


call my_routine(cptr,n)
deallocate(xyz)
pause
end program linkFwithC

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 2013-02-19
    • 2014-01-06
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多