thecodingidiot.com

f05-building-c

Building C

The Workshop

In December 1997, id Software released the Doom[1] source code to the public.

Doom cover art
Screenshot of the player armed with a chainsaw confronting an undead soldier with a shotgun
Doom's intense level of graphic violence, as seen in this gory effect of a rocket hitting a group of demons

Inside the tarball, in a directory called linuxdoom-1.10, there is a file called Makefile. A few dozen lines of declarations and rules. It assumes a Unix box with gcc and make. That file is what built the version of Doom that ran on Linux from 1994 onward — id programmer Dave Taylor's[2] port, compiled from the same C source the DOS version used.

The 1999 Quake[3] source release ships with Makefiles. The id Tech 3[4] release does. Every C-era game whose code you can read in public was built by typing make at a prompt and waiting.

There is no version of "shipping a C program" that does not involve a build system. There is no story of fixing a bug in a real C program that does not involve a debugger. There is no modern memory-safety story that does not involve at least one of valgrind, AddressSanitizer, or both.

You wrote your first C programs in the previous chapter and ran them with gcc hello.c -o hello. That worked because each program was one file and never crashed. The next chapter is where the real curriculum starts: multi-file projects, libtci, shells, threading. Before that, you need the workshop. This chapter is the workshop.

The implementation pages walk you through writing a tiny sort utility, watching it misbehave, fixing it with the right tool for each kind of misbehaviour, and then refactoring it into a multi-file project with a Makefile. By the end you have the toolchain id used to ship Doom, configured the same way they configured it. Start at Setup.

Implementation

Setup

Begin Implementation
  1. 0Setup
  2. 1Writing Sort
  3. 2Compiler Flags Worth Using
  4. 3It Crashes
  5. 4The Leak
  6. 5The Overflow
  7. 6A Real Project
  8. 7The Real Curriculum

Footnotes

  1. Doom (1993 video game) - Wikipedia

  2. Dave Taylor (programmer) - Wikipedia

  3. Quake (video game) - Wikipedia

  4. id Tech 3 - Wikipedia