Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't compile with module and main program in same file

Tags:

fortran

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
like image 546
David Hansen Avatar asked Jun 26 '15 23:06

David Hansen


1 Answers

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
like image 194
David Hansen Avatar answered Oct 23 '22 20:10

David Hansen