Skip to content

Commit cfca64d

Browse files
new post - c++ working
1 parent 95eda22 commit cfca64d

File tree

13 files changed

+663
-35
lines changed

13 files changed

+663
-35
lines changed

content/post/cplusplus-working.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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)

public/index.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@
190190
class="prose prose-neutral dark:prose-invert relative mx-auto min-h-[calc(100vh-9rem)] max-w-(--w) px-8 pt-14 pb-16"
191191
>
192192

193+
<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0"><h2 class="my-0!">C&#43;&#43; Working</h2>
194+
<time class="text-xs antialiased opacity-60"
195+
>May 11, 2025</time
196+
>
197+
<a class="absolute inset-0 text-[0px]" href="http://localhost:1313/post/cplusplus-working/"
198+
>C&#43;&#43; Working</a
199+
>
200+
</section>
201+
193202
<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0"><h2 class="my-0!">Writing a Burp Extension to Bypass Checksum</h2>
194203
<time class="text-xs antialiased opacity-60"
195204
>Dec 20, 2024</time
@@ -269,15 +278,6 @@
269278
<a class="absolute inset-0 text-[0px]" href="http://localhost:1313/post/decoding-dpda/"
270279
>Decoding Digital Personal Data Protection Act For Organizations</a
271280
>
272-
</section>
273-
274-
<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0"><h2 class="my-0!">HackTheBox Business CTF 2023 – Crypto</h2>
275-
<time class="text-xs antialiased opacity-60"
276-
>Jul 26, 2023</time
277-
>
278-
<a class="absolute inset-0 text-[0px]" href="http://localhost:1313/post/htb-businessctf-2023-crypto/"
279-
>HackTheBox Business CTF 2023 – Crypto</a
280-
>
281281
</section><nav class="mt-14 flex"><a class="btn not-prose ml-auto" href="/page/2/"
282282
>Next Page→</a
283283
></nav></main><footer

public/index.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@
66
<description>Recent content on devplayer55221</description>
77
<generator>Hugo -- gohugo.io</generator>
88
<language>en-us</language>
9-
<lastBuildDate>Fri, 20 Dec 2024 00:00:00 +0000</lastBuildDate>
9+
<lastBuildDate>Sun, 11 May 2025 00:00:00 +0000</lastBuildDate>
1010
<atom:link href="http://localhost:1313/index.xml" rel="self" type="application/rss+xml" />
11+
<item>
12+
<title>C&#43;&#43; Working</title>
13+
<link>http://localhost:1313/post/cplusplus-working/</link>
14+
<pubDate>Sun, 11 May 2025 00:00:00 +0000</pubDate>
15+
<guid>http://localhost:1313/post/cplusplus-working/</guid>
16+
<description>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.&#xA;What’s cmake?&#xA;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.&#xA;cmake_minimum_required (VERSION 3.5) project (HelloWorld) set (CMAKE_CXX_FLAGS &amp;#34;${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++14&amp;#34;) set (source_dir &amp;#34;${PROJECT_SOURCE_DIR}/src/&amp;#34;) file(GLOB source_files &amp;#34;${source_dir}/*.</description>
17+
</item>
1118
<item>
1219
<title>Writing a Burp Extension to Bypass Checksum</title>
1320
<link>http://localhost:1313/post/burp-ext-checksum/</link>

public/page/2/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@
190190
class="prose prose-neutral dark:prose-invert relative mx-auto min-h-[calc(100vh-9rem)] max-w-(--w) px-8 pt-14 pb-16"
191191
>
192192

193+
<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0"><h2 class="my-0!">HackTheBox Business CTF 2023 – Crypto</h2>
194+
<time class="text-xs antialiased opacity-60"
195+
>Jul 26, 2023</time
196+
>
197+
<a class="absolute inset-0 text-[0px]" href="http://localhost:1313/post/htb-businessctf-2023-crypto/"
198+
>HackTheBox Business CTF 2023 – Crypto</a
199+
>
200+
</section>
201+
193202
<section class="relative my-10 first-of-type:mt-0 last-of-type:mb-0"><h2 class="my-0!">How To Use NoSQL Injection To Overwrite The Redis Keys</h2>
194203
<time class="text-xs antialiased opacity-60"
195204
>May 13, 2023</time

0 commit comments

Comments
 (0)