Getting Started Gnu Fortran

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Getting started with GNU FORTRAN G77

By Gilberto E. Urroz
Created in August 1999 – Modified on September 2002

WHAT IS GNU FORTRAN G77

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.

HOW TO GET GNU FORTRAN G77

You can download a zip file containing the GNU FORTRAN files by accessing the following URL:

http://www.geocities.com/Athens/Olympus/5564/

Within that webpage you will find the following options:


__________________________________________________________________________________
Locally available files:

• A minimal set of G77 for Win32, binaries ( Mar. 29, 1998)


• SLATEC mathematical library for G77, binaries
• PSPLOT scientific graphics library for G77, binaries

Links to remote files:

• Professional Programmer's Guide to Fortran 77, free book


__________________________________________________________________________________

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

Figure 2. Select directory C to store the file "g77exe.zip."

HOW TO INSTALL GNU FORTRAN G77 IN YOUR COMPUTER

In the same webpage (http://www.geocities.com/Athens/Olympus/5564/g77.htm) you will


find the following information:

__________________________________________________________________________________
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:\

Then use the commands:

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.

HOW TO CREATE FORTRAN SOURCE CODE

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.

The PFE software

The PFE software is freeware and available for download at

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

How to use the compiler

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:

G77 MYTEST.F -o MYTEST.EXE

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:

G77 -Wall MYTEST.F

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:

-c Compile-only: produces .OBJ files.


-ffree-form Selects free-format source code
-fpedantic Warns of non-portable/non-standard code.
-fno-automatic Static storage for all variables, like universal SAVE
-fno-backslash Interprets "\" as a normal character in strings
-fvxt Use VAX Fortran interpretation of certain syntax
-g Produces debugging information.
-Idirectory Specifies directory to search for INCLUDE files

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.

For a free book on FORTRAN 77, go to the webpage

http://www.geocities.com/Athens/Olympus/5564/

and click on the text:

Professional Programmer's Guide to Fortran 77, free book

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

In the original GNU FORTRAN 77 webpage, namely,

http://www.geocities.com/Athens/Olympus/5564/

you had the following options:


__________________________________________________________________________________
Locally available files:

• A minimal set of G77 for Win32, binaries ( Mar. 29, 1998)


• SLATEC mathematical library for G77, binaries
• PSPLOT scientific graphics library for G77, binaries
__________________________________________________________________________________

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

AN EXAMPLE OF COMPILATION AND FILE MANIPULATION

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

Next, activate G77 by using:

C:\G77\G77SETUP

Next, compile program TEST1 by using:

G77 TEST1.F -o TEST1

If compilation is successful (and it is not always so), you will get the DOS prompt:
C:\G77\MYPROGS

To see if an executable file (TEST1.EXE) was created type

DIR

If the file TEST1.EXE exists, you can run it by typing

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

To delete the executable file 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.

You might also like