Goals

  • to use the C preprocessor
  • to understand when linker might produce errors.

Activity

Examine the source files in ~cs50/examples/seuss, shown below.

  1. Try to compile the program. Why do you get an error? is it the compiler or linker reporting the error? what could you do to fix it?
  2. Run gcc -E seuss.c, which runs the C preprocessor only; this output is what the compiler would consume. Look at the output; ignore the # lines, which are annotations to help the compiler produce error messages that reference the original file name and line number. Notice how the preprocessor has copied everything from the include files right into the input, at the point where they are included in seuss.c, and then stripped out all the comments.
  3. After class, re-read the Lecture extra about the C preprocessor.
/* seuss.c - uses thing one and thing two. */

#include "one.h"
#include "two.h"

int main()
{
  int fun = thing_one() + thing_two();
  return fun; // exit status
}
/* one.h */

int one = 1;
extern int thing_one(void);
/* two.h */

int two = 2;
extern int thing_two(void);
/* one.c */

#include "one.h"

int thing_one(void)
{
  return one;
}
/* two.c */

#include "two.h"

int thing_two(void)
{
  return two;
}