How Does A C++ Program Executes
How Does A C++ Program Executes
Whenever a C++ program file is compiled and executed, the compiler generates some
files with the same name as that of the C++ program file but with different extensions.
So, what are these files and how are they created?
Below image shows the compilation process with the files created at each step of the
compilation process:
Every file that contains a C++ program must be saved with ‘.cpp’ extension. This is
necessary for the compiler to understand that this is a C++ program file. Suppose a
program file is named, first.cpp. The file first.cpp is called the source file which keeps
the code of the program.
Compiler:
A C++ compiler is itself a computer program who's only job is to convert the C+
+ program from our form to a form the computer can read and execute (machine code).
The original C++ program is called the "source code", and the resulting compiled code
produced by the compiler is usually called an "object file".
When we compile the file, the C++ compiler looks for errors. If the C++ compiler reports
no error, then it stores the file as a .obj file of the same name, called the object file. So,
here it will create the first.obj. This .obj file is not executable. The process is continued
by the Linker which finally gives a .exe file which is executable.
Linker: First of all, let us know that library functions are not a part of any C++ program
but of the C++ software. Thus, the compiler doesn’t know the operation of any function,
whether it be cin or cout. The definitions of these functions are stored in their respective
library which the compiler should be able to link. This is what the Linker does. So, when
we write #include<iostream>, it includes iostream library which gives access to
Standard Input and Output. The linker links the object files to the library functions and
the program becomes a .exe file. Here, first.exe will be created which is in an
executable format.
Loader: Whenever we give the command to execute a particular program, the loader
comes into work. The loader will load the .exe file in RAM and inform the CPU with the
starting point of the address where this program is loaded.