You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/post/cplusplus-working.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,9 @@ tags = [
10
10
11
11
I was just learning about the C++ fundamentals from **The Cherno** C++ playlist and thought of making notes on it. Added reference at the bottom.
12
12
13
-
**What’s cmake?**
13
+
### What’s cmake?
14
14
15
-
CMake is an open-source, cross-platform build system generator. It doesn’t build software directly—it generates build files (like Makefiles, Ninja build files, or Visual Studio project files) based on the instructions you give it in `CMakeLists.txt`.
15
+
CMake is an open-source, cross-platform build system generator. It doesn’t build software directly—it generates build files (like Makefiles, Ninja build files, or Visual Studio project files) based on the instructions present in <mark>CMakeLists.txt</mark>.
This ``CMakeLists.txt`` file is a CMake build script that defines how to build a simple C++ project named `HelloWorld`.
30
+
This `CMakeLists.txt` file is a CMake build script that defines how to build a simple C++ project named `HelloWorld`.
31
31
32
32
- In the above code, it specifies that minimum version of cmake should be 3.5.
33
33
- The name of the project would be ‘HelloWorld’.
@@ -36,38 +36,38 @@ This ``CMakeLists.txt`` file is a CMake build script that defines how to build a
36
36
- Files with `.cpp` extension will be stored into the source directory.
37
37
- An executable with the name ‘HelloWorld’ is to be created on build using the .cpp files in source directory.
38
38
39
-
A [`build.sh`](http://build.sh) file is created that contains the cmake command to configure the build environment. It would generate Makefiles to use with the IDE (here CodeLite).
39
+
A <mark>build.sh</mark> file is created that contains the cmake command to configure the build environment. It would generate Makefiles to use with the IDE (here CodeLite).
There is a preprocessor statement `#include <iostream>`. Any line starting with a `#` would be a preprocessor statement. The `include` preprocessor will copy all the contents of the mentioned file (here iostream) directly into our program file. The preprocessor statement is executed before the compilation starts. Only `.cpp` files are compiled. The `.cpp` files are compiled into `.obj` files.
47
+
There is a <mark>preprocessor</mark> statement `#include <iostream>`. Any line starting with a `#` would be a preprocessor statement. The <mark>include</mark> preprocessor will copy all the contents of the mentioned file (here iostream) directly into our program file. The preprocessor statement is executed before the compilation starts. Only `.cpp` files are compiled. The `.cpp` files are compiled into `.obj` files.
48
48
49
-
**Linker** is used to join all the `.obj` files into one executable file (`.exe` in Windows)
49
+
<mark>**Linker**</mark> is used to join all the `.obj` files into one executable file (`.exe` in Windows)
50
50
51
-
We can just declare a function in our program, and call it in another function. The function declaration could be empty, it doesn’t need to be declared to compile successfully. The **compiler** doesn’t check if the function has been defined. Only during the runtime(build) would we get the error indicating that the function has no definition.
51
+
We can just declare a function in our program, and call it in another function. The function declaration could be empty, it doesn’t need to be declared to compile successfully. The <mark>**compiler**</mark> doesn’t check if the function has been defined. Only during the runtime(build) would we get the error indicating that the function has no definition.
52
52
53
53
The `.obj` file is binary and not readable. It contains the machine code that CPU runs. We can create a `.asm` file from the `.cpp` file which contains assembly instructions.
54
54
55
-
After compilation, a process called Linking takes place. Linking finds all the symbols, functions, etc. in our project and link them together. Compilation happens on individual files separately.
55
+
After compilation, a process called <mark>linking</mark> takes place. Linking finds all the symbols, functions, etc. in our project and link them together. Compilation happens on individual files separately.
56
56
57
-
Let’s say that we call a function A, which in turn calls a function B and function B is declared but not defined, we will get a linking error and not a compile error. If we do not call the function A, but it still exists and call the function B, we will still get the linking error. If we add static to the declaration of function A, and do not call it, we will not get the linking error.
57
+
Let’s say that we call a function A, which in turn calls a function B and function B is declared but not defined, we will get a linking error and not a compile error. If we do not call the function A, but it still exists and call the function B, we will still get the linking error. If we add <mark>static</mark> to the declaration of function A, and do not call it, we will not get the linking error.
58
58
59
-
If we add **static** in any function declaration, then it means that the function is only declared for that particular file, and the linker will understand that.
59
+
> If we add `static` in any function declaration, then it means that the function is only declared for that particular file.
60
60
61
-
So, the linker understands that the function A will only be used in the file where it is defined. It will see that function A is not being called, so it doesn’t matter whether the function B(which is being called inside function A) is defined or not. If the function is not declared as static, then there is a possibility that function A might get called in some other file, and for that the function B also needs to be defined.
61
+
So, the linker understands that the function A will only be used in the file where it is defined. It will see that function A is not being called, so it doesn’t matter whether the function B(which is being called inside function A) is defined or not. If the function is not declared as static, then there is a possibility that function A might get called in some other file, and for that the function B also needs to be defined.
62
62
63
-
If we define a function inside a header file, and then include the same header file in two different `.cpp` files, we will get a linking error. It is because, including the header file means copying the contents of the header file to those two files, and meaning declaration of the function in two files.
63
+
If we define a function inside a header file, and then include the same header file in two different `.cpp` files, we will get a linking error. It is because, including the header file means copying the contents of the header file, and here it means declaring the function in two files.
64
64
65
-
We can use `static` keyword while declaring the function in the header file. It will mean that the linking to the static function will be internal to the file calling it and separate files will have there own separate versions of the function.
65
+
> We can use `static` keyword while declaring the function in the header file. It will mean that the linking to the static function will be internal to the file calling it and separate files will have there own separate versions of that function declared as static.
0 commit comments