Skip to main content

Creating and executing c++ program.

In this slide we will discuss the following things.

  1. Creating an C++ file.
  2. Writing a Program and learning every aspect of its execution like.
    • compilation, assembling and linking of library functions.

Creating C++ File

You should skip to "Your First Code", if you know how to do this.😉

C++ program is nothing but a file with extension ".cpp". Creating a C++ file is not difficult. You can create it in anyway you like, I gave a simple way for creating.




(Note - If you have not installed c++ compiler in your computer. Then, to run the c++ files, you must install the compiler first or you can use some online c++ compiler like onlinegdb)

Your First Code

Now, start typing the following code in your ".cpp" that you created.
(This is also called as source code and you can consider it as the basic template for your c++ program, but I will progressively introduce more things that you can add to your c++ program).


1
2
3
4
5
6
#include<iostream>
int main()
{
	std::cout << "Hello world" << std::endl;
	return 0;
}

Now, let's break down the written code line by line.

Line 1.

1
#include<iostream>

 Here, #include is a preprocessor directive. (What that means?😕)

(There are other preprocessor directives too, like #define, #ifdef, #ifndef etc.)

Let's see.

From the name Preprocessor itself, we shall get some idea that it is related to preprocessing something i.e. it is processing something before something else.

Now, let's see what is "something" and what is "something else" here?

Actually, #include is including the header file with file name "iostream" (in some compiler it is iostream.h)

before the compilation process.

i.e. "Something" = iostream header file was included and "Something else" = compilation.

Now, as I said "Something else" is compilation, so let's first discuss it.

In Below diagram, all the steps are shown which occurs during compilation.


Briefly, this is what happening here.

  1. In First step header files are included in our program.
  2. In this Step the Compiler compiles the program and generate an intermediate file i.e. assembly coded file.
  3. In third Step Assembler convert this assembler coded file to an obect code with file extension ".o".
  4. linker links the object code of libraries functions that we used in our program.
After all of these steps we get our executable file which we can use any number of times as we want.
(Wait for execution part).

Now, let's understand these things in Layman language.
In the first step we have included header file name "iostream", but why?😕
  • Answer is, Because, we need to use the function "std::cout" in our program and this function is predefined(just include and use) inside the iostream header file.
Ok, now we know why iostream is included.

Now let's find out what compiler is doing?
  • Compiler is just converting our source code to assembly code, after each header file is included.
Ok, Now we are left with assembler and linker.
  • Assembler is converting our assembled code into machine code and this is the actual compiled file we get. But, there is still an issue to which compiler doesn't give a sh*t about, Let's, see what's that. When the compiled file is generated the compiler doesn't look for the things(I mean functions and classes etc) that we used in our program or. the functions(for example) that we used in our program is really present their or not? Linker resolves that issue.
  • Linker search for each function that we used in our program and now let's imagine that we are using some function named fun() in our program. What linker will do is, it will search the function that we used in our program, the searching will done like this,
    • first it will search in the file ".cpp" that we created by ourselves and if it isn't present there. then it will search in the header files that we have included in, before the compilation time. and if it founds the function in some header file it will link it's object code to our file and after doing it for the whole program, we will get our executable ".exe" file. 
    • But if Linker is unable to find the function it will just generate an error that, function is not defined.
As of now, we have cleared our mind about what are all the compilation steps,
Now, let's look at line 2 to line 6.

2
3
4
5
6
int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}

  • main() is function here, it is the most important function in a c++ program because the execution flow of the program starts from the main() function(what is int here? We will discuss about functions in details later.)
  • std::cout is also a function that is predefined in the iostream header file and is used to output some data, std::endl is also a function and used for flushing the output buffer and outputing a new line.
Extra thing to remember - in C language we have printf() function to print, which is included in the header file stdio.h.

Now, We have basic ideas about what happens during compilation process. 
Now, to run your ".cpp" file you can use your OS terminal or if you are using some IDE(integrated development environment) then you can directly run it through the IDE itself.

To compile and run your program through terminal. You should write following command.

1
g++ -o <the name with you want to save .exe file> <name of your file>.cpp

(remember you have to be in the same directory where you saved your ".cpp" file)

Now, to run your compiled c++ file i.e. ".exe" file through your terminal.

1
2
3
.\Ourfirstprogram.exe 
          or
  Ourfirstprogram

You can run your .exe file in windows terminal through these commands.

And if you are a linux user simply write it like this

1
./Ourfirstprogram

 For more info regarding the g++/gcc commands visit.

As I said previously that, we can use the executable file any number of times,

The reason is that, unlike interpreted languages like python where the code is interpreted line by line each time, we run it, c++ on other hand is a compiled language, so it generate a separate executable file which can be used as a program(remember how program runs? from previous slides) and does not need recompilation every time, but, if we add something to our source code file, we have to recompile it to generate a new executable file, to take the effect of new source code lines.

Stay tuned for the upcoming content. For any query leave a comment below.

Click the Subscribe button at the top, to follow my every post regarding c++.

References -
  • Wikipedia.
  • GeeksForGeeks.
  • Lippman, Stanley B. c++ primer 3rd edition(april 2, 1998).
  • Stackoverflow.

Comments

Post a Comment

Popular posts from this blog

Introduction to Computer Science.

 Before directly jumping deeply in c++, let's first start by creating the roots of computer science. In this post we will answer the following question. What is Computer and How it works? What is Program and Operating system? Low Level vs High Level Language? Compiler v/s Interpreter? By Knowing All these basic concepts You will be able to understand the upcoming more complex concepts easily. Let's start answering the above questions. What is Computer and How it works? What is computer? If I answer briefly,  what is computer?  then it is just a calculator used for doing simple calculations. If I have to answer where is computer used?, then I could probably say everywhere. We are surrounded by computer, for example mobile, smartwatches, and personal computer (obviously).  The below image is the first mechanical computer that we used for calculation.                                        Abacus How computer works? The computer's internal functionality is a whole separate bra

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a

Algorithms, Pseduocodes and FlowCharts.

Some Basic Knowledge. C++ is a general purpose programming language, created by Bjarne Stroustrup and was firstly released in  1985. It is an extension to C language, Reason for that is if you are familiar with C language than you must be familiar with the fact that there are no classes, templates etc. present in C language. Due to which when classes are added to C language it is named as "C with Classes", but C++ is considered as default name now.  Extra thing to remember - In "C++" the symbol "++" after C is post increment symbol and therefore, "C++" means that In C additional features are added. Now, let's define, what are algorithms? Algorithms are nothing but set of rules that must be followed to accomplish some task. (Now, What that mean?😕) let's take an example of swapping two numbers, i.e. if a = 10 and b = 20 after swapping a = 20 and b = 10. How will you write the procedure for this?  (There are many such algorithms). Accordin