Compile Programs with Modules

Normally, your programs and modules are stored in different files, all with filename suffix .f90. When you compile your program, all involved modules must also be compiled. For example, if your program is stored in a file main.f90 and you expect to use a few modules stored in files compute.f90, convert.f90 and constants.f90, then the following command will compile your program and all three modules, and make an executable file a.out

f90 compute.f90 convert.f90 constants.f90 main.f90
If you do not want the executable to be called a.out, you can do the following:
f90 compute.f90 convert.f90 constants.f90 main.f90 -o main
In this case, -o main instructs the Fortran compiler to generate an executable main instead of a.out.

Different compilers may have different "personalities." This means the way of compiling modules and your programs may be different from compilers to compilers. If you have several modules, say A, B, C, D and E, and C uses A, D uses B, and E uses A, C and D, then the safest way to compile your program is the following command:

f90 A.f90 B.f90 C.f90 D.f90 E.f90 main.f90
That is, list those modules that do not use any other modules first, followed by those modules that only use those listed modules, followed by your main program.

You may also compile your modules separately. For example, you may want to write and compile all modules before you start working on your main program. The command for you to compile a single program or module without generating an executable is the following:

f90 -c compute.f90
where -c means "compile the program without generating an executable." The output from Fortran compiler is a file whose name is identical to the program file's name but with a file extension .o. Therefore, the above command generates a file compute.o. The following commands compile all of your modules:
f90 -c compute.f90
f90 -c convert.f90
f90 -c constants.f90
After compiling these modules, your will see at least three files, compute.o, convert.o and constants.o.

After completing your main program, you have two choices: (1) compile your main program separately, or (2) compile your main with all "compiled" modules. In the former, you perhaps use the following command to compile your main program:

f90 -c main.f90
Then, you will have a file main.o. Now you have four .o files main.o, compute.o, convert.o and constants.o. To pull them into an executable, you need
f90 compute.o convert.o constants.o main.o
This would generate a.out. Or, you may want to use
f90 compute.o convert.o constants.o main.o -o main
so that your executable could be called main.

If you want compile your main program main.f90 with all .o files, then you need

f90 compute.o convert.o constants.o main.f90
or
f90 compute.o convert.o constants.o main.c -o main

Note that the order of listing these .o files may be important. Please consult the rules discussed earlier.