liveBook · Manning

Linker

As explained in Modern C, Third Edition

A linker is a tool that combines compiled program components and resolves references to produce an executable or other linked program image.

A linker is an external program that connects identifiers with linkage across object files and libraries. For example, functions and objects declared with external linkage can refer to one shared definition, while system-library functions may reside in a library such as libc.so. The compiler frontend usually hides linking, and a dynamic linker may complete related work at program startup. section 13.2

External and internal linkage

An object such as a file-scope unsigned i can be defined in one place and named through an extern declaration elsewhere. The linker establishes that correspondence. By contrast, a file-scope object or function intended only for its own object file should have internal linkage, commonly enforced with static. section 13.2

Listing 13.2 An example of shadowing with an extern variable
#include <stdio.h> 1 

unsigned i = 1;        

int main(void) { 2 
  unsigned i = 2;        /* A new object */
  if (i) {            3 
    extern unsigned i;   /* An existing object */ 
    printf("%u\n", i);  /* prints 1 */
  } else {
    printf("%u\n", i);  /* prints 2 */
  }
}

Compare a block-scope extern declaration with a new local object of the same name: the extern refers to the existing externally linked object. section 13.2

How Modern C, Third Edition covers it

How deep it goes
Explains cross-object correspondence, library residence, and startup timing, but does not detail linker algorithms or command usage.
Examples
Uses extern declarations, file-scope static identifiers, object files, and the system library libc.so.
What it assumes
Assumes familiarity with C identifiers, declarations, scope, object files, and storage duration terminology.
What you can do after
Evaluate/understand why declarations in different object files can designate one shared function or object.
How it connects
Connects external linkage, internal linkage, storage duration, object files, system libraries, and dynamic linking.

Also explained in

Compare how 2 books explain linker →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.