fortranfortran95silverfrost-fortran

How to include a module from a different file in Fortran 95?


The question is obvious I think, although I googled it, I could not find any solutions. I want to split my source code to keep it more maintainable. How can I reference a module in another file?


Solution

  • I think that you are looking for the use statement. You might, for example, have one source file containing the definition of a module, outline:

    module abstract_types
        implicit none
        ! declarations
      contains
        ! procedure definitions
    end module abstract_types 
    

    and then, in another source file, a program which uses the module, outline:

    program hello_there
        use abstract_types
        implicit none
        ! declarations
        ! executable statements
    end program hello_there
    

    Note:

    When it comes to compilation, make sure that you compile the module source file before the program source file; at compilation time (not at link time) the compiler will look for a module file (often called a mod file) to satisfy the reference to the module in the use statement. The mod file is a bit like a header file, but it's created by the compiler.

    Later, when you link your program you'll need the object files for both module and program.