我是Fortran初学者,我正在尝试使用一些ifort代码与gfortran进行编译。
我有c_loc()
函数的问题,在ifort中似乎接受动态数组,但是gfortran编译停止并出现错误:
错误:(1)处的'septr1'到'c_loc'的参数必须是关联的标量POINTER
那么有谁知道如何使用gfortran调整以下ifort代码进行编译?
integer(c_int), dimension(:), pointer :: septr1=>null()
type(c_PTR) :: septr
allocate (septr1(10))
septr1 = 33
septr = c_loc(septr1)
答案 0 :(得分: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
for C prototype
void some_c_function(int n, float* A) //(hope so, I am not so good in C).
答案 1 :(得分:0)
ifort开发人员表示,Fortran标准的规定并不要求对c_loc进行标准合规性检查,包括其比传统LOC()更严格的使用。 可能考虑到当前标准中这种内在功能的有用应用的更广泛范围,以及限制需要测试的使用范围的需要,大多数编译器将拒绝非标准用法。