c - Accordance of linkage between declaration and definition -



c - Accordance of linkage between declaration and definition -

i wondering if c snippet below, in definition of f fails repeat f of static linkage, correct:

static int f(int); int f(int x) { homecoming x; }

clang not emit warning it. read clause 6.7.1 of c11 standard without finding reply question.

it possible imagine more questions along same vein, instance t1.c , t2.c below, , nice if reply general plenty apply of these, concerned first illustration above.

~ $ cat t1.c static int f(int); int f(int); int f(int x) { homecoming 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) { homecoming 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 here int f(int); ^ 1 error generated.

the rules linkage little confusing, , different functions , objects. in short, rules follows:

the first declaration determines linkage. static means internal linkage. extern means linkage declared, if none declared, external. if neither of them given, it’s same extern functions, , external linkage object identifiers (with definition in same translation unit).

so, valid:

static int f(int); // linkage of f internal. int f(int); // same next line. extern int f(int); // linkage declared before, internal. int f(int x) { homecoming x; }

this, on other hand, undefined behaviour (cf. c11 (n1570) 6.2.2 p7):

int f(int); // same if extern given, no declaration visible, // linkage external. static int f(int); // ub, declared external linkage. int f(int x) { homecoming x; } // fine if either of above // declarations removed.

most of covered in c11 6.2.2. n1570 draft:

(3) if declaration of file scope identifier object or function contains storage-class specifier static, identifier has internal linkage. 30)

(4) identifier declared storage-class specifier extern in scope in prior declaration of identifier visible31), if prior declaration specifies internal or external linkage, linkage of identifier @ later declaration same linkage specified @ prior declaration. if no prior declaration visible, or if prior declaration specifies no linkage, identifier has external linkage.

(5) if declaration of identifier function has no storage-class specifier, linkage determined if declared storage-class specifier extern. if declaration of identifier object has file scope , no storage-class specifier, linkage external.

30) function declaration can contain storage-class specifier static if @ file scope; see 6.7.1. 31) specified in 6.2.1, later declaration might hide prior declaration.

c language-lawyer c11

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -