Computing: DOS, OS/2 & Windows Programming

Building Open Watcom C/C++ and Fortran 77 programs on FreeDOS.


Open Watcom is a C/C++ and Fortran 77 development environment with many cross-compilation targets. At the times of DOS, Watcom was a very popular compiler for high-end games because it produced very efficient binaries in memory-constrained environments. Watcom lives on as Open Watcom, with an official release at version 1.9 and an unofficial fork at version 2.0.

This tutorial describes the installation of Open Watcom 1.9 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 used the official FreeDOS Open Watcom packages from the FreeDOS 1.3 Bonus CD. I unzipped the package archives on my Windows 10 into two folders "watcomc" and "watcomf", and used these folders to create a CD ISO.

The ZIP contents are standard FreeDOS folder structures and you can use the FreeDOS commands COPY ans XCOPY to copy the new files to your FreeDOS installation. In my case, with F: being my CDROM drive, F:\WATCOMC containing the C/C++ files, F:\WATCOMF containing the Fortran 77 files, and FreeDOS being installed in C:\FREEDOS, the following commands copy the appinfo files:
  f:
  cd \watcomc\appinfo
  copy *.* c:\freedos\appinfo
  cd \watcomf\appinfo
  copy *.* c:\freedos\appinfo
To create the installation directories (in C:\DEVEL; this directory having to be created, too, if it does not exist), use the following commands:
  c:
  mkdir devel
  cd devel
  mkdir watcomc
  mkdir watcomf
To copy the C/C++ files (to C:\DEVEL\WATCOMC), run the commands:
  c:
  cd \devel\watcomc
  f:
  cd \watcomc\devel\watcomc
  xcopy *.* c: /e /i /h /q
And for the Fortran files:
  c:
  cd \devel\watcomf
  f:
  cd \watcomf\devel\watcomf
  xcopy *.* c: /e /i /h /q

The screenshots below show the directory creation and the copy process for the C/C++ files and folders (on the left), and the Fortran file and folder structure within the FreeDOS installation (on the right).

Open Watcom on FreeDOS: Copying the C/C++ files to the FreeDOS installation
Open Watcom on FreeDOS: Fortran folder structure within the FreeDOS installation

Open Watcom environment variables.

To successfully build an Open Watcom program, you'll have to set the following environment variables:

Creating 16-bit executables for DOS.

To build 16-bit C/C++ programs for DOS, use the command:
  wcl /l=dos <source-file>.c
  wcl /l=dos <source-file>.cpp
This will compile <source-file>.c resp. <source-file>.cpp, creating the object file <source-file>.obj, that will be linked creating a 16-bit executable called <source-file>.exe.

To build 16-bit Fortran 77 programs for DOS, use the command:
  wfl /l=dos <source-file>.for
This will compile <source-file>.for, creating the object file <source-file>.obj, that will be linked creating a 16-bit executable called <source-file>.exe.

Simple "Hello world" program in C (hello1.c):
  #include <stdio.h>
  void main() {
    printf( "Hello world\n" );
  }

Build the program using the command
  wcl /l=dos hello1.c
The screenshot shows the last part of the Open Watcom output during the build, the files created, and the execution of hello1.exe.

Open Watcom on FreeDOS: Building a 16-bit C program for DOS

And here the corresponding code in C++ (hello2.cpp):
  #include <iostream.h>
  int main() {
    cout << "Hello world" << endl;
    return 0;
  }

Build the program using the command
  wcl /l=dos hello2.cpp
The screenshot shows the last part of the Open Watcom output during the build, the files created, and the execution of hello2.exe. Note, the presence of the file hello2.err. This file is created if there are any errors or warnings during the build. It's a simple text file and you can view its content by opening it with the FreeDOS text editor. As hello2.exe was executed, the message can't just be a warning. In a first version of my program, I omitted the line return 0, and this resulted in the message "main must return 'int'".

Open Watcom on FreeDOS: Building a 16-bit C++ program for DOS

The following Fortran 77 program (prime.for) counts the prime numbers between 1 and 10,000.
  * This program counts the prime numbers between 1 and 10,000
  * using the Sieve of Eratosthenes algorithm.
        IMPLICIT NONE
        INTEGER UPBOUND
        PARAMETER (UPBOUND=10000)
        INTEGER I, K, PRIMES
        LOGICAL*1 NUMBERS(2:UPBOUND)
        CHARACTER*11 FORM
        PARAMETER (FORM='(A,I5,A,I5)')
        DO I = 2, UPBOUND
          NUMBERS(I) = .TRUE.
        ENDDO
        PRIMES = 0
        DO I = 2, UPBOUND
          IF( NUMBERS(I) )THEN
            PRIMES = PRIMES + 1
            DO K = I + I, UPBOUND, I
              NUMBERS(K) = .FALSE.
            ENDDO
          ENDIF
        ENDDO
        PRINT FORM, 'The Number of Primes between 1 and ', UPBOUND,
       1            ' are: ', PRIMES
        END

Build the program using the command
  wfl /l=dos prime.for
The screenshot shows the last part of the Open Watcom output during the build, the files created, and the execution of prime.exe.

Open Watcom on FreeDOS: Building a 16-bit Fortran 77 program for DOS

Creating 32-bit executables for DOS.

The Open Watcom compilers allow to build 32-bit programs for the following two DOS extenders: DOS/4GW and Phar Lap 386|DOS-Extender; only the first one is considered in this tutorial. To build 32-bit programs for DOS/4GW, use the commands:
  wcl386 /l=dos4g <source-file>.c
  wcl386 /l=dos4g <source-file>.cpp
  wfl386 /l=dos4g <source-file>.for
This will compile the C, C++ or Fortran 77 source-file, creating the object file <source-file>.obj, that will be linked creating a 32-bit protected mode executable called <source-file>.exe. To run this file on another DOS machine, the DOS/4GW extender must be installed; cf. my tutorial Running 32-bit applications on DOS.

I made copies of hello1.c and hello2.cpp (called hello1b.c and hello2b.cpp), and created the following simple Fortran program (hello3b.for).
        PROGRAM HELLO
        PRINT *, 'Hello World!'
        END

Here the commands to build these programs for DOS/4GW:
  wcl386 /l=dos4g hello1b.c
  wcl386 /l=dos4g hello2b.cpp
  wfl386 /l=dos4g hello3b.for

The screenshot shows how, on a MS-DOS 6.22 machine, I copy the extender from the floppy diskette to C:\DOS and then successfully run the three 32-bit protected mode programs build on FreeDOS with Open Watcom.

MS-DOS 6.22: Running 32-bit protected mode programs build with Open Watcom on FreeDOS

Open Watcom custom batch file.

I like to create batch files, and they can really be helpful. The following one allows to compile Open Watcom C/C++ and Fortran sources, creating either DOS (16-bit) or DOS/4GW (32-bit) executables. The script takes three parameters:

Here is the code of my watcom.bat:
  @echo off
  if "%1"=="-c" set _LANGUAGE=c
  if "%1"=="-cpp" set _LANGUAGE=cpp
  if "%1"=="-for" set _LANGUAGE=fortran
  if "%1"=="-f" set _LANGUAGE=fortran
  if "%2"=="-dos" set _TARGET=dos
  if "%2"=="-dos4g" set _TARGET=dos4g
  if "%2"=="-d4g" set _TARGET=dos4g
  set _SOURCE=%3
  if "%_LANGUAGE%"=="" goto NoLanguage
  if "%_TARGET%"=="" goto NoTarget
  if "%_SOURCE%"=="" goto NoSource
  if "%_LANGUAGE%"=="c" set _SOURCE=%_SOURCE%.C
  if "%_LANGUAGE%"=="cpp" set _SOURCE=%_SOURCE%.CPP
  if "%_LANGUAGE%"=="fortran" set _SOURCE=%_SOURCE%.FOR
  if "%_LANGUAGE%"=="fortran" goto Fortran
  :C
  set WATCOM=c:\devel\watcomc
  set PATH=%WATCOM%\binw;%PATH0%
  set EDPATH=%WATCOM%\EDDAT
  set INCLUDE=%WATCOM%\H
  if "%_TARGET%"=="dos4g" goto C4G
  set _COMPILER=wcl
  goto Build
  :C4G
  set _COMPILER=wcl386
  goto Build
  :Fortran
  set WATCOM=c:\devel\watcomf
  set PATH=%WATCOM%\binw;%PATH0%
  set EDPATH=%WATCOM%\EDDAT
  if "%_TARGET%"=="dos4g" goto Fortran4G
  set _COMPILER=wfl
  goto Build
  :Fortran4G
  set _COMPILER=wfl386
  goto Build
  :NoLanguage
  echo Missing or invalid programming language
  goto Usage
  :NoTarget
  echo Missing or invalid target platform
  goto Usage
  :NoSource
  echo Missing source file
  :Usage
  echo Usage: watcom -c/-cpp/-for -dos/-dos4g source-file
  goto End
  :Build
  set DEVEL=watcom
  D:
  cd \DEVEL\WATCOM
  %_COMPILER% /l=%_TARGET% %_SOURCE%
  :End
  set _LANGUAGE=
  set _TARGET=
  set _SOURCE=
  set _COMPILER=

Notes:

Examples:

Build the C++ source file hello2.cpp as 16-bit DOS program:
  watcom -cpp -dos hello2
Build the Fortran 77 source file hello3b.for as 32-bit DOS/4GW program:
  watcom -for -dos4g hello3b

Open Watcom development using a GUI.

Open Watcom is distributed with a custom version of Vi. Even though it is also possible to use the standard Vi Editor and configure it for Open Watcom, the Watcom version is lots more user-friendly, including a command menu as is usual for most applications, even DOS ones. The screenshots below show the Watcom Vi Editor, with a syntax-highlighted C++ program (on the left) and a syntax-highlighted Fortran 77 program (on the right).

Open Watcom on FreeDOS: C++ file opened in Open Watcom Vi Editor
Open Watcom on FreeDOS: Fortran 77 file opened in Open Watcom Vi Editor

There is an alternative to Vi: Power View is a complete IDE for Watcom C++ (unfortunately, it does not support Fortran). You can download it free of charge from the SAC website. The download is a ZIP archive that contains the different files, ready to use. Transfer them to your FreeDOS machine (for example, using a floppy diskette) and copy them to some directory; I chose C:\Powrview. The screenshot shows the files. The IDE is called w.exe; pve.exe is the Power Editor (similar to the IDE, but without the C++ related features).

Open Watcom on FreeDOS: Files included with the Power View IDE

In order to successfully run Power View, its installation directory has to be added to the PATH environment variable; also, we'll have to set all Open Watcom C/C++ environment variables. To set up the environment, set the current directory to the one containing my Open Watcom sources and start the IDE, all by one command, I created the batch file wcc#.bat, that runs Power View with all environment variables set. Here is the code:
  @echo off
  set WATCOM=c:\devel\watcomc
  set PATH=C:\POWRVIEW;%WATCOM%\binw;%PATH0%
  set EDPATH=%WATCOM%\EDDAT
  set INCLUDE=%WATCOM%\H
  set DEVEL=watcom
  D:
  cd \DEVEL\WATCOM
  c:\powrview\w.exe
To note that PATH0 is a custom environment variable of my system, set equal to PATH in FDAUTO.BAT (you will probably use PATH instead); DEVEL is another custom variable (that you can ignore resp. comment out or remove); D:\DEVEL\WATCOM is the directory with my Open Watcom sources (setting this as current directory allows to open files in Power View without having to change the directory).

The screenshot below shows Power view opening a C++ file.

Open Watcom on FreeDOS: Opening a C++ file in the Power View IDE

To compile the C++ source, choose Compile > Compile, or Compile > Build All from the menu bar (screenshot on the left). The compiler output will be displayed in the Log window (screenshot on the right).

Open Watcom on FreeDOS: Compiling a C++ file in the Power View IDE [1]
Open Watcom on FreeDOS: Compiling a C++ file in the Power View IDE [2]

This creates an object (.obj) file that we'll have to link using Compile > Link from the menu bar (screenshot on the left). The linker output will be displayed in the Log window (screenshot on the right).

Open Watcom on FreeDOS: Linking a C++ file in the Power View IDE [1]
Open Watcom on FreeDOS: Linking a C++ file in the Power View IDE [2]

You can run the DOS/4GW executable created using Compile > Run from the menu bar. To view the program output, choose Window > DOS screen. To return to the IDE, just hit a key. The screenshot shows this "DOS window" after I had compiled, linked and run hello2c.cpp. You can see the last part of my system startup (loading the NANSI.SYS driver and CuteMouse, and displaying the FreeDOS Welcome message), then the execution of the batch file wcc#.bat (that starts Power View), and finally loading the DOS/4GW runtime that executes the program hello2c.exe and this program's output.

Open Watcom on FreeDOS: Execution of the binary resulting from the build of a C++ file in Power View

Notes:


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