|
| 1 | ++++ |
| 2 | +author = "mukund" |
| 3 | +title = "C++ Working" |
| 4 | +date = "2025-05-11" |
| 5 | +description = "C++ Working" |
| 6 | +tags = [ |
| 7 | + "c++", |
| 8 | +] |
| 9 | ++++ |
| 10 | + |
| 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 | + |
| 13 | +**What’s cmake?** |
| 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`. |
| 16 | + |
| 17 | +```cpp |
| 18 | +cmake_minimum_required (VERSION 3.5) |
| 19 | + |
| 20 | +project (HelloWorld) |
| 21 | + |
| 22 | +set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14") |
| 23 | +set (source_dir "${PROJECT_SOURCE_DIR}/src/") |
| 24 | + |
| 25 | +file(GLOB source_files "${source_dir}/*.cpp") |
| 26 | + |
| 27 | +add_executable (HelloWorld ${source_files}) |
| 28 | +``` |
| 29 | +
|
| 30 | +This ``CMakeLists.txt`` file is a CMake build script that defines how to build a simple C++ project named `HelloWorld`. |
| 31 | +
|
| 32 | +- In the above code, it specifies that minimum version of cmake should be 3.5. |
| 33 | +- The name of the project would be ‘HelloWorld’. |
| 34 | +- Flags are added for the compiler to show warnings and errors. |
| 35 | +- A source directory variable is defined with the path of the subfolder inside the root directory. |
| 36 | +- Files with `.cpp` extension will be stored into the source directory. |
| 37 | +- An executable with the name ‘HelloWorld’ is to be created on build using the .cpp files in source directory. |
| 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). |
| 40 | +
|
| 41 | +```cpp |
| 42 | +cmake -G "CodeLite - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug |
| 43 | +``` |
| 44 | + |
| 45 | +**In a C program** |
| 46 | + |
| 47 | +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. |
| 48 | + |
| 49 | +**Linker** is used to join all the `.obj` files into one executable file (`.exe` in Windows) |
| 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. |
| 52 | + |
| 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 | + |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 66 | + |
| 67 | +**References:** |
| 68 | + |
| 69 | +- [TheCherno - Youtube](https://www.youtube.com/@TheCherno) |
| 70 | + - [How to setup C++ on Linux](https://www.youtube.com/watch?v=LKLuvoY6U0I&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=4&pp=iAQB) |
| 71 | + - [How C++ Works](https://www.youtube.com/watch?v=SfGuIVzE_Os&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=7) |
| 72 | + - [How the C++ Compiler Works](https://www.youtube.com/watch?v=3tIqpEmWMLI&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=6) |
| 73 | + - [How the C++ Linker Works](https://www.youtube.com/watch?v=H4s55GgAg0I&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=7) |
0 commit comments