An Easy Explanation of MS-DOS Compilation for the Liberated Linux Developer

Create 16-bit DOS games!

Setup

  1. Install DOSBox and add to your $PATH

  2. Download Turbo C and extract to an accessible location (e.g. ~/turboc)

  3. Create your project folder (e.g. ~/project)

Project Structure

You'll want to create the following files:

~/project ├── build.dosbox.conf ├── build.sh ├── run.sh ├── build | ├── .gitkeep └── src ├── main.c

build.sh

#!/usr/bin/env bash SDL_VIDEODRIVER=dummy dosbox -conf ./build.dosbox.conf -noconsole cat ./build/TCC.LOG

The SDL_VIDEODRIVER=dummy and -noconsole options hide the DOSBox window on Linux and Windows.

You can check for build errors by looking for the presence of ./build/TCC.LOG:

#!/usr/bin/env bash set -e executable="./build/GAME.EXE" buildlog="./build/TCC.LOG" SDL_VIDEODRIVER=dummy dosbox -conf ./build.dosbox.conf -noconsole if [ ! -f "$buildlog" ] then echo 'Error: No build log found.' exit 4 fi cat "$buildlog" if grep --quiet 'error\|Error\|Undefined symbol' $buildlog then echo 'Error(s) during compilation.' exit 5 elif [ ! -f "$executable" ] then echo 'Error: No executable found.' exit 6 fi

run.sh

#!/usr/bin/env bash dosbox ./build/GAME.EXE

If your game requires specific DOSBox settings, then you can make another configuration file:

#!/usr/bin/env bash dosbox -conf run.dosbox.conf ./build/GAME.EXE

build.dosbox.conf

The [autoexec] section mounts our source code, compiler, and build destination, and then compiles the code. DOSBoxWiki has more details on this file.

[autoexec] MOUNT S: ./src MOUNT T: ~/turboc MOUNT O: ./build DEL O:\TCC.LOG T: TCC -1 -ml -IT:\INCLUDE -IS:\ -LT:\LIB -eGAME.EXE -nO:\ S:\*.c > O:\TCC.LOG EXIT

A breakdown of these Turbo C arguments:

  • -1 - Target 80186/286 (Optional - targets 80386 when omitted)

  • -ml - Uses the Large memory model, which we need to access VGA video memory

  • -IT:\INCLUDE - Includes the Turbo C header files (such as DOS.H)

  • -IS:\ - Includes your own header files in ./src

  • -LT:\LIB - Includes Turbo C's own libraries

  • -eGAME.EXE - Sets the compiled executable name

    • If you change this name, keep in mind the usual MS-DOS 8.3 file lengths.

  • -nO:\ - Outputs the compiled file(s) to O: (a.k.a. ./build)

  • S:\*.c - Compile your C source files in ./src

  • > O:\TCC.LOG - Redirects the output to ./build/TCC.LOG

main.c

  1. Download this VGA color palette tester: main.c

  2. Run the build/run scripts to see if everything's in working order:

./build.sh && ./run.sh

A screenshot of DOSBox running a demo program

More Caveats

For compatibility with Turbo C:

  • Comments must be block comments (/* ... */)

  • Source files must Windows line endings (CRLF)

  • Variables must be declared at the beginning of scope blocks

Comments