CS 70

Exercise: Compiling a Multifile Program

Say we have a program that consists of four .cpp files:

  • cow.cpp
  • barn.cpp
  • chicken.cpp
  • farm.cpp

How many times do we have to run clang++ to generate an executable file named farm?

Now say we make a change in chicken.cpp but leave the rest of the files the same.

How many times do we need to run clang++ to update the executable?

Key Ideas:

  • We need to compile each .cpp file into a .o file, then one more command to link them together to create the executable.
  • When we change a file, we only need to recompile that one file and then re-link. This is another advantage to splitting up our program—we only need to compile parts of our program that change.
  • LHS Cow speaking

    Okay, we've got a chunk done, let's head up to the parent page to see our progress and take a break!

(When logged in, completion status appears here.)