4. Advanced Compiler Options (g77)
The following compiler options can be appended to the end of your fortran command, or alternatively you can use the g77 command directly.
Here is a summary of g77 options covering most of what you might need for more advanced compilation of Fortran programs (including linking objects and libraries). Complete g77 Fortran compilation options and other detailed information about the g77 compiler can be found at:
A basic compilation:
$ g77 myprog.f –o myprog
where the Fortran program source is contain in the file myprog.f and the compiled executable program is myprog.
Other options can be included, for example the command fortran (explained in Section 3) is equivalent to the following g77 compilation:
g77 myprog.f -o myprog -O2 -Wall -fbounds-check
-O2 is an optimise option (your program might run about twice as fast).
-Wall instructs the compiler to give warnings.
-fbound-check run-time array boundary checking.
To compile without linking use the -c option:
$ g77 myprog.f -c
this gives a compiled object file myprog.o
To link two sources or objects use any of the following commands:
$ g77 -o myprog myprog.f lapbits.f
$ g77 -o myprog myprog.f lapbits.o
$ g77 -o myprog myprog.o lapbits.o
the .o object files must exist; i.e. created with ‘g77 –c’
To give the address of INCLUDE files:
$ g77 myprog.f -I/home/yilmaz/fortran/inc/graphics/
or $ g77 myprog.f -I$MYINC
where the environment variable MYINC is set with:
Libraries are linked as follows:
$ g77 –o myprog myprog.f –L. -lgrafs
the convention is to begin the library name with ‘lib' and end with ‘.a’ these are omitted following the -l option and so the library linked in this example is called libgrafs.a
$ g77 –o myprog myprog.f -L/usr/lib -lgrafs
The –L option tells the compiler that the library can be found in /usr/lib i.e. the library /usr/lib/libgrafs.a exists.
Other compiler options:
-O2 (uppercase o) optimise (programs often run twice as fast with this).
-fbounds-check runt-me checking of array boundary errors (recommended!)
-fno-backslash \\ may be interpreted different from other compilers!
-fno-silent print program units being compiled.
-ffree-form free-format souce code (i.e. no column 6,7,72 etc restrictions).
-ffixed-line-length-132 up to 132 characters allowed , can be any value.
-fpedantic checks for statements which might be ambiguous when porting
from other compilers.
-Wimplicit Warning : implicit none.
-Wunused Warning : declared but unused variables.
-Wuninitialized Warning : uninitialised variables (you must use -O2 here).
-Wall Warning : = unused+uninitialised.
-finit-local-zero Initialises all un-initialised variables to zero.
Random number generator: RAN() (this is not a g77 option, it is a local library)
A random number generator is supplied on local Linux servers in /usr/local/lib (which is in your PATH variable). The usage is the same as the VMS FORTRAN Function 'RAN()' so programs ported from VMS will work in this sense; but you need to link the 'ran' library; for example:
$ g77 myprog.f -o myprog –L/usr/local/lib -lran
Example of usage (as in VMS FORTRAN) is given below; the program outputs ten random numbers:
C Fortran 77 program making use of the RAN() function
ISEED=314159265 ! initialise the seed
R=RAN(ISEED) ! call RAN() for a random number
Note: 'RAN()' is based on Park and Miller's "Minimal Standard" random number generator (Comm. ACM, 31, 1192, 1988) it has a period of about 109; If you need a 'serious' generator (e.g. with a very long period) then you shouldn’t use this function.
The g2 Graphics Library (this is not a g77 option, it is a local library)
You can obtain graphical output by using commands from the g2 graphic library;
Other g77 facilities
- g77 supports the three-octal-digit for ASCII codes, for example
print *,'\265' outputs the greek mu character
print *,'\007' gives a bell
- Linux commands can be given from the SYSTEM call:
CALL SYSTEM('Linux command',ISTAT)
where 'Linux command' can be, for example, 'ls' for directory listing. ISTAT is an INTEGER containing the exit status of the call.
Some notes which you might find useful:
1. In the currently installed g77 version there is no facility for run-time bounds checking (the next upgrade will contain a bounds checking option)
2. Uninitialised variables are NOT automatically set to zero (unlike the VMS compiler), you should therefore initialise all your variables otherwise they will take unpredictable values. If your program behaves strangely and you suspect it is due
uninitialised variables then try the compiler option -finit-local-zero this will initialise all uninitialised variables to zero.
To find these variables in your source code you can switch on the warning option:
–Wuninitialized (without -finit-local-zero).
3. Double-precision constants, for example 9.43874539284958 will be interpreted as a single precision value i.e. it will be truncated to 9.4387453. You should use the double-precision notation i.e. write 9.43874539284958D0 .