2

我是 Fortran 初学者,我正在尝试采用一些 ifort 代码来使用 gfortran 进行编译。

我的函数有问题c_loc(),它在 ifort 中似乎接受动态数组,但 gfortran 编译因错误而停止:

错误:(1)处的参数“septr1”到“c_loc”必须是关联的标量指针

那么有谁知道如何调整以下 ifort 代码以使用 gfortran 进行编译?

integer(c_int), dimension(:), pointer  :: septr1=>null()
type(c_PTR) :: septr

allocate (septr1(10))
septr1 = 33
septr = c_loc(septr1)
4

2 回答 2

2

这似乎是旧 Fortran 2003 的要求,在 Fortran 2008 中放宽了。最近的 gfortran (5+) 接受了这一点。


您可以获取数组开始的位置,即 C 中偏移量为 0 的值。

septr = c_loc(septr1(1))

或者通常不是 1,而是 lbound(septr1)。

请参阅 Metcalf、Reid 和 Cohen 或 Fortran 标准中对 c_loc 参数的要求。


以普通 Fortran 方式通过引用传递数组而不是构造显式 c 指针通常要好得多。例如:

 call some_c_function(n,A)

其中 some_c_function 具有 Fortran 接口

 interface
  subroutine some_c_function(n,A) bind(C)
   use iso_c_binding,only: c_int,c_float    !you can use also import here

   integer(c_int),value      :: n
   real(c_float),dimension(n):: A
  end subroutine some_c_function
 end interface

对于 C 原型

 void some_c_function(int n, float* A)  //(hope so, I am not so good in C).
于 2012-02-21T09:06:52.610 回答
0

ifort 开发人员表示,Fortran 标准的规定不需要对 c_loc 进行标准符合性检查,包括其比传统 LOC() 更受限制的使用。可能考虑到这个内在函数在当前标准中的有用应用范围更广,以及需要限制需要测试的使用范围,大多数编译器将拒绝非标准使用。

于 2013-05-17T15:35:41.433 回答