无法在同一个文件中使用模块和主程序进行编译 [英] Can't compile with module and main program in same file

查看:126
本文介绍了无法在同一个文件中使用模块和主程序进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用与我的主程序位于同一个文件中的模块。但是,我无法让它工作。 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


推荐答案

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

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

这篇关于无法在同一个文件中使用模块和主程序进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆