In this slide we will discuss the following things.
- Creating an C++ file.
- Writing a Program and learning every aspect of its execution like.
- compilation, assembling and linking of library functions.
Creating C++ File
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.
Your First Code
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.
- In First step header files are included in our program.
- In this Step the Compiler compiles the program and generate an intermediate file i.e. assembly coded file.
- In third Step Assembler convert this assembler coded file to an obect code with file extension ".o".
- linker links the object code of libraries functions that we used in our program.
- 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.
- Compiler is just converting our source code to assembly code, after each header file is included.
- 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.
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.
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.
- Wikipedia.
- GeeksForGeeks.
- Lippman, Stanley B. c++ primer 3rd edition(april 2, 1998).
- Stackoverflow.
Helpful... thanks
ReplyDelete