liveBook · Manning

External linkage

As explained in Modern C, Third Edition

External linkage is the property allowing declarations in different translation units to designate the same C entity.

External linkage allows declarations in different translation units to designate the same C object or function. The linker uses this relationship when combining object files, so each reference reaches the corresponding entity. section 13.2

An extern declaration reaches an existing object

A file-scope definition creates an object named i. Inside main, a block-scope declaration using extern does not create the local i again; it refers to the existing externally linked object, so the branch prints 1 rather than the local value 2. section 13.2

TAKEAWAY:

  • Use external linkage when multiple object files must access the same object or function. section 13.2
  • Do not equate extern with external linkage: extern may refer to identifiers with either external or internal linkage. section 13.2
  • Contrast file-scope objects or functions without cross-file connections, which should have internal linkage. 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 */
  }
}

Inspect the complete example showing a local identifier shadowing an externally linked object and an extern declaration restoring access to that object. section 13.2

How Modern C, Third Edition covers it

How deep it goes
Explains cross-file identity, linker correspondence, extern ambiguity, and contrast with internal linkage, without detailing linker mechanics.
Examples
Uses a shadowed global unsigned object, a block-scope extern declaration, and output distinguishing the existing object from a local one.
What it assumes
Assumes familiarity with declarations, scopes, object files, and linking while clarifying linkage terminology.
What you can do after
Evaluate whether declarations across object files can designate one shared object or function and distinguish extern from linkage.
How it connects
Connects storage duration, scope, visibility, internal linkage, extern, object files, and the linker.

Also explained in

Compare how 2 books explain external linkage →

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.