Previous Slide Separate Compilation Next Slide

Let's review what we need to do to compile this program separately (i.e., to intermediate object files) and then link it into an executable named main.

To just compile source code, use the -c flag with the compiler...

% g++ -c main.cpp
% g++ -c Point.cpp
% g++ -c Rectangle.cpp

This will generate the object files:

  • main.o (for main.cpp),
  • Point.o (for Point.cpp), and
  • Rectangle.o (for Rectangle.cpp)

Then, to link the object files (.o) into an executable, we use the compiler again (although this time it will just pass the .o files on to the linking stage):

% g++ -o main main.o Point.o Rectangle.o


BU CAS CS - Writing Make Files
Copyright © 1993-2000 by Robert I. Pitts <rip@bu.edu> All Rights Reserved.