C++ -- When recompilation is required

c++

Solution

Classes are defined in the header file. The header file will be compiled into both the library that implements the class and the code that uses the class. I am assuming that you are taking as a given that you will need to recompile the class implementation after changing the class header file and that the question you are asking is whether you will need to recompile any code that references the class.

The problem that you are describing is one of binary compatibility (BC) and generally follows the following rules:

- Adding non-virtual functions anywhere in the class does not break BC.

- Changing any function definition (adding parameters )will break BC.

- Adding virtual functions anywhere changes the v-table and therefore breaks BC.

- Adding data members will break BC.

- Changing a parameter from non-default to default will not break BC.

- Any changes to inline functions will break BC (inline function should therefore be avoided if BC is important.)

- Changing compiler (or sometimes even compiler versions) will probably break BC unless the compilers adhere strickly to the same ABI.

If BC is a major issue for the platform you are implementing it could well be a good idea to separate out the interface and implementation using the Bridge pattern.

As an aside, the C++ language does not deal with the Application Binary Interface (ABI). If binary compatibility is a major issue, you should probably refer to your platform's ABI specification for more details.

Edit: updated adding data members. This will break BC because more memory will now be needed for the class than before.

Problem

You have a class that many libraries depend on. You need to modify the class for one application. Which of the following changes require recompiling all libraries before it is safe to build the application? - add a constructor - add a data member - change destructor into virtual - add an argument with default value to an existing member function

Original source