-->
Fortran - Compiling in Unix

Fortran - Compiling in Unix

I. Introduction

Fortran was one of the first high level language, if not the first! It was created by John Backus, an engineer at IBM, in 1955-1957 when computers were extremely slow and most programming was done at the machine language level. Fortran is still widely used today in mathematical and scientific communities, almost a half century later. There have been several updates to the Fortran language in 1977, 1990, 1999, and again in 2000. Each of these versions of the language has a separate compiler to use. Your Instructor or TA will tell you which version they want you to use on your homework and projects. The department has the following versions installed for students to use:

Fortran 1977 use compiler: f77
Fortran 1990 use compiler: f90
Fortran 1999 use compiler: f99

II. Writing a Fortran program

In order to write a Fortran program you need to be able to type it into the computer and save it to disk so that the compiler can translate the program to machine language which can be executed by the computer. There are two options, you can write your program in Windows using notepad or you may write your program in UNIX using pico, vi, or emacs. Since the departments Fortran compiler is on UNIX there are a few extra steps to convert between Windows and UNIX.

Sample Hello World Program:
PROGRAM HELLO
PRINT*, 'Hello World!'
END

In all the examples below I use the f90 compiler, it is customary to name a Fortran program by the version it is written for. Thus I will put ".f90" at the end of my examples.

Using notepad to write your Fortran program,
  1. Open notepad, it is located under the Start menu.
  2. Write your program; see the hello world example above as an example.
  3. Save your program to your H: drive. The H: drive is accessible both by Windows and UNIX.
  4. Use Putty to SSH into UNIX
  5. Since the text file formats are different between Windows and UNIX you will need to use "dos2unix" and "unix2dos" to convert the files between the two types of systems. These two commands need to be typed in at the UNIX command prompt as shown below:
    To convert from Windows to UNIX:
    % dos2unix MyProgram.f90 MyProgram.f90
    To convert from UNIX to Windows
    % unix2dos MyProgram.f90 MyProgram.f90
Using UNIX to write your Fortran program,
  1. Use Putty to SSH into UNIX.
  2. Use vi, emacs, or pico (pico is the easest to use) to write your program.
  3. Save your program.

III. Compiling a fortran program

The department's Fortran compilers are only available on UNIX so even if you wrote your program using windows you must login to unix to compile your program.

  1. Once your program has been written, use Putty to SSH into UNIX
  2. Compile your program using the compiler your Instructor or TA instructed you to use, such as f99 or f90.

    % f90 MyProgram.f90 -o MyProgram

  3. The compilation step above may give you errors, if so you need to fix those errors and then recompile. Once you can compile your program without any errors, run your program by:

    % ./MyProgram

IV. Trouble Shooting

If you see errors like the following on every line of your program you need to use the dos2unix commands before compiling. See "Using notepad to write your Fortran program" for information on the dos2unix commands.

"MyProgram.f90", Line = 1, Column = 16: Error: Unexpected syntax: "EOS.." Expected but found

Windows uses "new line" and "carriage return" at the end of each line, where UNIX only uses "new line". These two commands remove or add the "carriage return" to convert between the two systems.


To Top of Page To Top Of Page Printer Friendly Version

©CSG Helpdesk

Valid CSS!