HP Fortran Programmer Guide (766160-001, March 2014)

Example 8 Example 2-4 data.f90
! stats_db: shared data declared here
MODULE stats_db
INTEGER, PARAMETER :: size = 100 ! max number of items in array
! n_items, sum, and item hold the data for statistical analysis
INTEGER :: n_items, sum
INTEGER, DIMENSION(size) :: item
! the initializations are just to start the program going
DATA n_items, sum, item/3, 233, 97, 22, 114, 97*0/
END MODULE stats_db
The use of modules in this program creates dependencies between the files because a file that uses
a module that is defined in another file is dependent on that other file. These dependencies affect
the order in which the program files must be compiled. The dependencies in the example program
are:
main.f90is dependent upon code.f90.
code.f90is dependent upon data.f90.
These dependencies require that data.f90be compiled before code.f90, and that code.f90be
compiled before main.f90. This order ensures that the compiler will have created each of the
.mod files before it needs to read them.
The order of the source files listed in the following command line ensures that they will compile
and link successfully:
$ f90 -o do_stats data.f90 code.f90 main.f90
During compilation, f90will create two .mod files, STATS_CODE.modand STATS_DB.mod.
These will be written to the current working directory, along with the object files and the executable
program, do_stats. Following is a sample run of the executable program:
$ do_stats
Enter an integer (hint: 77 is current average): 77
Average or better.
If instead of the preceding command line, the program had been compiled as follows:
$ f90 -o do_stats main.f90 data.f90 code.f90
the compilation would fail and f90would print the error message:
Error FCE37 : Module STATS_CODE not found
The compilation would fail because the compiler cannot process main.f90without
STATS_CODE.mod. But the order in which the program files appear on the command line prevents
the compiler from processing code.f90(and thereby creating STATS_CODE.mod) until after it
has processed main.f90.
Compiling with make
If you use the make utility to compile Fortran programs, the description file should take into account
the dependencies created by modules. For example, to compile the do_stats program using
the make utility, the description file should express the dependencies as follows:
Special-purpose compilations 57