How C++ Works | Compilation, Linking, PreProcessing | Build Process Explained |  Video Tutorial

How C++ Works | Compilation, Linking, PreProcessing | Build Process Explained | Video Tutorial


Hi this is Anil from LearningLad and welcome
to another C++ video. In this video we will see all the steps involved
from writing a C++ program to generating an executable file and then executing that generated
executable file. Now before getting started, this video is
for beginners and i have a simplified a lot of things in this video. let’s get started. now what is C++? C++ is a general purpose programming language
that we can use and write the computer programs. now the code written in any programming language
is called as the source code and the code written using the C++ programming language
is called as the C++ source code. you know that we use a text editor we write
the code and then we save that in the Computer memory as a computer file. Now the file containing the source code written
in any programming language is called as the source file and the file containing this C++
source code is called as the C++ source file. Now how we can differentiate a file containing
the C++ source code from any other file. for example if we have a text file then that
text file will have the .txt extension. similarly the MP3 file will have .mp3 Extension
and a PDF file will have .PDF extension. By looking at this file extension we can say
that that particular file is of which type. now the source file containing the C++ code
will be saved with extension of .cpp Now this is the most commonly used file extension
for the C++ source file. apart from this you guys can also find .cc
extension .cxx Extension and then .cp Extension and .C here capital C extension for the c++
source file. But the most commonly used file extension
is the .cpp now along with this file there is another
type of file which is used for writing the source code in the C++ programming language
and they are called as the header files and this header files will have .h or .hpp extension. The files with the .h or .hpp extension will
also contain the C++ source code but there is a slight difference between the files with
the .CPP Extension and the files with the .h or .hpp Extension and we will talk about
that in a moment but here .h is the extension for the header file in C programming language
and some people you know use that same extension for the C++ header files also. But if you really want to tell the difference
between the header files from the C++ programming language and header files from the C programming
language just by looking at the file extension then you should use the .hpp extension because
it will differentiate it from the C header files. now what is the difference between the files
with the .cpp extension and the .hpp extension. The files with the .hpp extension or the header
files will contain the declaration of features and the files with .cpp will actually contain
the implementation. for example let’s say we are writing a program
and we have created a feature to add two numbers and let’s call it as add now the header file will contain you know
what is the name of the feature, how many values we have to give to this feature so
that it can run properly and all these things. it will not contain the implementation. The implementation of this feature add will
be written with the files with the .cpp extension. It has a lot of advantage and the you will
learn about that later in this video series but right now just remember that the C++ source
code is saved with .cpp Extension and .hpp extension. ok now let’s talk about the C++ code. Here we are just talking about the things
in theoretical way, you now we are just drawing things but when we start writing the code
in this video series I will show you all those things practically now when we write programs in any programming
language in programs we have to do the normal task again and again. for example in most of the programs you will
be in a situation to convert the string value from lowercase to uppercase or you want to
print some text to the screen or you want to read the input from the user. There are a lot of things that you do again
and again while writing the programs. All programming languages provide the built-in
features to do the things which are frequently done in the programs. For example as I said before you now we print
some text to the screen, we read the input from the user, you know these are the things
which are done again and again and all the programming languages provide the built-in
features to do all this things. C++ also provides the built in and features
to do all this things and the C++ provides them in the form of C++ standard library. This library is provided by C++ it has all
those built-in features. so let’s say in your program you want to print
some text to the screen, all you have to do is you have to use the built in feature available
in the standard library, use that in your program and provide the text that you want
to print to the screen to that particular feature and it will do that job. In the C++ program you can use the features
provided by the standard library and you can write the code. now this is about the features provided by
the C++ In C++ programming language what you can do
it you can create a feature and you can share it with the other programmers. for example let’s say you are creating a feature
to load and MP3 file from the disk and play the MP3 file now you can write the code for that and you
can share the that code with the other programmers. The Other programmers who want to load and
MP3 file from disc and play it you know they can use your code instead of writing that. so in C++ you can create your own libraries
you can say that with other programmers. so if someone has already created a feature
and if they are sharing that then you can use it in your code also. so you can do that by including the external
libraries. You know will talk more about that in the
upcoming videos but just remember that you know we can use the features provided by other
programmers. you can include the standard library features
and also the features developed by other programmers, if you want in your program. Normally we all the time use the features
from the standard library and the usage of the external library depends on the program
that you are writing. ok Now here if you want to use the feature from
the libraries,maybe standard or the external libraries, all you have to do is you have
to include the header files which contain the declaration of features. once you include those files, those features
will be available in your program and you can write the code. now the next thing is in C++ programming while
writing the code if you want you can divide your program into multiple files. if your program is large then you should do
that. it has a lot of advantages. Now here let’s assume that I am writing a
C++ program and I have divided my program in two different files. one file name is one and the extension is
CPP so you guys can see here one.cpp and the other
file is two.cpp Ok now the next thing is we have written the
program but we want to execute our program. we directly can’t execute the code that
we have written in the C++ programming language because as a programmer we can look at the
code and understand what exactly is going on but the computers can only understand the
code written in the machine language. so what we have to do is we have to translate
this source code written in C++ programming language to machine code, so that the computers
can understand it and they can execute the program. so now we have to transform your source code
from C++ to machine code. now what we going to do is we are going to
create an executable file from this C++ source code and then the executable file can be executed
by the computer. Now the process of creating an executable
file from the source code is called as the build process and now let’s take a look at
the build process. so the first step in the build process in
order to create an executable file is preprocessing. we use a program called as pre processor and
this preprocessor is a macro processing program. This preprocessor takes the C++ source code
as the input and it does some processing and it generates and intermediate code. now the preprocessor does a lot of work and
it provides a lot of features. The preprocessor, it will clean up your code. for example in a source code if you have written
a lot of new lines, spaces then it will remove that. if you have used some comments then it will
remove that which you will learn about that later in the video series and also I said
that we can include all these features are to our program by including the header files,
the preprocessor will actually include the content of those header files. In your source code you provide the name of
the header file that you want to include and the preprocessor will actually include those
contents of the header files to your program. You know it will do all those processing and
it will generate and intermediate file. now the output of the preprocessor if it is
saved then it will be saved with a file which will have the extension .ii Now here we assume that we have divided our
program in two files one.cpp and two.cpp The preprocessors output will be one.ii
which will contain the preprocessed code of the one.cpp and the two.ii which will contain
the preprocessed code of the two.cpp ok now we have the intermediate code but we can’t
really execute that. now the next thing that we have to do is we
have to translate this code to machine code and for that purpose we use a program called
as the compiler. compiler is a computer program that transforms
the code written in the higher level programming language to lower level programming language
and this process of using this compiler to transform the code from higher level programming
language to lower level programming language is called as the compilation process. now here since we are working with the C++
program we will be using the C++ compiler and the C++ compiler will take the output
of the preprocessor which is the intermediate code and it will translate that to lower level
language. now a lot of compilers are available for C++ In this video series we will be using the
compiler provided by the GCC GCC stands for GNU compiler collection They provide compilers for a lot of different
programming languages like C, Ada then objective C, Go language etc. They also provide a compiler for the C++ programming
language. it is open source freely available and we
will be using that in this video series. now some C++ compilers, they take the output
of the preprocessor as the input and they directly produce the machine code but the
compiler that the GCC provides, it takes intermediate code as input and it generates the Assembly
code. Now the file containing the Assembly code
is called as the Assembly file and they will normally have the .s extension. Now during this compilation process the C++
compiler will take the intermediate code produced by the preprocessor and it will generate the
Assembly code and the compiler will also make sure that you have followed all the rules
of the C++ programming language. All the programming languages have their own
rules. for example in C++ programming language there
is a rule that every valid statement should have a semicolon. now the compiler will check your program for
all those rules. if you have not followed those rules then
the compiler will generate the error and you have to go back to your source code and you
have to correct those erroes and then you have to do this process again. If you have followed all the rules of the
C++ programming language then you will get the Assembly code or the file containing the
Assembly code. now here the one.s is the Assembly file containing
the Assembly code for the one.cpp and two.s will contain the Assembly code for this two.cpp Now the next thing that we have to do is we
have to translate this assembly code to machine code and for that purpose we use a program
called as the assembler. The assembler will take the Assembly code
and it will generate the object code or the machine code. The file containing the object code is called
as the object file and this will contain the machine code representation of the source
code. the object files generated by the assembler
will have .obj extension in Windows and .o extension in UNIX platform. Now here for the file one.s the object code
will be in this one.obj and for this two.s the object code will be two.obj now for every source file that you have in
your program there will be an object file for that Ok now we have the machine code representation
of a source code but this is not an executable file Now we know that we can only execute the executable
files now the next thing that we have to do is we
need to link all these subject files together and we have to create an executable file which
we can execute. now we can do that by using a program called
as the linker the linker will take the object files generated,
it will link them together and it will generate the executable file. now here what happens is the link I will take
the object files of this program. in this example one.obj and two.obj then what
it does is, we said that we are going to use the features from the standard library and
the external library. it links the features of all these libraries
which we have used in our program, it links them all together and it will generate the
executable file. now when we talk about the external libraries,
there are two types of external libraries. static libraries and dynamic libraries and
we will learn about them later. if it is a static library then the executable
file will contain that static library. if it is a dynamic library then the executable
file generated by this linker will actually contain the links to those dynamic libraries. Now here while writing the program I said
that we will be including the header files to our program and that’s because the header
files will contain only the declaration of features and the implementation of those features
will be provided in the form of object files. So the linker will link all those object files
and it will generate the executable file and this process of linking all these object files
together and generating the executable file is called as the linking process. Now during the stage if the linker can’t find
a library which you have used in the source code then it will generate the error saying
that it can’t find that library so in that case if you’re using the external
library then you need to provide that Library to the linker so that it can link it properly. so the linker will generate the error if it
can’t find that library and you need to go back to your source code and make sure that
you have used the proper library and then you have to correct that program and you need
to do all the steps again and if the linker can find all those libraries required then
it will generate the executable file. ok now we have done the Builder process we have
an executable file now the next thing that we have to do is we
have to execute this executable file and for that purpose we use a program called as the
loader. The loader will load the executable file into
the main memory which is the RAM so that the CPU can execute that program now the loader it will not only load the executable
file but also it will load the other libraries which are required for this executable file
to run properly and then the program will be executed by the CPU and after the execution
you will get the output. Now here the thing is when we get the output
your output can be either correct output or it can be the wrong output. Now you guys may ask like how we can get the
wrong output. now just for the example let’s say we are
writing a simple C++ program to calculate the area of a circle. now the computer is just a machine and it
doesn’t know the formula to calculate the area of a circle but as a programmer it’s
your job to give the right formula to calculate the area of a circle. if we have given the wrong formula to calculate
the area of a circle while writing the program then the computer doesn’t know that which
is the right formula. it will just produce the output according
to the instruction that you have given and if something like that happens then the output
will not be the proper output. so if your program is not producing the proper
output then you have to go back to your source code you have to make sure that you have done
all those things properly and you have to follow all these process again then you have
to generate the executable file and then you have to run that executable file to get the
proper output. now by looking at this thing you may think
like ok this is a tedious process, you know we have to do all these things again and again
and again. But it is not that difficult. I said that we will be using the C++ compiler
provided by GCC Now all these programs which are required
to create an executable file from the C++ source code they come in a bundle and they
are called as the toolset All you have to do is you have to install
that toolset and by writing just one command you can perform all these steps. if you want you can perform all these steps
individually or you can just type in one command and all the steps will be performed and you
will get the executable file that you can run ok now you don’t have to worry about the complexity
of that and also we will be using something called as the IDE’s which will simplify
the process furthermore. you don’t have to write the commands you just
have to click on a button and everything will happen for you. this is about the steps involved from writing
a C++ program to generating an executable and then execute in that executable file. now here I have simplified a lot of things
and my goal was to give you guys an overview about what exactly happens so that when we
start writing the programs when we talk about the preprocessor or the compiler or the linker,
you guys should know at what stage that program comes in and what is its job. so this is it guys for this video. if you like the video hit the like button. if you don’t like it then hit the dislike
button. if you want to say something then write that
in the comment box. if you want to watch more tutorials like this
then do subscribe to my channel and if this video helps you then write a comment. your comment will inspire me to make more
videos like this. thank you for watching I’ll see you later
in the next video

44 thoughts to “How C++ Works | Compilation, Linking, PreProcessing | Build Process Explained | Video Tutorial”

  1. first thank you for this great effort, next is a request if u can cover new features for c++ 11 and up as well java 8 and up new feature, thank you

  2. Really needed this. Can you do a video on how to compile and link multiple c++ files which are dependent on each other. What will the flow to compile and link all.

  3. Hello Anil I am an old subscriber and I watched all the old C++ videos from the series that were containing 143 videos but now it is increasing.

    Question: Is this a complete new series or you are adding few videos to the existing series?
    Thanks for the awesome lessons, I am always learning something new ๐Ÿ™‚

  4. Amazing explanation my brother recommended me to learn c++ from here. It's really amazing . Your teaching skills are excellent. Sir please make videos on c++ graphic

  5. Although I don't feel comfort while hearing Indian accent ; but this is the best programming tutorial I have ever watched . your crash videos like this one proofs that you are an experienced teacher or IT professor I love you and India and loved your work although I didn't complete it yet but I will InshaAllah . Keep the awesome work that you do and thank you for this tutorial

  6. I want to start programming in mobile, so please suggest c++ compiler for Android.๐Ÿ˜„๐Ÿ˜€๐Ÿ˜€

  7. I just started your series
    Your explanation is awesome
    But it will be more awesome if you'll and some graphics..๐Ÿ‘

  8. Thanks for taking out so much time to prepare videos. I am just trying to start to learn C++ and it it just the third video in the series and yet I am getting a thrust. This video especially explains it the way any beginner like would like to understand. Very nicely prepared video.

Leave a Reply

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