Menu
How C++ Works

How C++ Works

How C++ Works - ▶️YouTube

A Deep Dive into C++ Compilation and Linking

Preprocessing: The Foundation

  • Directives, Not Code: Preprocessing directives like #include are not C++ code. They instruct the preprocessor, a tool that modifies the source code before compilation.
  • Header Inclusion: #include <iostream> tells the preprocessor to find the iostream header file and insert its contents into your source code at that specific location. This brings in declarations for standard input/output objects like cin and cout.
  • Macro Definitions: Other directives like #define create macros, which are essentially text replacements.

Compilation: Turning Source Code into Machine Code

  • Translation Units: Each .cpp file is a translation unit. The compiler processes each unit independently, translating the C++ code into assembly language and then into machine code (object code).
  • Object Files: The result of compiling a translation unit is an object file (often with a .obj or .o extension). This file contains the machine code for that specific source file.
  • Declarations vs. Definitions:
    • Declaration: Tells the compiler that a symbol (like a function or variable) exists, but doesn’t provide its implementation.
    • Definition: Provides the actual implementation of the symbol.
    • Example: In a header file, you might declare a function void myFunction();. In a .cpp file, you’d define it with its actual code.

Linking: Stitching Together the Pieces

  • Combining Object Files: The linker takes multiple object files (possibly from different .cpp files) and combines them into a single executable file (usually with a .exe extension).
  • Resolving Symbols: The linker resolves external references. If a function is declared in one file and defined in another, the linker ensures that the calls to that function in the first file are correctly linked to its implementation in the second file.
  • Libraries: The linker can also link with libraries, which are pre-compiled collections of object code for commonly used functions (like those in the C++ standard library).

Why Separate Files?

  • Modularity: Breaking code into multiple files improves code organization and maintainability.
  • Reusability: Header files allow you to easily reuse code in multiple projects.
  • Easier Compilation: Smaller files compile faster.

Key Points

  • The compilation process involves preprocessing, compilation, and linking.
  • Each .cpp file is compiled into an object file.
  • The linker combines object files and resolves external references.
  • Declarations and definitions are crucial for successful compilation and linking.
  • Separating code into multiple files enhances code organization and reusability.
// Log.cpp
#include <iostream>

// Defination
void Log(const char* message)
{
    std::cout << message << std::endl;
}

``` cpp // Main.cpp #include

// Declaration - we can also ignore the name of variable: Example: void Log(const char); void Log(const char message);

int main() { Log(“Hello World!!”); std::cin.get(); }