How do computers understand C++ programs?

Before going to the structure of the C++ program we need to know how the computer can understand the C++ code and run it to produce the required results.

The computer can understand only binary code which consists of 0s and 1s. Whereas the C++ code written by us is a set of instructions that we give in the form of statements and expressions at a higher level.

The compiler converts the C++ source code written at a higher level to the binary code or machine level language and solves the above problem.

What are the different stages involved in compiling or translating your C++ program to an executable?

  1. Preprocessing: This is the first stage of translation where the preprocessor processes our source code to produce the output that is used by the compiler. The preprocessor is used to include header files, expand macros, conditional compilation.
  2. Compilation: The output of the Preprocessor is taken by the Compiler and converted into an assembly language code. Assembly code is a low-level language that has a strong correspondence with the architecture’s machine code instructions.
  3. Assembly: The Assembler converts the assembly code produced by the compiler to the target machine’s machine-level instructions. Each C++ source code file will be converted into a corresponding object file that contains machine-level instructions.
  4. Linking: This is the final phase where the linker combines several object files by resolving the symbol references with their definitions and also links the external libraries if any to produce the final executable file. The executable file can be run on the machine to get the results.

What if we run into unexpected problems when the executable is running?

The compilers will take care of the errors that occur due to invalid constructs of the language. If we are getting unexpected results or errors during runtime then this can be debugged using the debuggers.

Debuggers

Debugging is the process of finding and resolving bugs at runtime when the compiled file is executed.

A debugger is software used to test and debug other programs which run under controlled conditions and the programmer can track its operations by setting breakpoints or watchpoints.

We will discuss more on compilers and debuggers in the later part of this course.

Categorized in:

Tagged in: