Compiler

Linking

The motivation behind this is that sometimes, programs are very large, so it’s not convenient to assemble the whole program at once.

In practice, we often want to assemble and compile small parts of a program, typically one source file at a time.

Screen Shot 2022-10-25 at 11.02.01 AM.png The above is sort of an idea on how we can do it.

https://en.wikipedia.org/wiki/Linker_(computing)

Object files contain machine language with metadata about labels.

Linking is the process of combining multiple object files into one. It involves two main activities:

  1. relocating addresses in object files to account for the code from those files starting at an offset other than 0 in the resulting linked file
  2. resolving labels that are used in a different file than the one in which they are defined, by finding the value of the label in the symbol table metadata of the defining file and writing the value in all the places where the symbol is used.

Relocation / Relocating

Adjusting addresses in machine language code so the code works at a different starting address.

Internal Linkage

  • Internal Linkage (static keyword): The variable is only visible within the file (translation unit). Each file gets its own copy if declared separately.
  • External Linkage (default for non-static globals): The variable is visible across files and can be shared using extern.

NOTE: For static member variables and

Summary

ContextInternal Linkage?Notes
static global variables✅ YesLimited to the file (translation unit).
static functions (global)✅ YesCan’t be used outside the file.
static class members❌ NoBelong to the class, accessible globally.
static local variables❌ No (No Linkage)Bound to the function, but persist across calls.
So I guess there’s the programmer’s perspective (which is all about visibility between translation units), and then memory perspective, which is that it only exists in 1 memory location, and that is true for all kinds of static.