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.
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:
- 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
- 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 usingextern
.
NOTE: For static
member variables and
- Im confused https://stackoverflow.com/questions/76786399/external-or-internal-linkage-for-static-class-member-functions
Summary
Context | Internal Linkage? | Notes |
---|---|---|
static global variables | ✅ Yes | Limited to the file (translation unit). |
static functions (global) | ✅ Yes | Can’t be used outside the file. |
static class members | ❌ No | Belong 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. |