5. Separate Compilation #

Created Sunday 30 January 2022

Why separate compilation #

This is done to achieve an aspect of encapsulation: separate design and use of a class.

There are 3 kinds of files involved in the whole application:

  1. Interface files - the class with just member declarations. The file usually ends with a .h extension.
  2. Implementation files - contains the member function definitions. File extension is .cpp.
  3. Application file - contains the main function. FIle extension is .cpp.

How is separate compilation done? #

It’s a 3 step procedure:

  1. Both the implementation and application file need to include the interface file.
  2. The implementation and application files are compiled separately.
  3. The compiled files are then “linked” together to create the executable.

What are benefits of separate compilation #

  1. Independedence of compiled files from the source code. That is, source code does not need to be revealed in order for the application code to be written.
    • This is key for the working of proprietary software, i.e. source code remains private and only the comiled version with the interface file is made public.
    • Releasing the interface file and the compiled implementation is enough for writing and compiling the application file.
  2. It interface file is protected from change - as the original interface is already used in compiling the implementation files, so linking can only happen with the original interface file.
  3. The implementation file cannot be changed because it’s very difficult to “de-compile” the implementation’s compiled version.
  4. For very large applications, not all files have to be compiled if there’s a change at some place. Contrast this with a single file application, one would need to recompile the whole app for any change.

The effect of 1, 2, 3 is that the whole OOP design is protected from change by the end user.


Here’s an illustration of separate compilation: