We will be using a FORTRAN compiler called gfortran, created by the GNU project. It is available for a number of different platforms; we will be using the version for Windows.
The compiler is a command-line program. To use it, you will need to open a "DOS window" in Windows. To do this, go to the "Start" menu and select "Run". Type in "cmd" (without the quotation marks) and press Enter. A text-mode window should pop up with a prompt such as
C:\>
You can type commands at the prompt. The input and output is in text mode. To close the window, type "Exit".
Creating an executable program is a two step process. First the source code is compiled to object code. The object code may or may not be stored as a separate file, depending on the compiler used and the compiler options. Second, the object code is linked with other collections of object code (including system libraries) to form an executable file.
The GNU gfortran compiler can be used for both the compilation phase and the linking phase of creating executable files.
Options
There are a number of options available for gfortran. We will not need all of them. Some of the options are:
Prints out the version of the compiler. This option is usually used by itself (if at all).
Compiles source code files to object files and stops. The default behavior of the compiler is to compile the given source code files and link them directly into an executable file. The -c option is useful for efficiently making projects that have multiple source code files.
This will create an object file. Its name will be the same as the source code file, but with a ".o" extension.
Specifies the name of the output file. Without this option, executable files have the (unhelpful) default name of "a.exe".
Notice this is a small 'o'.
Turns on optimization. Allows the compiler to modify the code as it compiles and links to produce smaller and/or faster files. The results are (hopefully) functionally equivalent to the program without optimization. In practice optimizer bugs do occur. Recognizing things that can be optimized is actually rather difficult and is not always done correctly. Because of this, optimizing should be the last thing done to a program. Once a program works without optimization, then the optimizer can be used to try to improve the program. If the program doesn't work, then the optimizer can be blamed.
(It's a computer science joke that compilers also contain a pessimizer that breaks your code and introduces bugs. Unfortunately, nobody has been able to find the flag that turns this feature off.)
Notice this is a big 'O'.
Turns on code generating options for debugging. Detailed information is stored in the object files and executable files about which lines in the source code file are associated with the machine code instructions. This can make programs comparatively large and slow. However, this information is used by a debugger to allow stepping through a program line by line as it executes, which is very useful. The -g option is incompatible with the -O option. They should not be used together.
Turn on all the warning messages possible. Most useful during the compilation phase, but also works during the linking phase.
Use the given directory as a place to search for include files. This directory is used in addition to the standard system include directories. Multiple include directories are specified by using a separate -I option for each directory. There is no space between the -I option and the directory name.
This option is useful only during the compilation phase.
Link the given library into the program. A library is a collection of pre-compiled object code. This option is used only during the linking phase. This option comes at the end of the command line. Multiple libraries are included by using a separate -l option for each library.
Use the given directory as a place to search for library files. This directory is used in addition to the standard system library directories. Multiple library directories are specified by using a separate -L option for each directory. There is no space between the -L option and the directory name.
This option is useful only during the linking phase.
Examples
Here are some examples of using gfortran with a few of the options given above.
For the compilation phase:
gfortran -c -Wall prog1.f90
This compiles prog1.for and creates an object code file called "prog1.o", showing all warnings.
Notice that FORTRAN files normally have an extension of "f90". The object file has the same name but with an extension of "o".
For the linking phase:
gfortran -g -Wall -o prog1 prog1.o
This links the object code file "prog1.o" into the executable file "prog1.exe". All warnings are displayed.
If we don't use any options, as in:
gfortran prog1.f90
the result is that gfortran will first compile and then link, so we will now have two new files named "prog1.o" and "a.exe".
Notice that the executable file has an extension of "exe".