After a while, I think it’s time to re-cap C++. So in this post, I will talk about,

  • How a simple C++ source file works
  • The starting point of the C++ program
  • Libraries
  • The compiler and how it works

As I mentioned above, let’s write a simple C++ code first. Then let’s discuss how it works, how to compiler work.

So before we go into deep things about C++, let’s start our discussion with how a simple C++ source file works. 

How a simple C++ source file works

When we write any C++ program and then compile and run it. So we write the C++ code into human-readable text files, which we call source files. Then we use a compiler to convert the C++ into an executable machine code, which is a program that computers can run.

So let’s see how this happens step by step.

First, let’s create our first simple C++ source file. For that, just open any text editor which you like to work with. It may be Gedit in Linux, Notepad in Windows, or TextEdit on Mac, it doesn’t matter. Then write the following code and save it as main.cpp.

#include <cstdio>

int main(){
        printf(“Hello World!”);
        return 0;
}

FYI: This C++ program will print Hello World! To the screen. Also, C++ source files have a .cpp file extension by conventions.

The starting point of the C++ program

An entry point or the starting point is a function that executes when a user runs a program. As shown in the above sample code, the C++ programs have a single entry point called the main function. functions are code blocks that can get inputs, execute instructions, and return results.

In our sample code, the main function calls the function printf. The printf function prints the characters “Hello World!” to the console. After that, the program will exit by returning the exit code 0 to the operating system. exit codes are integer values that the operating system uses to determine how the program ran. Generally, exit code 0 means, the program ran successfully. If there are any issues or incorrect things that happened during the program, normally it returns a specific error code other than 0. For the main function, having a return statement is optional since the exit code is defaulting to 0.

Libraries

Libraries are helpful code collections for developers, that can easily import to their programs to prevent having to reinvent the wheel. Every programming language has library functionality. For example, we can load libraries in different languages as shown below.

  • Java, Python and GO -> import.
  • JavaScript, Lua, Perl and R -> require/ requires.
  • PHP, C# and Rust -> use/ using.
  • C and C++ -> #include

In our sample program, we have used, #include <cstdio>. cstdio is a library that performs input/ output operations, such as printing on the console.

The compiler and how it works

Turning the source code into an executable program is the next step after writing the source code. The compiler tool chain ( or tool chain) is a collection of three main elements that run after one another to convert source code into an executable program. Those three main elements are,

  1. The Preprocessor

  2. The Compiler

  3. The Linker

Let’s discuss one by one.

The Preprocessor performs basic source code manipulation. Let’s consider our sample source code for a moment. In that, #include <cstdio> is a directive that instructs the preprocessor to include information about cstdio library directly into our program’s source code. After the preprocessor finished processing a source file, it produces a single translation unit. Then each translation unit is passed to the compiler for further processing.

The compiler reads the translation unit which is generated by the preprocessor and generates an object file. Object files contain object code, which is an intermediate format. That intermediate format is not human readable, or understandable. The compilers work on one translation unit at a time, so each translation unit corresponds to a single object file. Simply we can think of the compiler as a kind of a language translator.

The Linker generates the programs from the object files. Also, the Linkers are responsible for finding the libraries which we included within the source code. For example, consider the cstdio library which we included in our sample code. The Linker will find the cstdio library and include everything which our program needs to sue the printf function.

So I guess now you have some idea about how the C++ compiler works. Now what we have to do next is setting up our Development Environment & start coding. Let’s discuss Setting up the development environment in another post…

Until then, feel free to comment below with your ideas/ suggestions/ corrections. Let’s discuss. And also don’t forget to subscribe for this kind of tech post.


***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

Leave a Reply

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