Program Linkage
Related Blog Items
- Name Mangling
- Can we use printf() in an ISR?
- Program that prints itself
- C++ Advanced Tutorial - Lesson 7
- Stream buffering
Linkage determines whether identifiers that have identical names refer to the same object, function, or other entity, even if those identifiers appear in different translation units. The linkage of an identifier depends on how it was declared. There are three types of linkages: external, internal, and no linkage.
* Identifiers with external linkage can be seen (and refered to) in other translation units.
* Identifiers with internal linkage can only be seen within the translation unit.
* Identifiers with no linkage can only be seen in the scope in which they are defined.
Linkage does not affect scoping, and normal name lookup considerations apply.
[Note] You can also have linkage between C++ and non-C++ code fragments, which is called language linkage. Language linkage enables the close relationship between C++ and C by allowing C++ code to link with that written in C. All identifiers have a language linkage, which by default is C++. Language linkage must be consistent across translation units, and non-C++ language linkage implies that the identifier has external linkage.
Internal Linkage:
The following kinds of identifiers have internal linkage:
* Objects, references, or functions explicitly declared static.
* Objects or references declared in namespace scope (or global scope in C) with the specifier const and neither explicitly declared extern, nor previously declared to have external linkage.
* Data members of an anonymous union.
* C++Function templates explicitly declared static.
* C++Identifiers declared in the unnamed namespace.
A function declared inside a block will usually have external linkage. An object declared inside a block will usually have external linkage if it is specified extern. If a variable that has static storage is defined outside a function, the variable has internal linkage and is available from the point where it is defined to the end of the current translation unit.
If the declaration of an identifier has the keyword extern and if a previous declaration of the identifier is visible at namespace or global scope, the identifier has the same linkage as the first declaration.
External Linkage:
C In global scope, identifiers for the following kinds of entities declared without the static storage class specifier have external linkage:
* An object.
* A function.
If an identifier in C is declared with the extern keyword and if a previous declaration of an object or function with the same identifier is visible, the identifier has the same linkage as the first declaration. For example, a variable or function that is first declared with the keyword static and later declared with the keyword extern has internal linkage. However, a variable or function that has no linkage and was later declared with a linkage specifier will have the linkage that was expressly specified.
In namespace scope, the identifiers for the following kinds of entities have external linkage:
* A reference or an object that does not have internal linkage.
* A function that does not have internal linkage.
* A named class or enumeration.
* An unnamed class or enumeration defined in a typedef declaration.
* An enumerator of an enumeration that has external linkage.
* A template, unless it is a function template with internal linkage.
* A namespace, unless it is declared in an unnamed namespace.
If the identifier for a class has external linkage, then, in the implementation of that class, the identifiers for the following will also have external linkage:
* A member function.
* A static data member.
* A class of class scope.
* An enumeration of class scope.
No Linkage:
The following kinds of identifiers have no linkage:
* Names that have neither external or internal linkage
* Names declared in local scopes (with exceptions like certain entities declared with the extern keyword)
* Identifiers that do not represent an object or a function, including labels, enumerators, typedef names that refer to entities with no linkage, type names, function parameters, and template names
You cannot use a name with no linkage to declare an entity with linkage. For example, you cannot use the name of a class or enumeration or a typedef name referring to an entity with no linkage to declare an entity with linkage. The following example demonstrates this:
int main() { struct A { }; //? extern A a1; typedef A myA; //? extern myA a2; }
The compiler will not allow the declaration of a1 with external linkage. Class A has no linkage. The compiler will not allow the declaration of a2 with external linkage. The typedef name a2 has no linkage because A has no linkage.
Linkage Specifications — Linking to Non-C++ Programs:
C++Linkage between C++ and non-C++ code fragments is called language linkage. All function types, function names, and variable names have a language linkage, which by default is C++.
You can link C++ object modules to object modules produced using other source languages such as C by using a linkage specification. The syntax is:
extern–string_literal–+-declaration
The string_literal is used to specify the linkage associated with a particular function. String literals used in linkage specifications should be considered as case-sensitive. All platforms support the following values for string_literal
“C++”
Unless otherwise specified, objects and functions have this default linkage specification.
“C”
Indicates linkage to a C procedure
Calling shared libraries that were written before C++ needed to be taken into account requires the #include directive to be within an extern “C” {} declaration.
extern “C” {
#include “shared.h”
}
The following example shows a C printing function that is called from C++.
//? in C++ program
extern "C" int displayfoo(const char *); int main() { return displayfoo("hello"); }
/*? in C program? ? ? ? */
#include <stdio .h> extern int displayfoo(const char * str) { while (*str) { putchar(*str); putchar(' '); ++str; } putchar('\n'); }</stdio>
[tags]program linkage, internal linkage, external linkage[/tags]
Popularity: 20%
You need to log on to convert this article into PDF
Related Blog Items - Name Mangling
- Can we use printf() in an ISR?
- Program that prints itself
- C++ Advanced Tutorial - Lesson 7
- Stream buffering
Related Blog Items
- Name Mangling
- Can we use printf() in an ISR?
- Program that prints itself
- C++ Advanced Tutorial - Lesson 7
- Stream buffering
No Comments
No comments yet.