Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accordance of linkage between declaration and definition

I am wondering if the C snippet below, in which the definition of f fails to repeat that f is of static linkage, is correct:

static int f(int);

int f(int x) { return x; }

Clang does not emit any warning for it. I read clause 6.7.1 of the C11 standard without finding the answer to my question.

It is possible to imagine more questions along the same vein, for instance t1.c and t2.c below, and it would be nice if an answer was general enough to apply to some of these, but I am only really concerned about the first example above.

~ $ cat t1.c
static int f(int);

int f(int);

int f(int x) { return x; }
~ $ clang -c -std=c99 -pedantic t1.c
~ $ nm t1.o
warning: /Applications/Xcode.app/…/bin/nm: no name list
~ $ cat t2.c
int f(int);

static int f(int);

int f(int x) { return x; }
~ $ clang -c -std=c99 -pedantic t2.c
t2.c:3:12: error: static declaration of 'f' follows non-static declaration
static int f(int);
           ^
t2.c:1:5: note: previous declaration is here
int f(int);
    ^
1 error generated.
like image 392
Pascal Cuoq Avatar asked Dec 19 '22 10:12

Pascal Cuoq


1 Answers

The rules for linkage are a little confusing, and it is different for functions and objects. In short, the rules are as follows:

  • The first declaration determines the linkage.
  • static means internal linkage.
  • extern means linkage as already declared, if none is declared, external.
  • If neither of them is given, it’s the same as extern for functions, and external linkage for object identifiers (with a definition in the same translation unit).

So, this is valid:

static int f(int); // Linkage of f is internal.

int f(int); // Same as next line.

extern int f(int); // Linkage as declared before, thus internal.

int f(int x) { return x; }

This, on the other hand, is undefined behaviour (cf. C11 (n1570) 6.2.2 p7):

int f(int); // Same as if extern was given, no declaration visible,
            // so linkage is external.

static int f(int); // UB, already declared with external linkage.

int f(int x) { return x; } // Would be fine if either of the above
                           // declarations was removed.

Most of this is covered in C11 6.2.2. From the N1570 draft:

(3) 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. 30)

(4) For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible31), if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.

(5) If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern. If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

30) A function declaration can contain the storage-class specifier static only if it is at file scope; see 6.7.1.
31) As specified in 6.2.1, the later declaration might hide the prior declaration.

like image 181
mafso Avatar answered Jan 17 '23 16:01

mafso