如何在Fortran 95中包含来自不同文件的模块?

Has*_*san 2 fortran fortran95 silverfrost-fortran

问题很明显我认为,虽然我用Google搜索,但我找不到任何解决方案.我想划分我的源代码以使其更易于维护.如何引用另一个文件中的模块?

Hig*_*ark 5

我认为你正在寻找use声明.例如,您可能有一个包含模块定义的源文件,大纲如下:

module abstract_types
    implicit none
    ! declarations
  contains
    ! procedure definitions
end module abstract_types 
Run Code Online (Sandbox Code Playgroud)

然后,在另一个源文件中,使用该模块的程序,概述:

program hello_there
    use abstract_types
    implicit none
    ! declarations
    ! executable statements
end program hello_there
Run Code Online (Sandbox Code Playgroud)

注意:

  • use声明之前的任何implicit陈述.

  • use语句按名称引用模块.

在编译时,请确保在程序源文件之前编译模块源文件; 在编译时(不在链接时),编译器将查找模块文件(通常称为mod文件)以满足use语句中对模块的引用.该mod文件有点像头文件,但它是由编译器创建的.

之后,当您链接程序时,您将需要模块和程序的目标文件.

  • @ High Performance Mar:您如何指示编译器将mod文件放在哪里? (3认同)