Can't compile with module and main program in

2020-04-03 16:58发布

I am trying to make use of a module that is in the same file as my main program. However, I cannot get it to work. Does Fortran allow a module to be contained in the same file as the main program or must it be in a separate file? Here is a simple version of my code:

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

When I try to compile this file I get:

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

标签: fortran
1条回答
▲ chillily
2楼-- · 2020-04-03 17:37

Yes, Fortran does allow modules to be contained in the same file as the main program. However, modules must be written before the main program:

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
查看更多
登录 后发表回答