4. How to use Fortran under Unix

Practical details

For the course SCCM-001 you will need an account on the leland computers. If you don't have one, you should go over to Sweet Hall and open an account ASAP.

In this class, we will be using Fortran under the Unix operating system. If you have no previous experience with Unix, you will have to learn the basics on your own. The consultants at Sweet Hall have some brochures that can be useful. There is also a class taught in the CS department that gives an introduction to Unix.

You can use any Unix workstation that has a Fortran 77 compiler. We recommend that you use either a Sun or a Dec, e.g. you should log onto one of the elaine, amy, or adelbert computers.

Source code, object code, compiling, and linking

A Fortran program consists of plain text that follows certain rules (syntax). This is called the source code. You need to use an editor to write (edit) the source code. The most common editors in Unix are emacs and vi, but these can be a bit tricky for novice users. You may want to use a simpler editor, like xedit which runs under X-windows.

When you have written a Fortran program, you should save it in a file that has the extention .f or .for. Before you can execute the program, you have translate the program into machine readable form. This is done by a special program called a compiler. The Fortran 77 compilers are usually called f77. The output from the compilation is given the somewhat cryptic name a.out by default, but you can choose another name if you wish. To run the program, simply type the name of the executable file, for example a.out. (This explanation is a bit oversimplified. Really the compiler translates source code into object code and the linker/loader makes this into an executable.)


In the class directory there is a small Fortran program called circle.f. Then you can compile and run it by following these steps:
    cp /usr/class/sccm001/circle*.f .
    f77 circle.f
(Note that there are several dots there that can be difficult to read!) If you need to have several executables at the same time, it is a good idea to give the executables descriptive names. This can be accomplished using the -o option. For example,
    f77 circle.f -o circle.out
will compile the file circle.f and save the executble in the file circle.out. Please note that object codes and executables take a lot of disk space, so you should delete them when you are not using them. (The remove command in Unix is rm.)

In the previous examples, we have not distinguished between compiling and linking. These are two different processes but the Fortran compiler performs them both, so the user usually does not need to know about it. But in the next example we will use two source code files.

    f77 circle1.f circle2.f
This will generate three files, the two object code files circle1.o and circle2.o, plus the executable file a.out. What really happened here, is that the Fortran compiler first compiled each of the source code files into obect files (ending in .o) and then linked the two object files together into the executable a.out. You can do these steps separately in the following way:
    f77 -c circle1.f circle2.f
    f77 circle1.o circle2.o
Compiling separate files like this may be useful if there are many files and only a few of them needs to be recompiled. In Unix there is a useful command called make which is usually used to handle large software packages with maany source files. These packages come with a makefile and all the user has to do is to type make. Writing makefiles is a bit complicated so we will not discuss this in this tutorial.


Exercise A
Log onto one of the elaine computers (tips: use elaine-best). Copy the files circle.* from the class directory /usr/class/sccm001. Compile and run these files as described in the examples above.

Exercise B
Keep your executable from excercise 1.1 and log onto one of the adelbert computers. Try running your executable (a.out). What happens? Why? What do you have to do in order to run the program on this computer?

Exercise C
Modify the circle program such that it computes the circumference instead of the area. Compile and run it and verify the answers you get.

Example files

The files you need for these exercises are now also available by WWW right here.
[AMS Home Page] [Fortran Tutorial Home]