Clicky

Fortran Wiki
Compiling and linking modules

Compiling and linking modules

The following example illustrates how to use the GNU Fortran compiler for compiling and linking modules.

Files

Example module

!> \\file testModule.f
module mathModule

  implicit none
  private
  real, public, parameter :: &
    pi = 3.1415 , &
    e = 2.7183 , &
    gamma = 0.57722

end module mathModule

Example main program

!> \\file mainProg.f
program testing
  use mathModule
  implicit none

  print *, "pi:", pi, "e:", e, "gamma:", gamma  

end program testing

Compiling and linking

At first compile both files and produce object files (*.o) and the module file (mathmodule.mod). Note that the ordering of the files is not important here.

$ gfortran -ffree-fortran -c testModule.f mainProg.f
$ ls
mainProg.f  mainProg.o testModule.f  testModule.o  mathmodule.mod

Linking is done by

$ gfortran testModule.o mainProg.o
$ ls
a.out mainProg.f  mainProg.o testModule.f  testModule.o  mathmodule.mod