Getting Started with C++

Compilation

A lot of us are aware of how to write codes and execute them. But is everyone aware of the underlying process which converts a source code to an executable program? The answer is not many. Basically, you have a program that we call source code. It must undergo the process of compilation for generating an output with the assistance of an executable program. Therefore, is there is a compilation process, it will also have a tool for performing this work.

compilation

Compiler

A compiler refers to a special program that processes statements written in a particular programming language. After that, it turns them into machine language or “code” that a computer’s processor uses.

Usually, a programmer will write language statements in a language, like Pascal or C, one line at a time by making the use of an editor. The file that creates comprises what we call the source statements.

The programmer then runs the suitable language compiler, which specifies the name of the file that consists of the source statements. When executing, the compiler will first analyze all of the language statements syntactically one after the other.

After that, in one or more successive stages or passes, creates the output code, which ensures that statements referring to other statements would refer to appropriately in the final code. Usually, the output of the compilation refers to as object code or sometimes an object module.

Further, the machine code which the processor has the ability for executing one instruction at a time is the object code. Customarily in some operating systems, an additional step was needed after compilation. We refer to it as linkage editing and we refer to the output as a load module.

Browse more Topics Under Getting Started with C++

How C++ Compiles Code?

The compilation of a C++ program includes three steps:

  1. Preprocessing: The preprocessor will take a C++ source code file and then deal with the #include’s, #define’s and other preprocessor directives. Further, the production of this step is a “pure” C++ file without pre-processor directives.
  2. Compilation: The compiler will take the preprocessor’s output and create an object file from it.
  3. Linking: The linker will take the object files which the compiler produces and creates either a library or an executable file.

Preprocessing

The preprocessor handles the preprocessor directives, like #include and #define. Moreover, it is agnostic of the syntax of C++. This is why it must be used with care.

It works on one C++ source file at a time when it replaces #include directives with the content of the respective files (which is generally merely declarations), replacing macros (#define), and then selects different portions of text depending of #if, #ifdef and #ifndef directives.

Moreover, the preprocessor works on a stream of preprocessing tokens. Macro substitution basically refers to placing tokens with other tokens (the operator ## allows merging two tokens when it makes sense).

Finally, the preprocessor generates a single output that is a stream of tokens that result from the transformations which are given above. Similarly, it also adds some special markers that tell the compiler where each line came from.

This allows it to make use of those for generating sensible error messages. Further, some errors can create at this stage with clever use of the #if and #error directives.

Compilation

The compilation step takes place on each output of the preprocessor. The compiler will parse the pure C++ source code (now without any preprocessor directives) and then convert it into assembly code.

After that, invokes the underlying back-end (assembler in toolchain) that assembles that code into machine code creating an actual binary file in some format (ELF, COFF, a.out, etc. ). Moreover, this object file comprises the compiled code (in binary form) of the symbols defined in the input. Also, we refer to symbols in object files by name.

In addition, object files can refer to symbols that do not define. This is what happens when you use a declaration and do not give a definition for it. Thus, the compiler does not mind this, and will happily produce the object file as long as the source code is well-formed.

Please remember that compilers generally let you stop compilation at this point. It is advantageous as with it one can compile each source code file separately. The benefit this offers is that one does not need to recompile everything if one only changes a single file.

The created object files can be placed in special archives which we refer to as static libraries, for easier reusing in the future. In addition, it is also at this stage that the regular compiler errors similar to syntax errors or failed overload resolution errors reports.

Linking

The linker is what generates the final compilation output from the object files that the compiler generates. Also, this output can be either a shared (or dynamic) library or an executable.

It connects all the object files when it replaces the references to undefined symbols with the correct addresses. Moreover, each of these symbols may be defined in other object files or in libraries. If they define in libraries other than the standard library, the linker must be informed about them.

At this point, the most common errors are missing definitions or duplicate definitions. Missing definitions mean that either the definitions do not exist (meaning to say, they are not written), or that the object files or libraries where they reside, the linker never got them. Alternatively, duplicate definitions mean the same symbol was defined in two different object files or libraries.

FAQ on Compilation

Question 1: What is the difference between compilation and execution?

Answer 1: Compile time address binding occurs before loading the program into memory. On the other hand, execution time address binding happens at the time of program execution.

Question 2: Is C++ a compiler?

Answer 2: C++ has been designed to be a compiled language. In other words, it is usually translated into machine language that can be understood directly by the system. Thereby, it makes the generated program highly efficient. In order to do that, a set of tools are necessary. They are called the development toolchain, whose core is a compiler and its linker.

Question 3: What is the use of C++?

Answer 3: C++ is a powerful general-purpose programming language. We can use it for developing operating systems, browsers, games, and so on. Moreover, C++ supports different ways of programming which are procedural, object-oriented, functional, and so on. Thus, C++ is quite powerful and flexible.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.