Comment

revised: 01/26/2008, 06/25/2017


Puzzles SL31 ... SL40

These are additional puzzles that deal with linkage. For reference, here again are the rules for linkage.


Linkage Rules (Again)

Linkage determines how multiple occurrences of the same identifier in different object files refer to the same or different entities. There are three kinds of linkage: no linkage, external linkage, and internal linkage.

1. No Linkage:

1.1 A declaration of an identifier with no linkage denotes a unique entity. (So the linker is not involved. The entity is completely contained in one file.) If there are several entities in the project with the same name and no linkage, scope determines which one a statement refers to.

1.2 The following have no linkage:

  1. an identifier declared to be anything other than a variable or a function,
  2. a function parameter,
  3. a identifier declared inside a block (and not explicitly declared external.)

2. External Linkage:

2.1 Several source files and libraries may declare a particular identifier with external linkage. Each declaration of that particular identifier denotes the same entity. It is the job of the linker to ensure that there is just one entity for that identifier in the executable file.

2.2 A variable declared outside of any function has external linkage unless otherwise specified (x in the previous puzzle has external linkage). Sometimes variables declared like this are called global variables.

2.3 A function name has external linkage unless otherwise specified (see foo and main, above.)

2.4 The keyword extern gives external linkage to an identifier and gives access to an entity defined in another file. If an identifier would already have external linkage, declaring it extern does not affect it.

3. Internal Linkage:

3.1 An identifier that has internal linkage is unique within its source file, but is invisible to other source files. If other source files use this same identifier, they refer to other entities.

3.2 The keyword static gives internal linkage to an identifier that otherwise would have external linkage. (This is the only way to get internal linkage.)



Next Page         Home