Fortran 90/95中的模块何时超出范围? [英] When does a module go out of scope in Fortran 90/95?
问题描述
我的预期用途是:
My intended use is
program main
use mod
external sub
call sub
end program main
subroutine sub
! code here calls subroutines in mod
end subroutine sub
具体来说, module mod
位于子程序子
范围内?另外,当模块处于或不在示波器范围内时,我有兴趣知道更一般的情况。我使用的是gfortran 4.6.1,如果它很重要。
Specifically, will module mod
be in scope in subroutine sub
? Also, I'd be interested to know more generally when a module is in/out of scope. I'm using gfortran 4.6.1, if it matters.
推荐答案
它不在subroutine sub的作用域中,因为sub不能调用例程或使用mod中的变量,因为 sub
不是程序 main
的一部分。它们没有共同之处,是单独的编译单元,只能互相调用(如果它们是可调用的)。
It is not in scope of subroutine sub, as sub cannot call routines or use variables from mod, because sub
is not part of the program main
. They have nothing in common, are separate compilation units and only may call each other (if they are callable).
考虑这一点:
Consider this:
program main
external sub
call sub
end program main
subroutine sub
use mod
! code here calls subroutines in mod
end subroutine sub
在这里,你可以使用变量和例程从 mod
在 sub
中,因为 sub
显式使用 mod
。
Here, you can use variables and routines from mod
in sub
, because sub
explicitly uses mod
.
另一个例子,其中 sub
是一个内部过程 main
:
Another example, where sub
is an internal procedure of main
:
program main
use mod
call sub
contains
subroutine sub
! code here calls subroutines in mod
end subroutine sub
end program main
同样在这种情况下,你可以在 sub
中使用 mod
中的所有内容,因为从 main
在 sub
中。
Also in this case you can use things from mod
in sub
because everything from main
is in scope in sub
.
最后,在这种情况下 mod
不在范围内,它与原始大小写相似。
Finally, in this case mod
is not in scope, it is similar to the original case.
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
。
Another issue is the undefining of module variables, when they go out of scope. Fortran 2008 solved this by making all module variables implicitly save
.
这篇关于Fortran 90/95中的模块何时超出范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!