next up previous contents
Next: Closing Remarks Up: Introduction to FORTRAN Previous: Statement Ordering and Program

   
Building Programs and Using Libraries

In section 13 we dealt with the subdivision of a program into routines. To create a program ready for execution all the routines must be compiled and combined using the LINK program. For a simple program a single file might contain all the routines in which case the commands to compile, like and run are exactly the same as for a single routine program. Alternatively each routine could be held in a separate file. For example, suppose a program consists of a main routine, and two subroutines SUBA and SUBB. These might be held in the 3 files :-

 
    main.for
    suba.for
    subb.for
To make a program on use:-
    f77 -o main main.for suba.for subb.for
    ./main
Once a routine has been compiled using the FORT command there is no need to compile again if rebuilding the program unless it has been changed. It might appear that if a program consists of a single routine there would be no need to run LINK but this is not the case. Even for the very simplest of programs extra software is needed to complete it. Statements such as PRINT, READ and STOP do not map down directly on to the machine instructions but instead call routines in a special program called the FORTRAN operating system or run time program. Besides this special program that is always in memory when a FORTRAN user program is run, FORTRAN also provides a library of standard subroutines and functions. A library is a collection of routines in a file that can be selected individually during LINK; only the required routines are extracted, unwanted routines are ignored. In the case of the FORTRAN library a number of mathematical functions are available, listed below are some of the most useful:-


EXP(X) 		 E**X

ALOG(X) loge(X)
ALOG10(X) log10(X)
SQRT(X) X**1/2
SIN(X) sin(X). X in radians
COS(X) cos(X). X in radians
TAN(X) tan(X). X in radians
ASJN(X) arcsin(X). Result in range -PI/2 to PI/2
ACOS(X) arcos(X). Result in range 0 to PI
ATAN(X) arctan(X). Result in range -PI/2 to PI/2
ATAN2(Y,X) arctan(Y/X). Result in range -PI to PI
SINH(X) sinh(X)
COSH(X) cosh(X)
TANH(X) tanh(X)
ABS(X) absolute value of X
MOD(Y,X) Y modulo X
MAX(Y,X,...) maximum of an arbitrary number of arguments
MIN(Y,X,...) minimum of an arbitrary number of arguments
SIGN(Y,X) sign of X combined with absolute value of Y
FLOAT(I) convert integer to real
IFIX(X) convert real to integer
ICHAR(K) convert first character of K to integer ASCII code
CHAR(I) convert integer ASCII code to one character string
LEN(K) length of character string K
INDEX(C1,C2) find character string C2 in C1 and return the
starting index
Besides the FORTRAN library there are libraries covering a wide range of applications on each of the main computers in the department. These libraries are, for the most part, not universal so the reader should speak to the manager of the computer to be used about what libraries are available. Once the user has found a library that contains routines needed to complete a program, linker (which runs as part of the f77 command) must be directed to search the library (it does this automatically in case of the FORTRAN library and no special action is needed by the user). Suppose the library file called mylib.a is needed to complete the program described at the start of this section then, command would have to be changed to:-
f77 -o main main.for suba.for subb.for mylib.a
The .a extension tells f77 that it is a library. Any number of libraries can be included in this way. The user can create his or her own library as follows. First all the source routines should be compiled. It must then be processed by the archive utility.
ar -r mylib.a *.o
to create mylib.a.



 
next up previous contents
Next: Closing Remarks Up: Introduction to FORTRAN Previous: Statement Ordering and Program
n west (APC)
2000-03-08