Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C++ Compilation Process

Last Updated on March 23, 2023 by Prepbytes

The compiler performs various stages of analysis to ensure the code is syntactically and semantically correct, and can also optimize the resulting code in the compilation process. In this article, let us discuss the compilation process and its stages in detail.

What is Compilation Process

The compilation process in C++ involves translating human-readable source code into machine-readable binary code that can be executed by a computer. The C++ compilation process has two steps: first, the source code is converted into object code, an intermediate representation, and then the object code is connected with other files and libraries to produce an executable file.

There are four steps in the C++ compilation process that converts source code into machine-readable code:

  • Preprocessing
  • Compilation
  • Assembling
  • Linking

The Preprocessor

By using the preprocessor, it can include additional files in the project without worrying about the C++ translation of the syntax. It operates on a single source file at a time, replacing #include directives with their contents replacing macro define statements with ifdef/endif clauses, and making choices based on the "#if" command. After all of this, the preprocessor generates a single output, a stream of tokens resulting from the alterations mentioned. Also, it contains a few special markers that identify each line’s production location so the compiler can provide helpful error messages. Using #if and #error directives wisely can cause some issues to appear.


The output of the preprocessor is compiled during the assembly phase. The compiler turns assembly language into pure C++ source code, which it then parses and builds. The underlying back-end is then called, compiling the code into machine language and creating a genuine binary file in some format. The syntax for the symbols defined in the input is contained in this file. In object files, symbols are referred to by name. Object files can make references to symbols that aren’t specified. This is the case when you use a declaration without giving it a definition. Given that we are defining an array inside another, this is an excellent illustration of when the cast was required. As long as the source code is valid, the Cpp compiler doesn’t care about this and will gladly create the object file. At this point, most compilers let you terminate the compilation process. This is highly useful because it enables you to separately compile each source code file. The advantage of this is that if only one file is modified, you don’t have to recompile everything.


Source code is transformed into object code by the assembler. On a UNIX system, you can notice files with the .o extension, which stand for object code files (.OBJ on MSDOS). The object files are processed by the assembler, which transforms their assembly code into machine language instructions. A relocatable object code is a file that is created. As a result, the compilation procedure creates an object code that is relocatable and may be utilized in other locations without the need for additional compilation.


The code known as the linker converts the object files produced by the compiler into a finished compilation output. This finished product could either be an executable or a shared library, which shares names with static libraries. By substituting the references to the missing symbols with their proper addresses, it resolves symbols that haven’t been declared in any file. Other object files or libraries may declare these symbols. You must inform the compiler about them if they are defined in modules other than the primary ones. At this phase, missing definitions or repeated definitions are the most frequent mistakes. The first suggests that the words are not present (such that, there were not written), but the second suggests that the same symbols were defined twice in two different object files and libraries.

In conclusion, the C++ compilation process involves several steps, including preprocessing, compilation, assembly, and linking, which ultimately produce an executable file that can be run on the target system.

Frequently Asked Questions(FAQs):

1. Does C++ use the compilation process?
It is necessary to compile every C++ source file into an object file. After several source files are combined into one object file, it is linked into either a compiled code, a standard library, or a static library (the last of these being just an archive of object files). Most C++ source files have the .cpp

2. Why do we use C++ to compile?
Since the computer framework is made up of control signals and wires which may only work with boolean 1s and 0s, a compiler is required to translate your program from high-level code to computer language that the CPU can understand.

3. What are the C++ compilation stages?
The four processes involved in compiling C++ source files into machine-readable code are as follows:

  • Preprocessing
  • Compiling
  • Assembling
  • Linking

4. Is C++ a compiled language?
An executable language is used for programming that has been converted into machine code so that it can be executed by a processor. Compiled code languages are usually compiled instead of interpreted

5. Can we compile C++ without the main?
Yes, We can start a program with no main() function. We have seen many times that main() is the starting point for a program’s execution. This is true only from the point of view of developers.

6. What happens when a CPP code is compiled?
The processor first transforms the C++ code, which has been removed of preprocessor commands, into machine-level language. The compiler optimizes the code during this syntax step by highlighting syntax errors, overload resolution errors, and any other compile-time errors.

Leave a Reply

Your email address will not be published. Required fields are marked *