Gfortran 对接口有点过于谨慎。它不仅想知道参数的类型、种类、秩和数量,还想知道形状、目标属性和意图(尽管我同意意图部分)。我遇到了类似的问题。
使用 gfortran,有三种不同的维度定义:
1. 固定
2. 变量
3. 假设大小
对于 ifort,类别 1 和 2 被视为相同,因此您可以在接口中将任何维度大小定义为 0,它就能正常工作。
program test
implicit none
integer, dimension(:), allocatable :: ownlist
interface
subroutine blueprint(sz,arr)
integer, intent(in) :: sz
integer, dimension(0), intent(in) :: arr
end subroutine blueprint
end interface
procedure(blueprint), pointer :: ptr
allocate(ownlist(3))
ownlist = (/3,4,5/)
ptr => rout1
call ptr(3,ownlist)
deallocate(ownlist)
allocate(ownlist(0:10))
ownlist = (/3,4,5,6,7,8,9,0,1,2,3/)
ptr => rout2
call ptr(3,ownlist)
deallocate(ownlist)
contains
subroutine rout1(sz,arr)
implicit none
integer, intent(in) :: sz
integer, dimension(sz), intent(in) :: arr
write(*,*) arr
write(*,*) arr(1)
end subroutine rout1
subroutine rout2(sz,arr)
implicit none
integer, intent(in) :: sz
integer, dimension(0:10), intent(in) :: arr
write(*,*) "Ignored integer: ",sz
write(*,*) arr
write(*,*) arr(1)
end subroutine rout2
end program test
Gfortran 抱怨接口。将0更改为“ sz”可以解决'rout1'的问题,但无法解决'rout2'的问题。
然而,您可以欺骗gfortran并说dimension(0:10 + 0 * sz)而不是dimension(0:10),gfortran编译并提供与ifort相同的结果。
这是一个愚蠢的技巧,它依赖于可能不存在的整数“ sz”的存在。另一个程序:
program difficult_test
implicit none
integer, dimension(:), allocatable :: ownlist
interface
subroutine blueprint(arr)
integer, dimension(0), intent(in) :: arr
end subroutine blueprint
end interface
procedure(blueprint), pointer :: ptr
allocate(ownlist(3))
ownlist = (/3,4,5/)
ptr => rout1
call ptr(ownlist)
deallocate(ownlist)
allocate(ownlist(0:10))
ownlist = (/3,4,5,6,7,8,9,0,1,2,3/)
ptr => rout2
call ptr(ownlist)
deallocate(ownlist)
contains
subroutine rout1(arr)
implicit none
integer, dimension(3), intent(in) :: arr
write(*,*) arr
write(*,*) arr(1)
end subroutine rout1
subroutine rout2(arr)
implicit none
integer, dimension(0:10), intent(in) :: arr
write(*,*) arr
write(*,*) arr(1)
end subroutine rout2
end program difficult_test
这个在ifort下可以工作,原因和之前的例子一样,但是gfortran会抱怨接口。我不知道该如何修复它。
我想告诉gfortran的唯一事情是“我还不知道维度大小,但我们会解决它”。但这需要一个备用整数参数(或其他可以转换为整数的东西)来欺骗gfortran。
reshape
内在解决方案。 - steabert