发生
同样的行为因此,我可以建议是为了处理数据使用重载的子程序 - 那么你将有通用的行为,而不需要将维度参数明确地传递给函数(从而摆脱警告)。然后,我建议您遵循Holmz关于在测试阶段使用所有警告的建议,然后在生产构建期间将其完全关闭(-w)。现在我无法找到抑制此警告的有效方法(除了-w) - 似乎默认打开数组边界检查并且未覆盖-fno-bounds-check或-Wno-array -bounds。但重载函数可以更好的解决您的问题,实施应该是这样在这种情况下:
module functions
implicit none
interface test_dim
module procedure test_func1d, test_func2d, test_func3d
end interface ! test_dim
contains
subroutine test_func1d(input1d)
real, intent(in) :: input1d(:)
print*, "DOING 1 DIM"
print*, "SHAPE OF ARRAY:", shape(input1d)
end subroutine test_func1d
subroutine test_func2d(input2d)
real, intent(in) :: input2d(:,:)
print*, "DOING 2 DIM"
print*, "SHAPE OF ARRAY:", shape(input2d)
end subroutine test_func2d
subroutine test_func3d(input3d)
real, intent(in) :: input3d(:,:,:)
print*, "DOING 3 DIM"
print*, "SHAPE OF ARRAY:", shape(input3d)
end subroutine test_func3d
end module functions
program test_prog
use functions
implicit none
real :: case1(10), case2(20,10), case3(30, 40, 20)
call test_dim(case1)
call test_dim(case2)
call test_dim(case3)
end program test_prog
并通过此功能产生的输出是这样的:
DOING 1 DIM
SHAPE OF ARRAY: 10
DOING 2 DIM
SHAPE OF ARRAY: 20 10
DOING 3 DIM
SHAPE OF ARRAY: 30 40 20
将gfortran与-fbounds-check设置为默认值并不常见。您的编译时检查是您应该在源代码中修复的另一个问题。 – tim18
我没有构建gfortran,我使用Ubuntu 16.04上的'sudo apt-get install gfortran'来安装它。当然,这表明源代码必须有一些东西。我会尝试删除gfortran并重新安装它 - 这没有什么区别。 – 1QuickQuestion
它可能不会在编译时压制检查。除了增加运行时间之外,边界检查总是很好用。编译器和计算机现在如此之快,为什么会担心增加编译时间? –