0
0
C++programming~10 mins

Compilation and execution process in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compilation and execution process
Write C++ source code
Preprocessing: handle #include, macros
Compilation: convert code to assembly
Assembly: convert assembly to machine code
Linking: combine object files and libraries
Executable file created
Execution: OS loads and runs program
This flow shows how C++ source code is transformed step-by-step into a running program.
Execution Sample
C++
#include <iostream>
int main() {
  std::cout << "Hello!" << std::endl;
  return 0;
}
A simple C++ program that prints 'Hello!' to the screen.
Execution Table
StepProcessActionOutput/Result
1Write source codeCreate .cpp file with codeSource code file created
2PreprocessingExpand #include and macrosExpanded source code
3CompilationConvert expanded code to assemblyAssembly code generated
4AssemblyConvert assembly to machine codeObject file (.o) created
5LinkingCombine object files and librariesExecutable file created
6ExecutionOS loads executable and runs itProgram runs and prints 'Hello!'
7ExitProgram ends with return 0Process terminates normally
💡 Program finishes execution and returns control to OS
Variable Tracker
VariableStartAfter PreprocessingAfter CompilationAfter AssemblyAfter LinkingDuring ExecutionFinal
source_codeOriginal code textExpanded code textAssembly codeMachine codeExecutable fileLoaded in memoryProgram ends
outputNoneNoneNoneNoneNone"Hello!" printed"Hello!" printed
Key Moments - 3 Insights
Why do we need preprocessing before compilation?
Preprocessing handles #include and macros so the compiler sees full code without shortcuts, as shown in step 2 of the execution_table.
What is the difference between compilation and assembly?
Compilation translates code to assembly language (step 3), while assembly converts that assembly to machine code (step 4), preparing it for linking.
Why is linking necessary after assembly?
Linking combines all object files and libraries into one executable (step 5), so the program can run as a whole.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after the linking step?
AAssembly code generated
BExecutable file created
CSource code expanded
DProgram prints 'Hello!'
💡 Hint
Check row 5 in execution_table under Output/Result column
At which step does the program actually run and print output?
AExecution
BLinking
CCompilation
DPreprocessing
💡 Hint
Look at step 6 in execution_table where the program runs
If the source code has a syntax error, at which step will the process stop?
AAssembly
BPreprocessing
CCompilation
DLinking
💡 Hint
Syntax errors are caught during compilation, see step 3 in execution_table
Concept Snapshot
C++ compilation process:
1. Preprocessing: expands includes/macros
2. Compilation: source to assembly
3. Assembly: assembly to machine code
4. Linking: combine files to executable
5. Execution: OS runs program
Errors usually stop compilation.
Full Transcript
This visual shows how a C++ program goes from source code to running program. First, you write code. Then preprocessing expands includes and macros. Compilation turns code into assembly language. Assembly converts that to machine code in object files. Linking combines these into an executable file. Finally, the OS loads and runs the program, which prints output and ends. Errors like syntax mistakes stop the process during compilation. This step-by-step helps beginners see how code becomes a running program.