HELP Sep. 15, 1984 F77/TUTORIAL f77 Fortran Compiler Tutorial for New Users This help file covers in detail the basics of using Fortran on UNIX; this is an expanded version of the "help f77 intro" and "help f77 options". To learn about UNIX, read "Communicating with UNIX" and "Edit: a Tutorial" (see "help f77 printed_doc"). This tutorial assumes that you know (or are learning) the Fortran language. If you already know the basics, see "help f77 intro" and "help f77 options" which cover more information but with less explanation. An Example: Here is how to write, compile, and execute a simple Fortran pro- gram. Use the editor to create a file called 'hello.f' that con- tains the three lines: print 100 100 format('hello, world.') end To start statements on or after column 7, you can either use spaces or a tab character (control-I). After exiting the editor, when you have the shell prompt again, type the command: f77 hello.f This invokes 'f77', the UNIX Fortran compiler. If you entered the file correctly, the system will respond with: hello.f: MAIN: Loading a.out ... If there are other messages, check that you entered the file 'hello.f' correctly. (Also, on some systems, the last line, "Loading a.out ..." may not appear). Two new files will have been created in your directory: 'hello.o', and 'a.out'. Type: ls to list your files and verify that 'hello.o' and 'a.out' have been created. To run the program, type: a.out Your program will respond by printing the message "hello, world." Terminology and Background: The name "Fortran" stands for FORmula TRANslation. A Fortran compiler is a special computer program that translates, or "com- piles", programs written in the Fortran source language into machine instructions (in binary code) that the computer hardware can read and execute. The files you create containing Fortran statements are known as "source files" since they provide source for the compiler to act upon. The file 'hello.f' was the source file in the example above. Fortran source files for f77 must have names that end in the two characters '.f'. The command "f77 hello.f" in the example above invoked the f77 compiler on the source file. If the compiler detects any errors in the source file it will print messages to that effect. If it finds no errors (as in this case), the compiler creates an "ob- ject file", 'hello.o'. The object file contains machine readable instructions corresponding to the Fortran instructions in the source file. It is not human readable. (You could see this by typing "cat hello.o", but it is generally a bad idea to use "cat" to look at an object file as it sometimes "hangs" the terminal). The compiler automatically invokes another special program called the "loader". The loader combines together object files for the main program, any subroutines or functions it might call, and various run-time support routines to form what is called the "ex- ecutable file". The executable file is created with the name 'a.out'. As you discovered above, one executes an executable file just as one executes any other UNIX command: just type its name. The 'hello' in the file name 'hello.f' is sometimes called the root part of the file name. The '.f' is called the extension. Object files created by f77 have names with the same root part as the source file name but with a '.o' extension. The default name for the executable file is 'a.out'. A More General Example: A Fortran source file may contain subroutines along with the main program. Alternatively, subroutines may be put into files separate from the main program. This is useful when a subroutine might be called by any one of several different programs - it al- lows you to maintain only one copy, saving computer time and mo- ney as you need to recompile only those subprograms that change. Suppose that your main program is in the file 'main.f' and that it calls subroutines in the files 'sub1.f', 'sub2.f', and 'sub3.f'. To compile and load these you would type: f77 main.f sub1.f sub2.f sub3.f This command would compile each of the four source files; that is, for each one it would report any errors or create an object file if there were no errors. Next, the UNIX loader would be in- voked by the f77 command to create a single executable file named 'a.out'. Execute this file by typing its name. If you change a source file, you must recompile it and reload everything to create a new executable file that incorporates the changes. On the other hand, if a source file has not been changed since the last time you compiled it, you can keep the corresponding object file and use it over and over again. To do so, just replace the name of the source file in the f77 command with the name of the object file. In this example, suppose that you have just edited the files 'sub1.f' and 'sub2.f' but 'main.f' and 'sub3.f' are unchanged since the previous compilation. Then you need to recompile 'sub1.f' and 'sub2.f' but not the other two source files. To recompile and reload you would type: f77 main.o sub1.f sub2.f sub3.o This is preferable to the previous command where all four files mentioned were source files, since it avoids unnecessary recompi- lation of 'main.f' and 'sub3.f'. Options: There are many options for the f77 command, for a complete list, see "man 1 f77", the f77 manual page. To compile only and not load, use the -c option. For example type, f77 -c prog.f to create 'prog.o' but not 'a.out'. Later, you could then create the a.out directly from the .o file: f77 prog.o The -C options directs the compiler to generate code to check that subscript references are legal: f77 -C prog.f The -g option directs the compiler and loader to save enough in- formation so that the source level debugger, dbx, can be used if the program does not work: f77 -g prog.f While the -C option is only specified to the compiler, the -g op- tion must be specified to both the compiler and loader; thus the equivalent commands for separately compiling and loading with -g are: f77 -g -c prog.f f77 -g prog.o The -o option directs the loader to put the executable file in a file other than 'a.out', e.g. to have the executable file put in 'myprog', type: f77 prog.f -o myprog The -o option and file name may appear anywhere in the f77 com- mand. The -l option specifies system libraries; e.g. to specify that you need subroutines from the f77 graphics interface li- brary, type: f77 prog.f -lf77plot Note that the name of the library, 'f77plot', follows immediately after the -l without a space. This is in contrast to the -o op- tion, which has a space between the -o and the file name. Also, libraries must be listed in the f77 command after the source and object file names. Many options may be requested simultaneously, e.g.: f77 -C -g prog.f sub1.f sub2.o -lf77plot -o myprog Misc: When developing a program, it's easiest to keep each subroutine in a separate file. Because you'll be modifying each one fre- quently, but probably not all at the same time, this strategy holds down unnecessary recompilations. Name each file after the subroutine it contains (or the most important subroutine if it contains several). If you have a file containing several subprograms, you can split each into a separate file by using the "fsplit" command: fsplit prog.f See "man 1 fsplit" and "help f77 style" for more details. To conserve disk space, delete files when finished with them. Keep object and executable files around if you'll be using them for a while - compilation costs money and imposes a heavy load on the system and unnecessary recompilation should be avoided when- ever possible. However, delete any object files or executable files that are not likely to be needed again. For additional information on f77, refer to "help f77".