Getting Started Gnu Fortran
Getting Started Gnu Fortran
Getting Started Gnu Fortran
By Gilberto E. Urroz
Created in August 1999 – Modified on September 2002
GNU Fortran G77 is a free Fortran compiler produced by the Free Software Foundation (FSF).
The credit for its creation goes to Mr. Craig Burley of the FSF and the team of very competent
individuals that volunteered to help polishing and extending the compiler. It can compile
Fortran 77 and supports all the common extensions that finally made it into Fortran 90 specs.
The executables it produces are as fast as those produced by any commercial compiler.
You can download a zip file containing the GNU FORTRAN files by accessing the following URL:
http://www.geocities.com/Athens/Olympus/5564/
Click on the text " A minimal set of G77 for Win32" to access the download page for the GNU
FORTRAN G77 binary files (http://www.geocities.com/Athens/Olympus/5564/g77.htm)
Somewhere in that page you will find the following information:
__________________________________________________________________________________
The set consists of the following files:
• g77exe.zip (1.2Mb), the archive that contains the compiler, linker and ancillary
executables
• g77lib.zip (440Kb), the archive that contains the G77 libraries
• g77doc.zip (240Kb), the archive that contains the G77 documentation. Note that this
documentation is in HTML format, so it can be read using any web browser such as
Netscape or Microsoft Internet Explorer. This documentation was originally converted to
HTML format and posted as such by Mr. Michel Kern.
__________________________________________________________________________________
Click on each of the following texts: "g77exe.zip", "g77lib.zip", and "g77doc.zip", saving the
corresponding files to your C directory. For example, clicking on "g77exe.zip" will produce the
dialog box in Figure 1. Click the "Save File" button to get the dialog box of Figure 2. Select
your directory C and press the "Save" button to save the file. Repeat the same procedure with
the files g77lib.zip and g77doc.zip.
1
Figure 1. Dialog box for downloading file "g77exe.zip" from
http://www.geocities.com/Athens/Olympus/5564/g77.htm
__________________________________________________________________________________
Installation Steps
All the above ZIP archives should be un-zipped with the free unzip.exe utility (115Kb.
Download the file unzip.zip and rename it to unzip.exe) . This utility will preserve both, the
directory structure of the archives and the long filenames. Typically, unzipping the files in,
say, the drive C: will create the directory C:\G77 with the three subdirectories C:\G77\Bin,
C:\G77\Lib and C:\G77\Doc. Further instructions are supplied in the file C:\G77\readme1.txt
__________________________________________________________________________________
2
Click on the text "unzip.exe". This will provide a file named unzip.zip for you to save,
however, as indicated above, you must save it under the name unzip.exe in directory C.
To install the software you will need to open a DOS window (press the "Start" button in your
Windows 95 bar, select the "Programs" icon, and then select the "MS-DOS Prompt" icon).
Change directory to C, by using the command:
CD C:\
UNZIP G77EXE.ZIP
UNZIP G77LIB.ZIP
UNZIP G77DOC.ZIP
To leave the DOS environment, type EXIT. When you finish using these commands you will have
created a directory C:\G77 that will contain three subdirectories called "bin," "doc," and "lib."
These subdirectories contain, respectively, the binary files, documentation, and library files for
the GNU FORTRAN G77 compiler. There will be also a text file called readme1.txt with
important information on the software.
To create FORTRAN source code to be compiled using GNU FORTRAN G77 you can use any text
editor, for example, Notepad, or my favorite PFE. Just make sure to follow the proper
FORTRAN 77 syntax. The FORTRAN source code files must use the suffix "f". For example, in
your subdirectory C:\G77 there will be a file called mytest.f, which is provided for you to test
your compiler. Open the file mytest.f using Notepad or PFE to see a FORTRAN program used
for the simultaneous solution of linear equations.
http://www.lancs.ac.uk/people/cpaap/pfe
PFE is a text editor designed for text-based programming in languages such as FORTRAN, JAVA,
C++, etc. PFE allows the user direct access to DOS windows for compilation and testing of
programs, runs DOS commands from the PFE interface, allows recording and playing of Macro
commands, and permits easy navigation of files by allowing line numbering for easy debugging
of programs.
PFE can be used in combination with SCILAB to type scripts and function files or to edit diary
files produced from within SCILAB.
3
HOW TO COMPILE FORTRAN PROGRAMS USING G77
To use the G77 compiler you need to use a DOS window and move to subdirectory G77. Use the
following command in the DOS window to move to subdirectory G77:
CD C:\G77
To use the system you need to execute the very short batch file G77SETUP.BAT that adds the
\G77\BIN directory to your path and defines an environment variable LIBRARY_PATH that points
to \G77\LIB . In your DOS window type:
G77SETUP
The command G77 will compile and link your program and produce an executable file that can
be run under Windows 95 or Windows NT, as well as under DOS. There are various command
options (see below) which need to be specified before the name of the program file(s). A
simple demonstration program called MYTEST.F is included in the package. To compile this
just use:
G77 MYTEST.F
This generates a file A.EXE, which can be executed simply using the command:
MYTEST
The compiler by default names the produced executable file as "A.EXE". If one wishes to
override this default behavior, then one can specify the desired name of the produced
executable, using the "-o" compiler option. E.g., to produce an executable with the name
MYTEST.EXE the above command should be:
Figure 3, below, shows the output from the DOS window showing the initialization of the
system (G77SETUP), compilation, and a sample run of program MYTEST.F.
It is good practice to get the compiler to warn you of unused and undeclared variables, the
switch to do this is "-Wall" which can be used like this:
Note that the switches are case-sensitive: -WALL will not work.
The g77 compiler has a large number of other command switches - a few of
the most useful are shown here:
4
-O Optimise code generation
-Wimplicit Warns of any names with no explicit data type
-Wuninitialised Warns of some cases of unset variables (if -O also set).
-Wall Warns of both of above cases.
Any number of source-code files can be used on the command-line, and wild-cards (*) are
supported in file-names. The command-line can also contain compiled object modules (.OBJ)
and library files (called "archives" in Unix-speak) which have .A as the extension. File-names
are, of course, not case-sensitive in under Windows 95 or NT.
Figure 3. DOS window output showing initialization of the g77 system and compilation and
running of mytest.f
FORTRAN DOCUMENTATION
Documentation for GNU FORTRAN G77 is provided in a collection of html documents located in
subdirectory C:\G77\DOC. These documents can be read using any web browser such as
NETSCAPE Communicator or MICROSOFT Internet Explorer. To use the documentation open the
file called g77_toc.html to access its Table of Contents. Then select any topic from the Table
of Contents and click on the corresponding text.
http://www.geocities.com/Athens/Olympus/5564/
5
You will be directed to a webpage where you can download a PostScript version of the
textbook. The textbook is about 120 pages long .
MATHEMATICAL & GRAPHICS PACKAGE FOR USE WITH GNU FORTRAN G77
http://www.geocities.com/Athens/Olympus/5564/
Click on the text " SLATEC mathematical library for G77" to obtain a mathematical library that
can be used with GNU FORTRAN G77. You can also click on the text "PSPLOT scientific graphics
library for G77" to obtain a graphics library for use with GNU FORTRAN G77. The webpages so
accessed contain all the information you need to download the library files and their
documentation.
Notes:
(1) To be able to see and print graphics output from the PSPLOT graphics library you need to
download the software Aladdin Ghostscript from
http://www.cs.wisc.edu/~ghost/aladdin/get510.html
(2) To see some of the related documentation that uses the PostScript format you will need to
download the software Adobe Acrobat from
http://www.adobe.com/prodindex/acrobat/readstep.html
You can save your FORTRAN programs in any subdirectory in your hard disk or in floppy disks.
For example, create a subdirectory called MYPROGS within subdirectory G77. Move within
subdirectory MYPROGS and type in the following program using Notepad or PFE:
C12345678901234567890
Program TEST1
integer i
real x(100)
do i = 1, 100
x(i) = i*i
write(*,*) i, x(i)
end do
end
Make sure that each line in the file starts in the 7-th position, or beyond, as shown above.
Save the file as TEST1.F.
6
Next, open a DOS window, and move to subdirectory MYPROGS by using:
CD C:\G77\MYPROGS
C:\G77\G77SETUP
If compilation is successful (and it is not always so), you will get the DOS prompt:
C:\G77\MYPROGS
DIR
TEST1
The output is a list of values from 1 to 100 and its corresponding squares.
You may have noticed, when using DIR, that while TEST1.F uses 1KB of memory, the executable
file TEST1.EXE uses about 265 KB. So, unless you will be using the executable program over
and over it is a good idea to purge the executable file once you have successfully compiled it.
From the DOS prompt you can use the command:
DEL TEST1.EXE
As you can see from this example you can activate G77 from any subdirectory just by typing the
complete address of the batch file C:\G77\G77SETUP
Once G77 is activated, you can compile your program from any subdirectory as we did for
TEST1.F in subdirectory C:\G77\MYPROGS. Please make sure the source file (in this case,
TEST1.F) exists in the subdirectory that you are currently using.