How does C/C++ Program run Behind the Scene?
If you are not a reader, you can watch the same article in the form of a video👇 👇👇👇 OR click here to watch
Ok, so let’s get started.
This article is applicable to both C and C++. But I will use a sample C++ program to show how a program is compiled and executed behind the curtain.
Our Objective
We will explore how the source code is converted to executable code by tools such as Preprocessor, Compiler, Assembler, Linker, and Loader.
Let us consider the following program as our sample program written in C++.
Step 1: Preprocessor converts Source code to Expanded code.
Source code is a file that contains the C or C++ program.
When you run the program, the source code is first sent to the tool known as Preprocessor. Preprocessors basically do two things:
- It removes the comments from the program.
- It expands the Preprocessor directives such as macros or file inclusion.
It finally converts the my-file.cpp or my-file.c to my-file.i which contains the expanded source code (or simply expanded code).
What just happened?
- The comments were removed.
- The macros #define PI 3.14 was expanded i.e. it replaced the PI with 3.14 within the area() method.
- The file inclusion #include <iostream> expanded i.e. it got replaced by actual code present in the file iostream.
- The source code (my-file.cpp) was converted to expanded code (my-file.i).
Step 2: Compiler converts the Expanded code to Assembly code.
Next, is the role of the compiler. The compiler does two things:
- It checks the program for syntax errors.
- If no error is found, it converts the expanded code to assembly code.
Basically, the expanded code my-file.i is converted to my-file.s which contains the assembly code.
Step 3: Assembler converts the Assembly code to Object code.
In this step, the next tool known as Assembler converts the Assembly code into Object code.
Object code is also known as bytecode or binary code or machine-level code which is understandable by the computer.
On windows, the file containing the object code will be my-file.obj.
On macOS and Linux, the file containing the object code will be my-file.o.
Step 4: Linker converts the Object code to Executable code.
In this step, the tool known as Linker converts the Object code to Executable code which is the executable file my-file.exe.
Step 5: Finally, the Loader loads the executable file into memory.
In this last step, the Loader loads the executable file into memory and the program starts to run in an executable environment.
Okay, that is it. Let’s summarize and see the takeaways.
Summary
A few important takeaways:
- As a programmer, we can only understand the file content of Source code and expanded code.
- But you cannot directly understand the file content of Assembly code, Object code, and Executable code. They are understandable by computers or some special software since they contain machine-level code.
See you next time! Thank you!
One clap, two claps, three claps ….. Forty !
If you want more such tutorials, subscribe to my youtube channel Smartherd. 100 thousand already did. Don’t wait!