About Me

Hello there! Before talking about qualifications and professional abilities, I would like to introduce myself. I am a Yuvraj Basnet and professional software developer and have been working in this field for 3 long years. I have finished my graduation from The University of Delhi on Computer Science Engineering I considered myself a reasonably experienced programmer with a particular interest in the human side of software development, computers are fascinating machine, but they're mostly reflection of the people using them. In the art of software development, studying code isn't enough; you have to study the people behind the software, too.

C++ Source Code to Executable | Compilation, Linker, Preprocessor, Loader | Build Process

C++ Source Code to Executable | Compilation, Linker, Preprocessor, Loader | Build Process:

Hi this is Yuvraj from welearncode and welcome to another C++ tutorial in this tutorial 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 tutorial is for beginners and I have simplified a lot of things in this tutorial. 

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++ 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. Similary 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 the extension of .cpp and 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 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 slightly difference between the files with .cpp extension and the files with the .h or .hpp extension and here .h is the extension for the header file in C programming language and some people 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 .cpp extension and the .hpp extension. The files with the .hpp extension or the header files will contain the declaration of features and the file with .cpp extension will actually contain the implementation for example we writing a program and we have created a feature to add two numbers and let's call it as add. The header file will contain 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 and the implementation of this feature add will be written with the files with the .cpp extension. Remember that the C++ source code is saved with .cpp extension and .hpp extension.

Now, when we write programs in any programming languages in programs we have to do normal tasks again and again for example in most of the programs you will be in a situation to convert 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 and there are a lot of things that you do again and again while writing the programs. All the programming languages provide the built-in features to do the things which are frequently done in the programs for example as I write before we print some text to the screen, read input from the user, these are the things which are done again and again. C++ also provides the built-in features to do all these things and the C++ provides them in the form of C++ standard library. 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.

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 you are creating a feature to load an mp3 file from the disk and play the mp3 file for this you can write the code for that and you can share that code with the other programmers. The other programmers who want to load an mp3 file from disk and play it they can use your code instead of writing that. So in C++ you can create your own libraries and share that with other programmers and if someone has already created a feature and if they are sharing that then you can use it in your code also you can do that by including the external libraries.

Now, here if you want to use the feature from the libraries may be standard or the external libraries all you have to do is to include the header files which contain the declaration of features and 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 and If your program is large then you should do that and it has a lot of advantages. 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.cpp and the other file is two.cpp.

But 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 programs. So, now we have to transform our source code from C++ to machine code for this we are going to create an executable file from this C++ source code and then the executable file can be executed by the computer. The process of creating an executable file from the source code is called as the build process

So, the first step in the build process in order to create an executable file is pre processing we use a program called as pre processor and this pre processor is a macro processing program. This pre processor takes the C++ source code as the input and it does some processing and it generates an intermediate code. The pre processor does a lot of work and it provides a lot of features 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 and if you have used some comments then it will remove that. I previously write that we can include all these features to our program by including the header files. The pre processor 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 pre processor will actually include those contents of the header files to your program and it will do all those processing and it will generate an intermediate file. Now the output of the pre processor if it is saved then it will be saved with a file will have the extension .ii. Here we assume that we have divided our program in two files one.cpp and two.cpp the preprocessor output will be one.ii which will contain the pre processed code of one.cpp and two.ii which will contain the preprocessed code of the two.cpp.

Now, we have the intermediate code but we can't really execute that and the next thing that we have to do is 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 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. 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 pre processor which is the intermediate code and it will translate that to lower level language. Some C++ compiler, they take the output of the pre processor as the input and they directly produce the machine code but the compiler that the GCC provides, it takes intermediate code as a input and it generates the assembly code. The file containing the assembly code is called as the Assembly file and they will normally have the .s extension. During this compilation process the C++ compiler will take the intermediate code produced by the pre processor 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++ there is a rule that every valid statement should have a semicolon. The compiler will check your program for all those rules and 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 have to correct those errors and have to do this process again. 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 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. 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. For every source file that you have in your program there will be an object file for that.
Now, we have the machine code representation of a source code but this is not an executable file. 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. 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. Here linker will take the object files of program (one.ojb and two.obj) then what it does is, we are going to using 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 generate the executable file. 

There are two types of external libraries static libraries and dynamic libraries and 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. So, the linker will link all those object files and it will generates the executable and this process of linking all object files together and generating the executable file is called as the linking process. 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 so in that case if you are using the external library then you need to provide that library to the linker so that it can link it properly.

Now, we have done the build process, and have an executable file so the next thing 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 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.            
     

 

Post a Comment

0 Comments