TAGS :Viewed: 6 - Published at: a few seconds ago

[ What are the linkage of the following functions? ]

When I was reading the c++ 03 standard (7.1.1 Storage class specifiers [dcl.stc]), there are some examples as below, I'm not able to tell how the linkage of each successive declarations is determined? Could anyone help here? Thanks in advance!

static char* f();    // f() has internal linkage
char* f() 
       { /* ... */ } // f() still has internal linkage

char* g();           // g() has external linkage
static char* g() 
       { /* ... */ } // error: inconsistent linkage

void h();
inline void h(); // external linkage

inline void l();
void l(); // external linkage

inline void m();
extern void m(); // external linkage

static void n();
inline void n(); // internal linkage

static int a; // a has internal linkage
int a; // error: two definitions

static int b; // b has internal linkage
extern int b; // b still has internal linkage

int c; // c has external linkage
static int c; // error: inconsistent linkage

extern int d; // d has external linkage
static int d; // error: inconsistent linkage

UPD: Additionally, how can I understand the statement in the standard,

The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same object name or the same overloading of a function name shall imply the same linkage. Each function in a given set of overloaded functions can have a different linkage, however.

Answer 1


Unless you use static on a free standing function or variable declared at global scope they will have external linkage.

Note that using the keyword inline on a function does not change the linkage of an function.

Another important point to note is that const variables have Internal Linkage in C++ unlike in C.


The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same object name or the same overloading of a function name shall imply the same linkage. Each function in a given set of overloaded functions can have a different linkage, however."

An declaration introduces an Identifier to the compiler, so that the compiler can know its type.In C++ an identifier can be declared as many times as you want, for example:

void doSomething(int, double);         // External Linkage
extern void doSomething(int, double);  // External Linkage is explicitly mentioned 

The quoted passage implies that all such declarations for the same identifier should specify the same linkage. For eg, following would be invalid:

void doSomething(int, double);        //External Linkage
static void doSomething(int, double); //Internal Linkage due to static

While, overloaded versions of the same function can have different linkages, so following is valid:

void doSomething(int, double);
extern void doSomething(int, double);
void doSomething(double,int);