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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With