DEV Community

Cover image for C++ Compiler Explained: What is the Compiler and How Do You Use it?
Akshay Sharma
Akshay Sharma

Posted on

C++ Compiler Explained: What is the Compiler and How Do You Use it?

Programmers are always excited to know what happens at the backend when they compile a certain code. After all, all we have learnt is that the system understands only one language and it is all about 1 and 0.

Then what does a C++ compiler do? How does it work?

Well, this question is often asked by a lot of programmers and the internet is always full of jargon information related to it that does not clear everyone’s doubt.

Therefore, we have broken down the process for you into different parts to give you a clear understanding.

Here, we are going to discuss in detail about C++ compiler and its work. So, let’s begin with a complete guide on the C++ compiler.

What Is A C++ Compiler?

If you wish to begin learning C++, it is crucial for you to understand the C++ compiler.

You must know that a system understands only machine language. Though a programmer can write code in machine language, it will only be a tedious and lengthy process.

Therefore, to ease the work of programmers, high-level languages like C, C++, Python, Java and many more are introduced.

But the question still remains! How will the computer understand high-level language?

This is where compilers are used. The compiler is a program that converts the high-level language to machine language and vice versa. So, when you write code in a high-level language, it will be first converted into a machine language.

The computer will then execute the code and then it will again convert the machine-level code to high-level code.

This maintains the communication between the programmer and the system in their concerned languages. But this is not it.
A C compiler or any other compiler converts your code into a platform-independent file which can be then run on multiple platforms.

So, now you are familiar with what C++ compilers are, let’s discuss in detail how the compilers work.

Working Of C++ Compilers

To give you a better understanding of how the C++ compilers work, we have divided the whole process into the following parts:

=> Preprocessing
=> Compilation
=> Assembly
=> Linking

Now let’s discuss all these parts in detail.

Preprocessing

The very first thing that a compiler does is preprocess. This part of the process covers the header part of the program. While writing the code, you usually mention header files at the beginning of your code.

Initially, the C++ compiler will send your code to the preprocessor. A preprocessor is a directive starting from #.
Therefore, whatever you write after #include, # define or #then, the compiler will treat that line of code as a preprocessor.

This processor defines or intimates the compiler to carry out any mathematical or logical operation of the code.
For example, if you are using #define preprocessor, it will intimate the compiler to make a symbolic constant which is also known as a macro.

All in all, at this stage of compilation, the source code is expanded temporarily and it is prepared for further compilation. This file will contain simpler codes than the original source code. However, the size of the preprocessed files increases with the increase in the number of header files.

Moreover, the preprocessor will also add a marker in your code to intimate the compiler of the source of the line as well.
This marking helps to produce the error lines which you see.

Compilation

The next stage of working with the C++ compiler is the compilation stage. This stage is almost similar to the C compilers. At this stage, the compiler will take every output from the preprocessing stage and then it will create an object file.

Initially, it will convert the C++ code into an assembly language code. This is the binary code which is easily readable.

It is sometimes beneficial to read the code in assembly language. The compiler will then optimize the code. It will then compile the code and produce the object file in which the user-generated functions or the main functions are generated as symbols.

Assembly

The next stage of the process is assembly. In this process, the assembler will convert the code present in the assembly language into the bit code. At this stage, the output will be in the binary file format.

However, if you want, you can terminate the compilation process at this point. This is useful if you wish to compile every code individually.

Other than this, you can also put each object file that you will get into static libraries which are also called archives. After that, you can also pull these files out whenever you want to use them. You will not need to recompile all the files again if you wish to make changes to any one file.

Linking

Now the last thing that the C++ compiler will do is to link. You will not be able to get the desired output without linking all the assembly files which were generated in the previous stages. A linker is responsible to create either an executable library file or a dynamic library file.

There are also chances of errors at this stage. The errors may occur because of missing or duplicate information or definitions. Moreover, you need to understand that the missing definitions include multiple things. It is not only restricted to the definition that you have skipped but it is also the information that the linker is not able to find the object file for.

However, duplicate files issue occurs when multiple object files or libraries have the same symbol’s definition.

Conclusion

So, these were all the stages that a C++ compiler goes through. However, the process is more complex than this but for a programmer, a basic understanding is enough.
Now that you are aware of how the compiler works, you can write your codes in a better way and avoid some unwanted and minor bugs. You can implement better headers or remove unnecessary definitions on your own to avoid errors.

Top comments (0)