无法与同一文件中的模块和主程序一起编译

时间:2015-06-26 23:04:27

标签: fortran

我正在尝试使用与我的主程序位于同一文件中的模块。但是,我无法让它发挥作用。 Fortran是否允许模块包含在与主程序相同的文件中,或者它是否必须位于单独的文件中?这是我的代码的简单版本:

main program
  use my_module
  call my_subroutine()
end program main

module my_module
  contains
    subroutine my_subroutine()
      print *, "Hello World!"
    end subroutine my_subroutine
end module my_module

当我尝试编译此文件时,我得到:

Fatal Error: Can't open module file 'my_module.mod' for reading at (1): No such file or directory

1 个答案:

答案 0 :(得分:7)

是的,Fortran确实允许模块包含在与主程序相同的文件中。但是,必须在主程序之前编写模块:

module my_module
  contains
    subroutine my_subroutine()
      print *, "Hello World!"
    end subroutine my_subroutine
end module my_module

program main
  use my_module
  call my_subroutine()
end program main