C++ Compilation – How C++ works

In this article, we will talk about the C++ compilation process and it involves a number of steps. Compilation means we will learn how the C++ code gets converted into an executable. There are four steps involves in the C++ compilation.

  • Preprocessing
  • Compilation
  • Linking
  • The Output of the Linking is an executable file

Preprocessing

Preprocessing is performed by the preprocessor. The preprocessor processes all the statements that begin with the hash (#) or pound sign. If you have included any header files, in your source-code the preprocessor replaces the include statements with the content of the corresponding file. Any macros that has been defined or expanded .

READ MORE What is C++? – Best Way to Learn C++

Compilation

The next step after preprocessing is the compilation. This is performed by the compiler. The compiler checks the code for the correct syntax. If there is a syntax error, the compiler stops the compilation. The software you are using to compile the program will give you an error if the code is incorrect syntax.

If there are no errors, then the code is converted into object code. It also generates a file that has “.OBJ” extension. On some platforms, the extension is omitted.

Linker

The next step in C++ compilation is performed by the linker and it is called as linking. The object code is linked with the standard libraries. If there are no errors during linking, then an executable is generated. We can execute this generated file from the command line or from the explorer.

The Output of the Linking is an executable file

When we finish the above three steps then your code is ready to execute. You can execute this code in windows or any other operating system that has a C++ compiler installed. The execution of the C++ code is very simple to follow the below practice example to execute a program on your own computer.

Particle Example

#include <iostream>
int main(){
	// This is a single line comment
	/* This is a multi-line 
        comment */
	std::cout << "Hello Universe" << std::endl;
	return 0;
} 

To know the execution process you have to compile this code on your computer.

  • Open a Command prompt and type the ‘cd’ command to navigate the directory where you have saved this program.
  • If you are using UNIX, then you can use the following command:
> g++ -o HelloUniverse HelloUniverse.cpp
> ./HelloUniverse
  • If you are using windows then to execute it you should have a C++ compiler installed on your computer.  You can install visual studio and type the following command:
> cl /EHsc HelloUniverse.cpp
> HelloUniverse.exe

The code will execute and print the “Hello World!” message to the terminal. You can also understand it the by figure below.

C++ Compilation Model

If you like this article about C++ Compilation then share it with your friends on Facebook, Twitter, etc.

Muzammal Ahmad

I am an Ethical Hacker, Programmer, and SEO (Search Engine Optimization) expert. I love to write blogs and share knowledge with people. I also like to help people to solve their technical or internet relates problems.

Leave a Reply

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