模块外部的Fortran模块过程 [英] Fortran module procedure outside module
问题描述
我在Google中什么也没找到,所以...
I found nothing in Google, so...
我尝试使用Fortran 90的所有面向对象功能,并为某些特定工作(例如C ++中的类)创建模块.我写了这样的东西:
I try to use all objected-oriented features of Fortran 90 and create module for some specific work (like class in C++). I have written something like this:
module test
integer, PRIVATE :: dummy
PUBLIC :: sub
contains
subroutine sub()
dummy = 1
end subroutine sub
end module test
但是,我大约有10个这样的子例程,将它们全部放入一个文件中是一个非常糟糕的主意.是否可以告诉编译器sub
是模块子例程,但是将其放置在另一个文件中?因为,看起来只有在我定义sub
的正文的情况下,此代码才会被编译.
But, I have about 10 such subroutines and it's very bad idea to place them all into one file. Is it possible to tell compiler, that sub
is module subroutine, but place it in another file? Because, it looks like this code will be compiled only if I define body of sub
there.
推荐答案
作为您问题的直接答案-是的-您可以使用INCLUDE
行引用文件中包含sub
定义的文件包含模块其余部分定义的文件(具有子例程代码的文件的include行将出现在模块中的CONTAINS
语句之后.
As a direct answer to your question - yes - you can use a INCLUDE
line to reference a file that contains the definition of sub
from within the file that contains the definition of the rest of the module (the include line for the files with the subroutine code would come after the CONTAINS
statement in the module.
MODULE test
INTEGER, PRIVATE :: dummy
PUBLIC :: sub
CONTAINS
INCLUDE 'sub.f90'
END MODULE test
! sub.f90
SUBROUTINE sub
dummy = 1
END SUBROUTINE sub
但是,这是非典型的安排.此外,如果将它们全部放入一个文件中是一个非常糟糕的主意",那么从一个模块中获得大概不同的子例程是一个好主意(或必要)吗?
However, this is an atypical arrangement. Further, if it is a "very bad idea to place them all into one file", then is it a good idea (or necessary) to have the presumably disparate subroutines available from the one module?
更传统的F90解决方案是将共享数据(虚拟)作为公共实体放置在一个低级"模块中,以将子例程定义放入使用该低级模块的一系列中级"模块中根据需要,然后是使用中间模块并提供对客户端代码的集体导出的最终高级"模块.然后,使用源代码约定而不是语言规则来避免客户端代码直接使用中级和低级模块.
The more conventional F90 solution is for the shared data (dummy) to be placed in one "low level" module as public entities, to place the subroutine definitions into a series of "intermediate level" modules that use the low level module as required, and then for a final "high level" module that uses the intermediate modules and provide a collective export to client code. Source code conventions, rather than language rules, are then used to avoid client code from directly using the intermediate and low level modules.
MODULE low_level_shared
IMPLICIT NONE
INTEGER, PUBLIC :: dummy
END MODULE low_level_shared
MODULE intermediate_sub1
IMPLICIT NONE
CONTAINS
SUBROUTINE sub1
USE low_level_shared
dummy = 1
END SUBROUTINE sub1
END MODULE intermediate_sub1
MODULE intermediate_sub2
IMPLICIT NONE
CONTAINS
SUBROUTINE sub2
USE low_level_shared
...
END SUBROUTINE sub2
END MODULE intermediate_sub2
MODULE high_level
USE intermediate_sub1
USE intermediate_sub2
PRIVATE
PUBLIC :: sub1, sub2
END MODULE high_level
PROGRAM client_code
USE high_level
...
CALL sub1
END PROGRAM client_code
Fortran 2008允许通过子模块功能进行其他结构和控制,其中可以在模块程序实现的单独程序单元中定义模块过程接口.
Fortran 2008 allows additional structures and control through the submodule feature, where module procedures interfaces can be defined in a separate program unit to the module procedure implementation.
这篇关于模块外部的Fortran模块过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!