I don’t have MFE handy right now, so I’m curious what exactly that sentence means. Does it mean, practically speaking, that a module must be compiled before it is USEd?
If modules are placed in separate files, then that statement is typically true. That is why Makefiles, or other ways of specifying compilation order, are necessary when modules occur in this case. If several modules are placed in a single file, then it is a fairly standard requirement that a module must appear within that file before it is used by a subsequent module in that file. Is that the context in which that statement applies?
This is in contrast to, for example, external functions or common blocks. These entities can be referenced in one file before they are compiled in another file, or if they are defined in a single file, they can be referenced early in the file before they are defined later in that file. Matching of interfaces or of common block lengths is the responsibility of the programmer, and this is a common source of errors. Compilation order is irrelevant in these cases.
That’s true for pretty much all explicit interfaces —i.e., procedures and interface blocks.
But interface blocks (i.e., procedure signatures) must precede its use in other interface blocks, especially when procedure prefixes are involved.
Here, both ifort/ifx and flang (v21) complain if the max_len_trim signature is placed after the do_something signature, but gfortran (v14) doesn’t seem to care:
module mod1
implicit none
interface
module pure function max_len_trim(array) result(ml)
integer :: ml
character(*), intent(in) :: array(:)
end function
module pure function do_something(array) result(res)
character(*), intent(in) :: array(:)
character(max_len_trim(array)) :: res
end function
end interface
end module mod1
3 cases arise. (1) Module and its user in the same file, (2) module and
its user in different files compiled together, (3) module and its user
compiled separately.
The whole paragraph of MFE that I should have quoted (sorry I didn’t) is
“A module may contain use statements that access other modules. It
must not access itself directly or indirectly theough a chain of use
statements, for example a accessing b and b accessing a. No overall ordering of modules is required by the standard, but
no module may precede a module that it uses.”
I therefore think that in case (1) if the module called mymod, say, and
its user (either another module or a main program) is called myuser then
the file should be
module mymod
...
end mymod
program myuser ! or module myuser
use mymod
...
end program myuser ! or end module myuser
If the module and its user are in separate files mymod.f90 and
myuser.f90 then MFE seem to be recommending in case (2)
lfortran mymod.f90 myuser.f90
but not
lfortran myuser.f90 mymod.f90
In case (3) compile mymod.f90 first.
– John Harper, School of Mathematics and Statistics
Victoria Univ. of Wellington, PO Box 600, Wellington 6140, New Zealand.
e-mail john.harper@vuw.ac.nz