Compiling and running Fortran programs
Before your program can be executed, it must be compiled.
This converts the English-like text
of a Fortran program into a binary form that the processor
understands.
You compile a program by typing a line similar to the
following:
gfortran -o myprogram myprogram.f
It is important that you understand what is happening here:
- gfortran is the name of the
Fortran compiler. This is a "free" compiler that often is included
in Linux distributions.
Your compiler may have a different name
such as
ifort
or
pgf77.
- The -o option tells the
compiler that the next word (here, myprogram) will be the name of
the binary version of the program. If
you omit this option, most systems will
use the default name
a.out
for the binary version regardless of the name of your program. It's OK
to use this default, though it's usually helpful
to use a more meaningful name.
The binary file is often called the executable
file because the computer can run (execute) it.
- myprogram.f is the
name of the file that contains the source code
of your program. The source code is the file of
instructions that you actually edit and type. Your source
file could be named something other than
myprogram.f,
such as
homework1.f.
or some other descriptive name. On many systems the name
of the source file must end with the
suffix .f
or the compiler will become unhappy.
To run the program, simply type the name of the executable
file:
myprogram
On Unix systems (or Linux, or other Unix variants) you may need to explicitly state
that the program resides in your current directory. The shorthand for the current
directory is . (a period, or "dot"). Then the program would be run as:
./myprogram
Next: The simplest Fortran program.
Back to the main page for the programming tutorial.