What is linkage (internal vs external) in C++?

In C++, linkage refers to the visibility of identifiers (such as variables and functions) across different translation units (source files). There are two primary types of linkage: internal linkage and external linkage.

Internal Linkage

Identifiers with internal linkage are restricted to the translation unit in which they are declared. Other translation units cannot access or use these identifiers. This is typically achieved using the `static` keyword.

External Linkage

Identifiers with external linkage can be accessed from other translation units. By default, functions and global variables have external linkage unless specified otherwise. This allows them to be used across multiple files.

Examples

// Internal linkage example static int internalVar = 5; void internalFunction() { // This function can only be accessed within this file } // External linkage example int externalVar = 10; void externalFunction() { // This function can be accessed from other files }

C++ Linkage Internal Linkage External Linkage Static Keyword Translation Unit