Team LiB   Previous Section   Next Section

2.4 Linkage

Every name has linkage, which determines how the compiler and linker can use the name. Linkage has two aspects: scope and language. Scope linkage dictates which scopes have access to an entity. Language linkage dictates an entity's properties that depend on programming language.

2.4.1 Scope Linkage

Scope linkage can be one of the following:

Internal linkage

A name with internal linkage can be referred to from a different scope within the same source file. At namespace scope (that is, outside of functions and classes), static declarations have internal linkage, as do const declarations that are not also extern. Data members of anonymous unions have internal linkage. Names in an unnamed namespace have internal linkage.

External linkage

A name with external linkage can be referred to from a different scope, possibly in a different source file. Functions and objects declared with the extern specifier have external linkage, as do entities declared at namespace scope that do not have internal linkage.

No linkage

A name with no linkage can be referred to only from within the scope where it is declared. Local declarations that are not extern have no linkage.

2.4.2 Language Linkage

figs/acorn.gif

Every function, function type, and object has a language linkage, which is specified as a simple character string. By default, the linkage is "C++". The only other standard language linkage is "C". All other language linkages and the properties associated with different language linkages are implementation-defined.

You can specify the language linkage for a single declaration (not a definition) or for a series of declarations and definitions. When you specify linkage for a series of declarations and definitions, you must enclose the series in curly braces. A language linkage declaration does not define a scope within the curly braces. For example:

extern "C" void cfunction(int);
extern "C++" {
  void cppfunc(int);
  void cppfunc(double);
}

Language linkage is part of a function's type, so typedef declarations keep track of the language linkage. When assigning a function to a function pointer, the function and pointer must have the same linkage. In the following example, funcptr is a function pointer with "C" linkage (note the need for curly braces because it is a definition, not a declaration). You can assign a "C" function to funcptr, but not a "C++" function, even though the rest of the function type matches.

extern "C" { void (*funcptr)(int); }
funcptr = cfunction; // OK
funcptr = cppfunc;   // Error

C does not support function overloading, so there can be at most one function with "C" linkage of a given name. Even if you declare a C function in two different namespaces, both declarations refer to the same function, for which there must be a single definition.

Typically, "C" linkage is used for external functions that are written in C (such as those in the C standard library), but that you want to call from a C++ program. "C++" linkage is used for native C++ code. Sometimes, though, you want to write a function in C++ that can be called from C; in that case, you should declare the C++ function with "C" linkage.

figs/acorn.gif

An implementation might support other language linkages. It is up to the implementation to define the properties of each language: how parameters are passed to functions, how values are returned from functions, whether and how function names are altered, and so on. In many C++ implementations, a function with "C++" linkage has a "mangled" name, that is, the external name encodes the function name and the types of all its arguments. So the function strlen(const char*) might have an external name of strlen_ _FCcP, making it hard to call the function from a C program, which does not know about C++ name-mangling rules. Using "C" linkage, the compiler might not mangle the name, exporting the function under the plain name of strlen, which can be called easily from C.

    Team LiB   Previous Section   Next Section