Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ -- When recompilation is required

Tags:

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
like image 712
q0987 Avatar asked Oct 27 '10 13:10

q0987


People also ask

Why does C need to be compiled?

Compiling a C program:- Behind the Scenes. C is a mid-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine.

Why do we need to compile C++?

Because computer architecture is made up of electronic switches and cables that can only work with binary 1s and 0s, you need a compiler to translate your code from high level C++ to machine language that the CPU can understand.

Does C++ have to be compiled?

Each C++ source file needs to be compiled into an object file. The object files resulting from the compilation of multiple source files are then linked into an executable, a shared library, or a static library (the last of these being just an archive of object files).

What is the role of compiler in C++?

The principal use of any compiler is to transform a program written in a high-level language like C++ into a data file that is executable by the target computer hardware.


1 Answers

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:

  1. Adding non-virtual functions anywhere in the class does not break BC.
  2. Changing any function definition (adding parameters )will break BC.
  3. Adding virtual functions anywhere changes the v-table and therefore breaks BC.
  4. Adding data members will break BC.
  5. Changing a parameter from non-default to default will not break BC.
  6. Any changes to inline functions will break BC (inline function should therefore be avoided if BC is important.)
  7. 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.

like image 139
doron Avatar answered Sep 29 '22 03:09

doron