我的预期用途是
program main
use mod
external sub
call sub
end program main
subroutine sub
! code here calls subroutines in mod
end subroutine sub
具体来说,将module mod在范围内subroutine sub?另外,我有兴趣更广泛地了解模块何时在/超出范围。如果重要的话,我正在使用 gfortran 4.6.1。
它不在子程序 sub 的范围内,因为 sub 不能调用程序或使用 mod 中的变量,因为sub它不是程序的一部分main。它们没有共同点,是独立的编译单元,只能相互调用(如果它们是可调用的)。
考虑一下:
program main
external sub
call sub
end program main
subroutine sub
use mod
! code here calls subroutines in mod
end subroutine sub
mod在这里,您可以使用in 中的变量和例程sub,因为sub显式使用mod.
另一个例子,sub的内部过程在哪里main:
program main
use mod
call sub
contains
subroutine sub
! code here calls subroutines in mod
end subroutine sub
end program main
同样在这种情况下,您可以使用 in 中的内容,因为 frommod中的sub所有内容都main在sub.
最后,本案mod不在范围内,与原案类似。
program main
use mod
use mod2
call sub
end program main
module mod2
contains
subroutine sub
! code here calls subroutines in mod
end subroutine sub
end module mod2
另一个问题是模块变量超出范围时未定义。Fortran 2008 通过隐式生成所有模块变量来解决这个问题save。