How the C++ Linker Works

The Cherno

The Cherno

15 min, 52 sec

Detailed explanation of the linking process in programming with examples and error handling.

Summary

  • Linking is the process of going from source code files to an executable binary, following the compilation stage.
  • The linker's primary role is to find and link symbols and functions across different files into a single program.
  • Unresolved symbols or multiply defined symbols can lead to linker errors which need to be addressed for successful compilation.
  • Static and inline functions, as well as proper declarations and definitions, are key to avoiding multiple definition errors.
  • The video demonstrates how to fix common linking errors using Visual Studio and discusses compile-time versus link-time errors.

Chapter 1

Introduction to Linking

0:00 - 1 min, 16 sec

Introduction to the concept of linking in the compilation process.

Introduction to the concept of linking in the compilation process.

  • Linking is the step after compiling source code files, resulting in an executable binary.
  • Linking connects symbols and functions from multiple object files into one cohesive program.
  • Even a single file program requires linking to identify the entry point, such as the main function.

Chapter 2

Compiling vs Linking

1:16 - 1 min, 57 sec

Differentiation between compiling and linking stages using Visual Studio.

Differentiation between compiling and linking stages using Visual Studio.

  • Compilation and linking are two distinct stages that can be separately triggered in Visual Studio.
  • Compilation errors and linking errors produce different error messages and codes.
  • Compiling is successful if syntax is correct, while linking requires correct definitions and entry points.

Chapter 3

Handling Linking Errors

3:13 - 1 min, 7 sec

Exploration of common linking errors and their resolution.

Exploration of common linking errors and their resolution.

  • Linking errors occur when the linker cannot resolve symbols or when multiple definitions of a symbol are found.
  • Resolving errors involves ensuring the correct declaration of functions and entry points across files.
  • Examples include adding missing main functions, resolving duplicate symbols, and correcting function declarations.

Chapter 4

Example of a Working Application

4:20 - 1 min, 22 sec

Demonstration of a working application and the impact of splitting code across files.

Demonstration of a working application and the impact of splitting code across files.

  • A sample code with functions 'log' and 'multiply' is shown to work as a single file application.
  • Splitting the log function into a separate file illustrates the need for correct function declarations for linking.

Chapter 5

Unresolved External Symbol Error

5:42 - 1 min, 54 sec

Investigation of the 'unresolved external symbol' linking error and its fix.

Investigation of the 'unresolved external symbol' linking error and its fix.

  • The error arises when the linker cannot find a symbol it needs to link, such as a function call.
  • The video shows how to resolve the error by ensuring that the function definition matches the declaration across files.

Chapter 6

Multiple Definition Error

7:36 - 1 min, 43 sec

Discussion of the 'multiply defined symbols' linking error and methods to avoid it.

Discussion of the 'multiply defined symbols' linking error and methods to avoid it.

  • This error occurs when identical symbols are defined in multiple object files.
  • The video demonstrates resolving this error by using static or inline specifications, or by consolidating definitions into one translation unit.

Chapter 7

Final Thoughts on Linking

9:19 - 6 min, 18 sec

Summary of linking concepts and a reminder of the importance of understanding error types.

Summary of linking concepts and a reminder of the importance of understanding error types.

  • Linking is essential for combining object files and libraries into a final executable.
  • Different types of linking include static and dynamic, which will be discussed in a future video.
  • Understanding compiling versus linking errors is crucial for effective debugging.

More The Cherno summaries

PATH TRACER made by 15-YEAR-OLD in C++ OpenGL! // Code Review

PATH TRACER made by 15-YEAR-OLD in C++ OpenGL! // Code Review

The Cherno

The Cherno

A detailed code review of a path tracer project created by a 15-year-old Ukrainian programmer, exploring the code structure, rendering techniques, and potential improvements.

How the C++ Compiler Works

How the C++ Compiler Works

The Cherno

The Cherno

The video explains the C++ compiler process in detail, from pre-processing to object file generation.

Variables in C++

Variables in C++

The Cherno

The Cherno

A comprehensive explanation of variables in C++, covering their purpose, types, memory allocation, and the relationship between data types and memory size.