How the C++ Compiler Works

preview_player
Показать описание


Рекомендации по теме
Комментарии
Автор

"But we're not talking about Linking. The video about that is in the description"
>Clicks
"So the first stage is actually compiling, I made an entire video about that.. Link is in the video description"
WARNING: Infinite Loop Detected

sonictimm
Автор

I've been programming for 20 years, and I actually learned something new here. Thanks!

kevnar
Автор

I'm super late to the party but screw it, this was hands down the best C++ video I have ever seen, full stop. It might be the best tutorial I've ever seen. Not only are you engaging and interesting to listen to, but you make the concepts really easy to understand without oversimplifying or dropping necessary information.

This should be mandatory viewing for anyone learning C++. How none of this was ever covered in my entire 4 year Computer Engineering program is beyond me.

jeffreyhymas
Автор

Trying to master C++ for five years. You are the first person that makes sense!

guycpsi
Автор

summary:
Compilation is the process where we convert our plain text of the cpp files into a machine code that is readable by the CPU.
It occurs in several steps. The first step is 'preprocessing', which evaluates the proprocess statements. This is followed up py tokenizing, parsing and so on, which will create an abstract data tree(?).


In cpp, files have no meaning, and this is to be compared with Javascript. The cpp compiler by default treats .h, .cpp files as the way they are, but we can specifically direct the compiler to treat any random file like any other random file. Therefore, we can just make a .cherno file, and make the compiler treat it like a cpp file.
//To be honest, I didn't really get what he meant here.


Going over the preprocessing statements, the most common statements are #include, #define, #prgma(?) and so on.
#include basically results in the computer to find the file with that name and copy and paste its content into the current file.
#define is followed by 2 arguments, and will replace all of the first argument found in the text into the second argument. To see what is going on during the preprocessing stage, we can change the property of our exe file so that it preprocesses to a file, and open the .i file with an text editor.


After the preprocessing stage and a series of subsequent stages(cherno didn't cover obout these stages in these video), the compiler results in an obj file. Opening this in an text editor will show hexadecimal looking numbers, which we cannot understand. So we can manipulate the property of the project, click c/c++ --> output files --> assembly output, and change the option from no listing to assembly only listing. Compiling the file after this settings will result in a .asm file being created ind the debug folder, together with the other obj files. This is the readable format of the machine code, and Cherno uses this file to demonstrate how optimization option runs our code faster by reducing redundancy.


So in our properties, we can enable optimation, which also requires to change the code generation-> basic runtime check should also be set to default. Cherno makes a 'log' function that returns the message that it receives, and *for no reason*, puts it in the middle of the 'multiply' function he created. Without the optimization, we can see in the .asm file that the compiler calls for the 'log'function for no reason, which slows down the computer by adding additional line to the code. With the optimization turned on and compiling the file again, the whole line that calls for the redundant 'log' line is just gone, because the compiler decides to delete the whole section since it understands that it is unnecessary.

팍준하
Автор

Best C++ series ever. I'm actuallty a very experienced C programmer, and have started diving into C++ recently. These videos are just really good. Thanks for the good work!

shunshirou
Автор

I'm on my 3rd year of a computer science degree and I feel like I learned more from your videos than I did in my first 2 years. Absolutely brilliant content, pacing and delivery. I started with Java and touched on other high level languages like C#. I was really weary about moving to C++ as most videos go from the absolute basics of coding so it's difficult to find what you need. This series on the other hand delivered EXACTLY what I was after. So glad I found you. Thank you!

dayday
Автор

All my .cpp files are gonna be .cherno files now

sweatygenius
Автор

The background music is perfect. It keeps the viewer focused. Great job dude

rohitsanjay
Автор

Okay man seriously your C++ tutorials are the best programming tutorials in any language I've ever coded in. It feels so good, even if it's only scratching the surface, getting a good understanding of the underlying nuts and bolts of every line of code. Thank you very much!

susmitislam
Автор

Dude, this series is amazing, seriously! I come with a bit of background of assembly, c++, c# and python and you explain things so well!
Thank you very much for these videos :)

rapteakewl
Автор

Been programming c++ for just under a year now, consistently. It's amazing how I still learn new things everyday, especially from you. Thanks so much, man. I appreciate it so much.

ExoContinuum
Автор

Playing around like this is the absolute best way to learn the internals of programming. Not merely watching videos, but actually DOING the work. Now go play!!! 😁

Chiramisudo
Автор

Reading the comments, I am so glad you are one of the first channels I found when I started looking for cpp tutorials...

paulsalinas
Автор

"so instead of fixing this like a normal person" cracked me up for some reason

esben
Автор

I'm only a few videos in and it's such a breath of fresh air to find someone who knows their topic well enough to explain it in such easy terms. Preprocessing itself has been so badly explained to me by teachers at all levels and udemy 'instructors' and then here you go and show me that it's perhaps one of the simplest things you can imagine.

Thank you. Thank you for the effort you've made in creating this.

nerdy-wizard
Автор

Love the series. Please keep it up. There is a lot of unfinished cpp series on youtube. It would be a shame if this one will fall aswell.

SoulBruteflow
Автор

I found your channel and I am thrilled. I wish I could freeze time for anything but me, my computer, my internet connection and YouTube servers in order to just get all this information in no time.

AdriansNetlis
Автор

Super cool to see how the C++ code gets turned into Assembly and Machine Code, and also really helpful to have a better understanding of what the compiler is actually doing!

svenbtb
Автор

Great videos, thank you so much. I have been watching your videos. For brief notes, i asked gpt "How the C++ Compiler Works":

The C++ compiler transforms C++ source code into executable machine code through several stages. Understanding these stages can help you write more efficient and error-free code. Here’s an overview of how the C++ compiler works:

### 1. **Preprocessing**
- **Input:** Source code files (`.cpp`, `.h`).
- **Output:** Preprocessed source code.
- **Actions:**
- **Macro Expansion:** Expands all macros defined with `#define`.
- **File Inclusion:** Inserts the content of the included header files (`#include` directives).
- **Conditional Compilation:** Evaluates `#if`, `#ifdef`, `#ifndef`, and similar directives to include or exclude parts of the code.
- **Removing Comments:** Strips out comments from the source code.

### 2. **Compilation**
- **Input:** Preprocessed source code.
- **Output:** Assembly code.
- **Actions:**
- **Syntax Analysis (Parsing):** Checks the code for syntactical correctness. Converts the code into an Abstract Syntax Tree (AST).
- **Semantic Analysis:** Ensures the code adheres to the language rules and checks for semantic errors (e.g., type checking, variable declarations).
- **Intermediate Code Generation:** Converts the AST into an intermediate representation (IR).
- **Optimization:** Performs various optimizations on the IR to improve performance and reduce the code size.
- **Code Generation:** Translates the optimized IR into assembly code for the target architecture.

### 3. **Assembly**
- **Input:** Assembly code.
- **Output:** Object code (machine code).
- **Actions:**
- Converts the human-readable assembly code into machine code instructions specific to the target CPU architecture.
- Produces an object file (`.o` or `.obj`), which contains the machine code and additional information like symbol tables and relocation information.

### 4. **Linking**
- **Input:** Object code files and libraries.
- **Output:** Executable file.
- **Actions:**
- **Symbol Resolution:** Resolves references to functions and variables across different object files.
- **Relocation:** Adjusts addresses within the code and data sections.
- **Library Linking:** Includes necessary code from static libraries (`.lib` or `.a`) or dynamic/shared libraries (`.dll` or `.so`).
- Produces the final executable file that can be run on the target system.

### 5. **Loading (Execution Time)**
- **Input:** Executable file.
- **Output:** Running process.
- **Actions:**
- The operating system loads the executable into memory.
- Allocates memory for the program’s stack, heap, and data segments.
- Resolves dynamic library references (if any).
- Transfers control to the program’s entry point (usually the `main` function).

### Additional Details
- **Error Handling:** At each stage, the compiler checks for errors and warnings, providing feedback to the programmer.
- **Debugging Information:** The compiler can include debugging information in the object files to help with debugging.
- **Optimization Levels:** Compilers often offer various optimization levels (e.g., `-O1`, `-O2`, `-O3` in GCC) to control the trade-off between compilation time and the performance of the generated code.

### Example of a Compilation Process
For a file `main.cpp`, the typical compilation process using a command-line tool like `g++` might look like this:

```sh
g++ -o main main.cpp
```

This command tells the `g++` compiler to preprocess, compile, assemble, and link `main.cpp` into an executable named `main`.

By breaking down the compilation process into these stages, you can better understand how your C++ code is transformed into a running program and optimize or debug each part of the process effectively.

eda-wm