# Order of modules in a Fortran program

**URL:** <https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130>\
**Category:** Help\
**Created:** [February 3, 2023, 10:51pm UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130 "2023-02-03T22:51:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [February 3, 2023, 10:51pm UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/1 "2023-02-03T22:51:36Z")

</div>

According to MFE p.74 “No ordering of modules is required by the standard, but normal practice is to require each module to precede its use. … [This practice] is required by many compilers.” I found that gfortran and ifort won’t compile the program below, which would seem to be standard-conforming. Does any compiler accept it and print `'In subroutine test2'` ?

```auto
! Must used modules be in the right order?
module mod1
  use mod2,only:test2
contains
  subroutine test1
    call test2
  end subroutine test1
end module mod1

module mod2
contains
  subroutine test2
    print *,'In subroutine test2'
  end subroutine test2
end module mod2

program test
  use mod1
  call test1
end program test

```

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [February 4, 2023, 1:31am UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/3 "2023-02-04T01:31:48Z")

</div>

Should the standard specify that a module is not available if it appears after its USE statement? The F2008 = F2023 wording seems to have misled the three authors of MFE if Kargl is right. All four of them are of course renowned Fortran experts.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [February 4, 2023, 2:35am UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/4 "2023-02-04T02:35:58Z")

</div>

It is not a requirement of the standard that all modules contained in a single file be compiled in the order provided. The standard says nothing about files or compilation. So what then does it mean to the standard for a module “to be available”?

Now of course in practice it’s true every processor in common usage is a compiler, and they compile the contents of a file in order. I would say that yes, MFE is a bit optimistic with that statement, but not technically wrong.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 4, 2023, 5:16am UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/6 "2023-02-04T05:16:31Z")

</div>

> [@Harper](#):
>
> I found that gfortran and ifort won’t compile the program below, which would seem to be standard-conforming.

The sentence you quoted from MFE says that it is not required by the standard for a processor to compile it correctly. In practice, as you noted, most compilers will not compile it when the source file is in a clean directory. Even worse, if for some reason there is an existing `mod2.mod` file sitting around, then that is the file that will be used when `module mod1` is compiled. If the actual `module mod2` has changed since the previous `mod2.mod` file was created, this can result in strange errors. Then the next time you try to compile `module mod1`, it might then work, because there is now a new correct `mod2.mod` file in the directory.

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [February 4, 2023, 8:14pm UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/7 "2023-02-04T20:14:34Z")

</div>

Indeed. I have found sometimes that if I compile a program with gfortran, then ifort won’t compile it unless I delete all the relevant .mod files and then try ifort again. I didn’t need that workaround when compiling first with ifort then with gfortran. Sorry, I haven’t isolated exactly what feature of the offending program caused the problem.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 4, 2023, 10:34pm UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/8 "2023-02-04T22:34:05Z")

</div>

> [@Harper](#):
>
> … I found that gfortran and ifort won’t compile the program below, which would seem to be standard-conforming …

@Harper,

The word “optimistic” has appeared here in the context of MFE, but please note another term, “a stretch”, can be introduced here to view this as standard-conforming on the basis of `MFE`!

Please also note from a practical point-of-view with actual projects (where modules are better off in separate files) and `Intel Fortran` and Windows OS and Microsoft Visual Studio IDE (you will know no licensing needed for most nonprofit domains including educational institutions), a user can just add all the files to a Visual Studio project in any order which the IDE then displays in alphabetical order and then at build time, the Intel Fortran integration parses and compiles them usually in the right order.  
 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/915b1abc513fcd876181fc864163a38fcc056c2f.png)

![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/b/b43ddab4f33b90d4abc4311fae993f55f55b09fe.png)

You can then add `-gen-dep` compiler option to generate a listing you can use with other processors, say like so:

```auto
mod2.obj: \
  mod2.f90
mod1.obj : \
  mod1.f90 mod2.mod
p.obj : \
  p.f90 mod1.mod

```

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 5, 2023, 2:09am UTC](https://fortran-lang.discourse.group/t/order-of-modules-in-a-fortran-program/5130/9 "2023-02-05T02:09:52Z")

</div>

> [@Harper](#):
>
> I didn’t need that workaround when compiling first with ifort then with gfortran.

Anyone know why or how this would work? How can gfortran use `*.mod` files created with ifort?

It is typical when switching compilers to clean all of the `*.mod` and `*.o` files first. With the `make` build utility, for example, one would typically define a `clean:` target that does that. So the sequence “make clean” and then “make” would be executed when switching to a new compiler.
