Search code examples
clanguage-lawyerdeclarationdefinitionlinkage

External definition of an object declared with external linkage


I'm a bit confused by the wording in 6.9 p5 of N2310 C18:

If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof or _Alignof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one. 164)

QUESTION: Is it obvious from this quote that the external definition somewhere in the program (if any) should also declare an identifier with external linkage?

As I emphasized somewhere in the entire program there shall be exactly one external definition for the identifier. It does not specify which linkage the definition should declare the identifier with. Example:

tu1.c:

int a = 10;

tu2.c:

static int a = 20;

Formally speaking we have one external definition for identifier a declared in tu1.c and another one in tu2.c so we could apply the quote I cited above to this example.

Although to denote the same entity identifiers declared in different should all be declared with external linkage as specified in 6.2.2/2:

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function.

Which is not the case here.


Solution

  • See C11 §6.2.2 Linkages of identifiers:

    … There are three kinds of linkage: external, internal, and none.

    In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

    If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

    Emphasis added.

    If a file scope variable is specified with static, it has internal linkage and isn't relevant to a discussion of variables with external linkage.