COMP 2012H

Honors OOP and Data Structures

Lab 7: Separate Compilation

Objective

The objective is to learn the separate compilation and the usage of Makefile in both Eclipse IDE and Linux command-prompt environment 

Please refer to the following link if you are not familiar with the common Linux commands: http://files.fosswire.com/2007/08/fwunixref.pdf

 

Walking through a Simple Example

Suppose we have the following files and we want to make a color printing program out of them: blue.h, blue.cpp, green.h, green.cpp and main.cpp.

Solution 1: put everything in one line

In the console, type:

g++ blue.cpp green.cpp main.cpp -o printcolor

It works.

Solution 2: Separate compilation and makefile

One thing at a time:

g++ -c  blue.cpp
g++ -c  green.cpp
g++ blue.o green.o main.cpp -o printcolor

If we change green.cpp, we just need to rebuild green.o and printcolor, leaving the blue color files untouched.

Use a makefile to put everything together, thus every time we just need to type "gmake" to do compiling, assembling and linking.

About Makefile

Reference material for the Makefile:  makefile.pdf  (TA in your lab section will briefly go through the slides of the Makefile)

Create and Use a g++ Makefile

You can use gmake (which calls g++) to compile and link your program or object files. When using gmake, it looks for a file called makefile or Makefile. (The name Makefile is preferred as it can easily be distinguished from other files which typically have filenames in lowercase.) You can write all the rules in your makefile to simplify and speed up your compilation and linking process.

Sample Makefile.

To make and use a g++ makefile, do the following in the console:

1.      Create a text file called Makefile

¡¤    The simplest way is to type:

pico Makefile

               Or you can use any text editor like Vi or Emacs

2.      Edit the makefile, add:

¡¤    Variable definitions (optional), in the form:

VARIABLE_NAME = variable_definition

               Such as:

                  TARGET = printcolor

               Use the variable by $(VARIABLE_NAME)

¡¤    Dependencies followed by compilation rules in the second line, such as:

blue.o: blue.h blue.cpp

      $(CXX) $(CXXFLAGS) -c blue.cpp

        Which translates to, blue.o depends on blue.h and blue.cpp, compile blue.o using the rule: g++ (flags) ¨Cc blue.cpp.

        The tag all and clean specifies the final target to make, and the rules to clean up.

3.      Save and quit editing. Use the makefile by typing:

        make or make all

        and you should see the program being compiled and the target being created.

            Clean up all results by typing:

        make clean

Work on Makefile with Eclipse


Lab Task

Download

Lab Work

Declaration, Definition and Implementation

In practice, programmers always separate a segment of program source code into three parts: declaration, definition and implementation. The purpose is to separate the interface from the actual implementation to make the program easier to understand and reuse.  The objectives are to achieve:

For example, consider the class

class Date;

Class declaration: Introduce a class called Date to the compiler, but with only function prototypes without any detailed information provided yet

class Date {
   Date();
   // ... function prototypes and member variables
};

Class definition or implementation: Define the class Date's member variables and member functions so that codes can be generated.  The are in the form of the following

Date::Date() {
   // ... detailed implementation
};
¡¡
¡¡
// ...

Part I:

  1. Re-factor the program in date_all.cpp to make use of the idea of separate compilation, which is to separate the class Date's definition and implementation in the given date_all.cpp:
  2. Complete the code according to comment TODO
  3. Create a g++ Makefile so that it can:

¡¡

Part II: (Optional)

  1. In Eclipse, create a Makefile project called "date".
  2. Copy the re-factored source and header files (date.h, date.cpp, main.cpp) to the project.
  3. Complete the Makefile created by Eclipse, so that it can do the same thing as the g++ Makefile created in Part I.

References

 

 © CSE, HKUST | OpenDesign