Computing: Computer Basics

FreeDOS: Installing and running Gnu Fortran.

FORTRAN is one of the very first higher (i.e. other than assembly) programming languages, but it is still used today, in particular to realize complicated scientific calculations that have to be executed at high speed, as for example in meteorology, or super-computer benchmarking. Intel has a FORTRAN compiler in their free development suite for the latest Windows releases; it may be fully integrated in the Visual Studio environment. GFortran is part of the GNU Compiler Collection. It is fully compliant with the FORTRAN 95 Standard and includes legacy F77 support. In addition, a significant number of FORTRAN 2003 and FORTRAN 2008 features are implemented. G77 was the predecessor of GFortran. It implemented FORTRAN 77 and added several FORTRAN 90 features. Unfortunately, with G77 becoming GFortran, support for DOS was abandoned. So, there is no GFortran for DOS, and if we want to use FORTRAN as a part of the GNU Compiler Collection, we'll have to content with old G77.

This tutorial describes the installation of G77 for DOS 0.5.19 (released in 1996) on FreeDOS 1.3 RC5. It should also work with other versions of FreeDOS, and probably will with MS-DOS, PC-DOS, and others. I have downloaded G77 from the Free FORTRAN Compilers page of professor H. Roumani, Dept. of Computer Science, York University, Toronto, Canada. There are two major files available at this site: (the compiler files) and (the documentation). These files are meant to be run in Command Prompt of Windows 95, 98, NT and 2000 using the EMX DOS-extender (that is included in the download archive). They also run on FreeDOS, where I installed them into my DJGPP installation and where I successfully build FORTRAN 77 programs using RHIDE as IDE (concerning DJGPP and RHIDE, please, have a look at my Installing and running DJGPP on FreeDOS tutorial).

Note: In fact, I thought that I'd installed G77 on top of DJGPP, but I think that that's not true and that the builds are done using EMX and not DJGPP. On the other hand, installing G77 into the DJGPP folders is probably the simplest way to use RHIDE as IDE.

I also downloaded the SLATEC Mathematical Library (binaries for G77) and the SLATEC routines documentation, the PSPLOT Scientific Graphics Library (binaries for G77) and the PSPLOT manual (PDF), later-on the SLATEC sources and the PSPLOT sources. Bad news, however: I did not succeed to build G77 programs using these libraries...

Installing G77.

I extracted the compiler, the documentation, and the SLATEC and PSPLOT binaries on my Windows 10 machine and moved the extracted files to common directories bin, lib and doc. I then burned these folders onto a CDROM (aka included them into an ISO that I mounted in my virtual CD-drive).

If you compare the content of the DJGPP folders with the folders on the CDROM, you'll see that there are several file duplicates: in the bin folder: ar.exe, as.exe, gcc.exe and ld.exe; in the lib folder: crt0.o. The question is: Must we keep the files that came with DJGPP, or must we overwrite them with the files included with G77? None of these two possibilities will work! We cannot keep the old files, because G77 only works with the files that came with its download archive; replacing the files will allow to build FORTRAN programs, but C and C++ will not work any longer. Thus, we'll have to install both versions of these files. For the moment lets rename the DJGPP files with an extension of .djc. This will allow to copy all G77 files without having any duplicate problems.
    cd \djgpp\bin
    rename as.exe as.djc
    rename ar.exe ar.djc
    rename gcc.exe gcc.djc
    rename ld.exe ld.djc
    cd ..\lib
    rename crt0.o crt0.djc

To copy the binaries from the CDROM (drive F: on my machine) to the C:\djgpp\bin folder, run the following commands:
    cd \djgpp\bin
    cd \bin
    copy *.* c:

G77 on FreeDOS: Copying the G77 binaries to the DJGPP 'bin' directory

We can copy the G77 library files in a similar way (to C:\djgpp\lib):
    cd \djgpp\lib
    cd \lib
    copy *.* c:

The doc directory on my CDROM has subdirectories, thus, we'll have to use the xcopy command to copy the documentation (to C:\djgpp\doc):
    cd \djgpp\doc
    cd \doc
    xcopy *.* c: /s /i /h /q

The screenshots below show the content of the DJGPP bin directory (on the left) and of the DJGPP lib directory (on the right). Notice the renamed DJGPP files and their G77 duplicates with the actually correct extension.

G77 on FreeDOS: Content of the DJGPP 'bin' folder after copy of the FORTRAN files
G77 on FreeDOS: Content of the DJGPP 'lib' folder after copy of the FORTRAN files

Testing the G77 compiler.

To run the G77 compiler and linker, we'll have to include the directory containing the binaries to the PATH, and we'll have to specify the path to the directory that contains the libraries (by setting the environment variable LIBRARY_PATH). We can do this with:
    set path=%path%;c:\djgpp\bin
    set library_path=c:\djgpp\lib

Here the code of a simple "Hello world" program in FORTRAN (I called it hello3.f and placed in my d:\devel\djgpp directory):
    program hello
        write (*,*) "Hello World!"
This is not a typical FORTRAN 77 program. In fact this legacy version of FORTRAN uses the 80-columns fixed format used with punch-cards in the time. G77 supports fixed format, of course, but also allows to use free format (as defined in FORTRAN 90). However, if we use free format, we'll have to indicate this by a compiler flag when building the program. The actual directory being d:\devel\djgpp, we build hello3.f using the following command:
    g77 -ffree-form hello3.f

The screenshot shows the setting of the path and of the library related environment variable, the build command and the resulting executable, and finally the execution of hello3.exe.

G77 on FreeDOS: Building a free-form FORTRAN program (FreeDOS command line)

Building G77 programs using RHIDE.

As I described in my Installing and running DJGPP on FreeDOS tutorial, RHIDE works at a project level (not on an individual source file level). So we have to create a new project (lets call it hello3) and then add the FORTRAN source file (hello3.f) to this project. The new project is created by launching RHIDE using the command
    rhide hello3
This opens an empty project. Hitting the INSERT key on the keyboard (or clicking Add with the mouse) opens a list of the files located in the current directory. Using the TAB key and then the up and down arrow keys, we can select hello3.f (screenshot on the left). With the Add Item window closed and hello3,f selected in the project window, hitting ENTER will open the FORTRAN source in the editor window. We suppose here that the path and the library path have been set as explained above. However, when choosing Compile > Compile from the menu, we get a lots of error messages (screenshot on the right).

G77 on FreeDOS: RHIDE - Adding a FORTRAN source file to a new project
G77 on FreeDOS: RHIDE - Trying to compile a FORTRAN program (free format flag not set)

I suppose that you guess the reason? The free format flag has to be set... In RHIDE, this is done by choosing Options > Compilers > Fortran-Flags from the menu. This opens the Fortran Flags window with of long list of options. Scroll down and select the checkbox in front of ffree-form.

G77 on FreeDOS: RHIDE - Setting the ffree-form G77 compiler flag

Now the compilation terminates without problems, but when trying to build the project ld (the linker) issues the message Error: ld: No such file or directory for g2c.a.

G77 on FreeDOS: RHIDE - Linker error when trying to build a FORTRAN program

FORTRAN uses the extension .a for its library files, thus the error message suggests that one of the libraries needed to build hello3 is missing. If you have a look into the c:\djgpp\lib directory, you can see that this file effectively isn't there. However, there is a file with a similar name: f2c.a. Should this be the file that we need? It is! Just make a copy of f2c.a and name it g2c.a:
    copy f2c.a g2c.a
and the build of hello3 succeeds (screenshot on the left). With RHIDE configured to redirect STDOUT to a window of the IDE (cf. my Installing and running DJGPP on FreeDOS tutorial), we can run the executable resulting from this build, using the menu command Run > Run. The screenshot on the right shows the result.

G77 on FreeDOS: RHIDE - Successful build of a FORTRAN project
G77 on FreeDOS: RHIDE - Running a FORTRAN program with STDOUT redirected to a window within the IDE

Custom batch file to start the DJGPP environment.

Starting the DJGPP environment is not really correct, because, as I mentioned above, G77 runs on EMX rather than on DJGPP. Anyway, the batch file, that I describe here, is an extension of the one described in my Installing and running DJGPP on FreeDOS tutorial. This original batch file, called by djgpp# <project-name>, sets the path to the DJGPP binaries, and the DJGPP environment variable, sets the current directory to the directory with my DJGPP sources and finally starts RHIDE; if the project specified exists, it is opened, if not, a new project of this name is created.

The new batch file must handle a supplemental task: making sure that the correct version of the duplicate files is used, i.e. when we want to build a C or C++ project, the files included with DJGPP have to be used, with a FORTRAN project, the files included with G77 must be used. Also, the LIBRARY_PATH environment variable has to be set in this case.

First of all the batch script must know if want to build a C/C++ or a FORTRAN project, so it will need two command line arguments: the first to specify the programming language, the second to specify the project name. Using switches for the first parameter (/c for C/++, /f for FORTRAN), the general format of the command to start the DJGPP environment becomes:
    djgpp# /c|/f [<project-name>]
where "|" indicates that either /c or /f has to be specified (if none is specified the script will abort with an error message), and where "[]" indicates an optional parameter (if no project name is specified, the project "noname" will be opened).

The implementation of this "file switching" is rather simple:

  1. Taking a one-time manual action to rename the DJGPP files with a .djc extension and the G77 files with a .djf extension
  2. At the beginning of the script, testing if a correct language parameter has been specified; if not abort with error message
  3. Deleting the duplicate .exe files (to be sure that we don't use the wrong ones)
  4. Depending on the programming language switch, making copies of the DJGPP (.djc) resp. the G77 (.djf) files giving these copies their "real" extension (.exe for the binaries, .o for the library file crt0).

Concerning point 1, the .djc files (to be used with C and C++) have already been created. Remains to create the .djf files (to be used with FORTRAN). As the actual .exe files are those that came with G77, we can create the .djf files by renaming or copying those files, just as we did for the .djc files before.

And here the code of my actual djgpp#.bat file:
    @echo off
    if "%1"=="/f" set _LANG=fortran
    if "%1"=="-f" set _LANG=fortran
    if "%1"=="/c" set _LANG=c
    if "%1"=="-c" set _LANG=c
    if "%_LANG%"=="" goto Error
    del C:\DJGPP\LIB\CRT0.O
    if "%_LANG%"=="fortran" goto Fortran
    set DEVEL=djgppc
    goto Common
    set DEVEL=djgppf
    goto Common
    echo Invalid command line arguments
    echo First parameter must be programming language: /c or /f
    goto End
    if "%2"=="" goto Default
    RHIDE.EXE %2
    goto End
    set _LANG=

Some notes concerning the code: 1. Beside the DOS-format switches (using a "/"), the script also accepts Unix-format switches (using "-"). If you usually type using uppercase letters, you should modify the 4 switch-related commands, or add "if" statements with uppercase C and F. 2. DEVEL is a custom environment variable for my system; you can simply ignore it. 3. My DJGPP sources are located in the directory D:\DEVEL\DJGPP; you'll have to adapt this to your own system.

FORTRAN 77 basics.

FORTRAN is a rather easily to learn programming language, especially if you have some knowledge of Pascal, C or similar. If you'd like to play a little bit around with G77, my A (very short) introduction to FORTRAN 77 (G77) text may be helpful. It's not really a tutorial, in fact it's nothing more than 3 simple program examples, showing how variable declarations, calculation expressions, arrays, decision making (IF statements), loops (DO statements), reading from the console (keyboard input), writing to the console (screen output) or a text file, or subroutines are coded in FORTRAN. The whole with comments and explanations. You may see it as a quick look-up for common FORTRAN statements, or as template code for your own first FORTRAN programs.

If you find this text helpful, please, support me and this website by signing my guestbook.