|
|
1.1 root 1: .TI F77/TUTORIAL "Sep. 4, 1985"
2: f77 Fortran Compiler Tutorial for New Users
3:
4: This help file covers in detail the basics of using Fortran on UNIX;
5: this is an expanded version of the "help f77 intro" and "help
6: f77 options". To learn about UNIX, read "Communicating with UNIX"
7: and "Edit: a Tutorial" (see "help f77 printed_doc"). This tutorial
8: assumes that you know (or are learning) the Fortran language.
9: If you already know the basics, see "help f77 intro" and "help f77 options"
10: which cover more information but with less explanation.
11:
12: An Example:
13:
14: Here is how to write, compile, and execute a simple Fortran program.
15: Use the editor to create a file called 'hello.f' that contains the
16: three lines:
17:
18: .nf
19: print 100
20: 100 format('hello, world.')
21: end
22: .fi
23:
24: To start statements on or after column 7, you can either use spaces or
25: a tab character (control-I). After exiting the editor, when you have
26: the shell prompt again, type the command:
27:
28: f77 hello.f
29:
30: This invokes 'f77', the UNIX Fortran compiler. If you entered the file
31: correctly, the system will respond with:
32:
33: .nf
34: hello.f:
35: MAIN:
36: .fi
37:
38: If there are other messages, check that you entered the file 'hello.f'
39: correctly.
40: Two new files will have been created in your
41: directory: 'hello.o', and 'a.out'. Type:
42:
43: ls
44:
45: to list your files and verify that 'hello.o' and 'a.out' have
46: been created. To run the program, type:
47:
48: a.out
49:
50: Your program will respond by printing the message "hello, world."
51:
52: Terminology and Background:
53:
54: The name "Fortran" stands for FORmula TRANslation. A Fortran compiler
55: is a special computer program that translates, or "compiles", programs
56: written in the Fortran source language into machine
57: instructions (in binary code) that the computer hardware can read and
58: execute.
59:
60: The files you create containing Fortran statements are known as
61: "source files" since they provide source for the compiler to act upon.
62: The file 'hello.f' was the source file in the example above. Fortran
63: source files for f77 must have names that end in the two characters '.f'.
64:
65: The command "f77 hello.f" in the example above invoked the f77 compiler
66: on the source file. If the compiler detects any errors in the source
67: file it will print messages to that effect. If it finds no errors (as
68: in this case), the compiler creates an "object file", 'hello.o'. The
69: object file contains machine readable instructions corresponding to the
70: Fortran instructions in the source file. It is not human readable.
71: (You could see this by typing "cat hello.o", but it is generally a
72: bad idea to use "cat" to look at an object file as it sometimes "hangs"
73: the terminal).
74:
75: The compiler automatically invokes another special program called the
76: "loader". The loader combines together object files for the main
77: program, any subroutines or functions it might call, and various
78: run-time support routines to form what is called the "executable file".
79: The executable file is created with the name 'a.out'. As you
80: discovered above, one executes an executable file just as one executes
81: any other UNIX command: just type its name.
82:
83: The 'hello' in the file name 'hello.f' is sometimes called the root
84: part of the file name. The '.f' is called the extension. Object files
85: created by f77 have names with the same root part as the source file
86: name but with a '.o' extension. The default name for the executable
87: file is 'a.out'.
88:
89:
90: A More General Example:
91:
92: A Fortran source file may contain subroutines along with the main
93: program. Alternatively, subroutines may be put into files separate
94: from the main program. This is useful when a subroutine might be
95: called by any one of several different programs - it allows you to
96: maintain only one copy, saving computer time and money as
97: you need to recompile only those subprograms that change.
98:
99: Suppose that your main program is in the file 'main.f' and that it
100: calls subroutines in the files 'sub1.f', 'sub2.f', and 'sub3.f'. To
101: compile and load these you would type:
102:
103: f77 main.f sub1.f sub2.f sub3.f
104:
105: This command would compile each of the four source files; that is, for
106: each one it would report any errors or create an object file if
107: there were no errors.
108: Next, the UNIX loader would be invoked by the f77 command to
109: create a single executable file named 'a.out'.
110: Execute this file by typing its name.
111:
112: If you change a source file, you must recompile it and reload
113: everything to create a new executable file that incorporates
114: the changes. On the other hand, if a source file has not been changed
115: since the last time you compiled it, you can keep the corresponding
116: object file and use it over and over again. To do so, just replace the
117: name of the source file in the f77 command with the name of the object
118: file. In this example, suppose that you have just edited the files 'sub1.f'
119: and 'sub2.f' but 'main.f' and 'sub3.f' are unchanged
120: since the previous compilation. Then you need to recompile 'sub1.f'
121: and 'sub2.f' but not the other two source files. To recompile and
122: reload you would type:
123:
124: f77 main.o sub1.f sub2.f sub3.o
125:
126: This is preferable to the previous command where all four files
127: mentioned were source files, since it avoids unnecessary recompilation
128: of 'main.f' and 'sub3.f'.
129:
130:
131: Options:
132:
133: There are many options for the f77 command, for a
134: complete list, see "man 1 f77", the f77 manual page.
135:
136: To compile only and not load, use the -c option. For example type,
137:
138: f77 -c prog.f
139:
140: to create 'prog.o' but not 'a.out'. Later, you could then create
141: the a.out directly from the .o file:
142:
143: f77 prog.o
144:
145: The -C options directs the compiler to generate code to check that
146: subscript references are legal:
147:
148: f77 -C prog.f
149:
150: The -g option directs the compiler and loader to save enough
151: information so that the source level debugger, dbx, can be used if the
152: program does not work:
153:
154: f77 -g prog.f
155:
156: While the -C option is only specified to the compiler, the -g option must
157: be specified to both the compiler and loader; thus the equivalent
158: commands for separately compiling and loading with -g are:
159:
160: .nf
161: f77 -g -c prog.f
162: f77 -g prog.o
163: .fi
164:
165: The -o option directs the loader to put the executable file in a file
166: other than 'a.out', e.g. to have the executable file put in 'myprog',
167: type:
168:
169: f77 prog.f -o myprog
170:
171: The -o option and file name may appear anywhere in the f77 command.
172: The -l option specifies system libraries; e.g. to specify that
173: you need subroutines from the f77 graphics interface library and
174: the Tektronics 4014 library, type:
175:
176: f77 prog.f -lf77plot -l4014
177:
178: Note that the names of the libraries, 'f77plot' and '4014',
179: follow immediately after the -l without a space.
180: This is in contrast to the -o option, which
181: has a space between the -o and the file name. Also, libraries must be
182: listed in the f77 command after the source and object file names.
183:
184: Many options may be requested simultaneously, e.g.:
185:
186: f77 -C -g prog.f sub1.f sub2.o -lf77plot -l4014 -o myprog
187:
188: Misc:
189:
190: When developing a program, it's easiest to keep each subroutine
191: in a separate file. Because you'll be modifying each one frequently, but
192: probably not all at the same time, this strategy holds
193: down unnecessary recompilations.
194: Name each file after the
195: subroutine it contains (or the most important subroutine if it contains
196: several).
197:
198: If you have a file containing several subprograms, you can split each
199: into a separate file by using the "fsplit" command:
200:
201: fsplit prog.f
202:
203: See "man 1 fsplit" and "help f77 style" for more details.
204:
205: To conserve disk space, delete files when finished with them.
206: Keep object and executable files around if
207: you'll be using them for a while - compilation costs money and imposes
208: a heavy load on the system and unnecessary recompilation should be
209: avoided whenever possible. However, delete any object files
210: or executable files that are not likely to be needed again.
211:
212: For additional information on f77, refer to "help f77".
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.