Lecture 7 Demos:

Second pass

queens.c, checkboard.c, printboard.c, queens.h.

We want to be able to compile the pieces of the program separately, but the danger demonstrated by the first pass is that it is all too easy to get the pieces out of sync with each other.

The first part of the solution is to create a header file, queens.h, which will contain all of the definitions that should be shared amongst the separate source files.

  • A header file is a good place for important constants (#define's,) typedef's, struct definitions, and function prototypes.

  • Notice that each of the source (.c) files now has near the top, This causes the preprocessor to insert the text of queens.h in that location before passing the source file on to the compiler. Note that the use of double-quotes indicates that the header file is in the current directory, rather than in a well-known location, as suggested by the angle-braces used in other #include lines.

    To compile the program, we can either compile each of the source files separately,

    and link them together into an executable,

    Or we can compile and link them all at once.

    But this is still less than ideal. We want to be able take advantage of separate compilation; if we change the contents of the header file, but forget to recompile each of the source files, it is possible to still have stale object (.o) files sitting around.

    Why doesn't someone write a tool to make this easier?

    Third Pass.