在单独的文件中构造Fortran派生类型
我有以下程序在单独的文件中构造Fortran派生类型,fortran,Fortran,我有以下程序 program main type mytype integer :: a integer :: b end type type(mytype), dimension(10,10) :: data common data ... rest of source code ... end program 我想将类型mytype定义移动到一个单独的文件中,但该类型将在所有子例程中使用。如何才能做到这一点?我必须把类型定义放在同一个位置吗 对于定义为公共的数据数组,是否可
program main
type mytype
integer :: a
integer :: b
end type
type(mytype), dimension(10,10) :: data
common data
... rest of source code ...
end program
我想将类型mytype定义移动到一个单独的文件中,但该类型将在所有子例程中使用。如何才能做到这一点?我必须把类型定义放在同一个位置吗
对于定义为公共的数据数组,是否可以将此类定义放在单独的文件中
目标是这样的(在C中),即将所有全局数据分组到一个文件中,将所有类型定义分组到一个头中
main.c
#包括
#包括
main(){
...
}
global.c
#包括
结构mytype数据[10][10];
自定义类型.h:
struct myType {
int a;
int b;
};
global.h:
struct myType {
int a;
int b;
};
extern struct mytype data[10][10];
模块是计划以所需方式使用的程序单元。事实上,Fortran标准将其描述为(F2018,14.2.1):
模块包含声明、规范和定义。通过使用关联,其他程序单元可以访问模块实体的公共标识符
模块类似于:
module module_name
implicit none
! type, interface and object definitions
contains
! module procedure definitions
end module module_name
除了这个基本结构之外,模块还有很多内容(当然,隐式无
只是可选的),但是这里有一本好的参考书/其他问题可以补充这个细节
让我们看一个适合问题目标的模块:
module mymodule
implicit none
type mytype
integer :: a
integer :: b
end type
type(mytype), dimension(10,10) :: data
end module mymodule
在其他地方,使用模块可访问定义和对象:
program main
use mymodule ! make all public entities from the module available
! The entity "data" is available from the module, as is the
! default structure constructor
data(1,1) = mytype(1,1)
end program
subroutine externalsub
use mymodule
implicit none
print *, data(1,1) ! We're talking about the same element of the module's array
end subroutine
你可以使用一个模块。有关Fortran模块的更多详细信息,请参阅:谢谢,看来这正是我所需要的。模块来自Fortran 90,不晚于所提供的代码。您应该提到模块是在Fortran 90中引入的。