菊花链使用Fortran模块的语句
我的问题的上下文并不容易描述,所以我有充分的理由尝试构造一个DLL F90项目,以便-菊花链使用Fortran模块的语句,fortran,Fortran,我的问题的上下文并不容易描述,所以我有充分的理由尝试构造一个DLL F90项目,以便- 我有一些低级模块(特点是很少或没有 依赖其他模块) 一些中级模块(即 主要使用低层数据执行中间计算 模块)和 一些高级模块(和主DLL)可以 将这些计算结果汇编成输出 我的目标结构的简化版本将以以下内容结束: 模块1(无依赖项) 模块2(无依赖项) 模块3(使用模块1) 模块4(使用模块2) 模块5(使用模块1参数和模块3方法) module6(使用module2参数和module4方法) 主DLL代码
- 我有一些低级模块(特点是很少或没有 依赖其他模块)
- 一些中级模块(即 主要使用低层数据执行中间计算 模块)和
- 一些高级模块(和主DLL)可以 将这些计算结果汇编成输出
- 模块1(无依赖项)
- 模块2(无依赖项)
- 模块3(使用模块1)
- 模块4(使用模块2)
- 模块5(使用模块1参数和模块3方法)
- module6(使用module2参数和module4方法)
- 主DLL代码(使用上述所有模块)
模块5
即可访问:
- 在
自身中声明的任何模块实体,标记为module5
或public
protected
- 在
中通过使用关联访问的任何实体,由module5
标记为module5
(实际上,如果未指定,则public是默认的可访问性)。如果实体通过使用关联从public
或module1
访问,但被module3
标记为module5
,则该实体将无法访问private
module module0
! Utiliy module with global constants, could be used at any level.
character(2) :: w = 'w0'
end
module module1
! Low level module. Exposes all the symbols that are not marked as private,
! both defined locally or accessed by use from 'module0'.
use :: module0
private :: z
character(2) :: x1 = 'x1', y1 = 'y1', z = 'z1'
! defined entities: w, x1, y1, z
! public entities : w, x1, y1
end
module module2
! Also low level module, but default access modifier was changed to private,
! so it exposes only symbols marked as public ('z' isn't).
use :: module0
public :: w, x2, y2
private
character(2) :: x2 = 'x2', y2 = 'y2', z = 'z2'
! defined entities: w, x2, y2, z
! public entities : w, x2, y2
end
module module3
! Uses all public names from 'module1' (including 'w' that is from 'module0'),
! but only exposes some of them. Also, defines and exposes local symbols.
use :: module1
private :: x3, y1
character(2) :: x3 = 'x3', y3 = 'y3'
! defined entities: w, x1, y1, x3, y3
! public entities : w, x1, y3
end
module module4
! Here, only selected symbols are accessed from 'module2', and 'w' has been
! renamed into 'w2' to avoid name-conflict with locally defined name 'w'
! (a compile error would had been thrown).
use :: module2, only: w2 => w, x2
public :: w, x2, y4
private
character(2) :: w = 'w4', x4 = 'x4', y4 = 'y4'
! defined entities: w, w2, x2, x4, y4
! public entities : w, x2, y4
end
module module5
! This module can use symbols from lowest level modules that are made public
! by 'module3', without explicitly using those modules.
use :: module3
character(2) :: z = 'z5'
! defined entities: w, x1, y3, z
! public entities : w, x1, y3, z
end
module module6
! As 'y2' is not exposed by 'module4', we could have access to it by using
! 'module2' directly. There is no name-conflict between 'w' from 'module0'
! and from 'module2' because both relate to the same entity. There would be
! conflict with 'w' from 'module4' though, hence the rename.
use :: module0
use :: module2
use :: module4, w4 => w
public :: w, x2, y4, z
private
character(2) :: z = 'z6'
! defined entities: w, w4, x2, y2, y4, z
! public entities : w, x2, y4, z
end
我强烈建议您尽可能使用显式导入,因为它使代码更加清晰,避免名称冲突。作为一般规则,尝试在模块中为公共实体使用区分名称,或在use语句中使用rename子句
这是前面模块的使用示例:
program main
! There aren't namespaces in Fortran (yet), so attention shall be paid when
! naming symbols meant to be accessed by use association, to avoid conflicts.
! That's why explicit imports are encouraged, as well as implicit none.
use :: module5
use :: module6, z6 => z
implicit none
character(2) :: v = 'v#'
call test_a
call test_b
call test_c
contains
subroutine test_a
! You can access used and defined names in procedures by host association.
print '(5a3)', v, w, x1, y3, z ! output: v# w0 x1 y3 z5
print '(5a3)', v, w, x2, y4, z6 ! output: v# w0 x2 y4 z6
end
subroutine test_b
! Also, you can use modules locally inside procedures. In this case, local
! names "shadow" host associated names, as with 'z' from 'module6' here.
use :: module6
print '(5a3)', v, w, x2, y4, z ! output: v# w0 x2 y4 z6
end
subroutine test_c
! There is no name-conflict error between host and local symbols; the local
! definition (or use association) always "shadows" the host associated name.
use :: module4, only: w
character(2) :: v = 'v_', z = 'z_'
print '(5a3)', v, w, x1, y3, z ! output: v_ w4 x1 y3 z_
end
end
为完整起见,我要提到的是,包含了一个名为“从模块访问的实体的默认可访问性”的新功能,该功能允许您将模块名称放入
public
或private
语句中,并将所述可访问性修饰符应用于该模块中使用的所有实体
如果模块a使用模块b,则它的默认可访问性
来自b的访问是公共的。为每个组件指定另一个可访问性
实体是笨拙和容易出错的。现在可以使用
一个模块,包含在公开的实体名称列表中
或在公开或私下的声明中。这将设置默认值
从该模块访问的所有实体
我今天(2018年)所知道的唯一一个包含此功能的编译器是。我很想说这是的一个副本。概念是完全不同的,但理解其中所说的内容将使答案变得清晰。请阅读这篇文章和答案,如果没有帮助,请告诉我们。