What happens if you compile a C++ source file? The answer is very obvious: an object or executable file is generated. The executable or the binary files contains machine executable instructions and some metadata of the machine instructions.
Now the next question arises- how are the machine instructions generated out of a C++ source file? Does the CPU understand object oriented concepts like inheritance, polymorphism etc? Theoretically a CPU can be designed which can understand the object oriented concepts directly. But the popular x86 or x64 processors do not understand the object oriented concepts directly. These CPUs can execute only a definite set of instructions. And these instructions are sequential and procedural in nature.
But if you compile a C++ source file, some machine instructions are generated. These machine instructions are only from a allowed set of instructions that CPU supports. That means the Object Oriented concept is implemented using the machine instructions.
This article tries to explain how C++ code gets translated into low level instructions. In this article, instead of using the low level machine instructions (or assembly language), we will use C language code snippet to see how a C++ code is translated into low level. This is done because the C is very easy to understand and understanding how a C code is translated into machine instructions is pretty straight forward.
Who should read this?
- Anyone who is curious to understand what happens behind the scene. Understanding these things often helps you to write good code.
- If you are a system programmer and write mix-mode code (C, C++, assembly) then this will be very helpful.
Pre-req: Knowledge of C, C++.
NOTE: The actual structure of the generated code depends on the computer architecture, operating system and compiler. In this article we will not go with the exact generated code but will work with a simplified model. This is done to simplify things so that readers can understand without dealing with the complication of a operating system, computer architecture or a particular compiler. The author has explored most of the things by reverse engineering the generated code by g++ on Linux. So the concept described in this article will more close to code generated by g++. But the code generated by other compilers will look very similar.