In C++26, the interaction between external linkage and module linkage is not that they are two distinct, competing types of linkage. Instead, modules provide a sophisticated mechanism to control and manage the visibility of entities that possess external linkage.
Let's break down each concept and then explain their interaction.
1. External Linkage
Definition: An entity (variable, function, class, etc.) has external linkage if its name can be referred to from different translation units. This means that if you define a function foo() in file1.cpp with external linkage, you can call foo() from file2.cpp (provided you declare it there, e.g., via a header file).
Key Characteristics:
Default for Globals: Non-static global variables and non-static non-member functions typically have external linkage by default.
extern Keyword: For variables, extern explicitly declares that a variable is defined elsewhere and has external linkage. For functions, it's implied.
One Definition Rule (ODR): For any entity with external linkage, there can be only one definition in the entire program. Violating ODR leads to linker errors or undefined behavior.
Traditional Sharing: In pre-module C++, external linkage, facilitated by header files (for declarations), was the primary way to share code across multiple .cpp files.
Example (Traditional):
cpp
// my_header.h
#pragma once
void myFunction(); // Declaration with implied external linkage
extern int myGlobalVar; // Declaration with explicit external linkage
// my_source.cpp
#include "my_header.h"
#include <iostream>
void myFunction() { // Definition with external linkage
std::cout << "myFunction called. myGlobalVar: " << myGlobalVar << std::endl;
}
int myGlobalVar = 10; // Definition with external linkage
// main.cpp
#include "my_header.h"
int main() {
myFunction(); // Calls the function defined in my_source.cpp
return 0;
}
2. Module Linkage (More Accurately: Linkage within Modules)
"Module linkage" isn't a new, separate category of linkage like "external" or "internal." Instead, modules introduce a new way to control the visibility and accessibility of entities, profoundly impacting how external linkage is managed.
Core Concept of Modules:
Modules provide a superior encapsulation mechanism compared to traditional header files. They allow you to explicitly define what parts of your code are exported (public API) and what parts are internal to the module.
Key export Keyword:
The export keyword is central to module visibility. When you export a declaration within a module interface unit:
It means the corresponding definition (which has external linkage) becomes visible and usable to other translation units that import that module.
Non-exported entities within a module are generally not visible outside that module, even if they would otherwise have external linkage. This is the essence of module encapsulation.
Example (Module Basics):
cpp
// my_module.ixx (Module Interface Unit)
export module MyModule; // Declares this file as the interface for 'MyModule'
export void exportedFunction(); // This function will be visible outside MyModule
void internalFunction(); // This function is internal to MyModule
// (even if it has external linkage within the module)
export int exportedVar = 20; // This variable will be visible outside MyModule
int internalVar = 30; // This variable is internal to MyModule
// (even if it has external linkage within the module)
// (Optional) my_module.cpp (Module Implementation Unit - not strictly required for simple cases)
// module MyModule; // Associates this file with the 'MyModule' module
// #include <iostream>
//
// void exportedFunction() {
// std::cout << "exportedFunction called. internalVar: " << internalVar << std::endl;
// }
//
// void internalFunction() {
// std::cout << "internalFunction called." << std::endl;
// }
3. How External and Module Linkage Interact in C++26
The interaction is symbiotic: Modules provide a controlled gateway for entities with external linkage.
Exported Entities Have External Linkage and Are Visible:
When you export a declaration (e.g., export void myFunction();) within a module, you are explicitly stating that myFunction is part of the module's public interface.
The definition of myFunction (wherever it resides within the module's implementation) must have external linkage. If it had internal linkage (e.g., static void myFunction();), exporting it would be ill-formed.
For an entity that is exported, its name becomes visible to any translation unit that imports the module. The linker will then resolve this name to the single definition of that entity, upholding the ODR.
Non-Exported Entities May Still Have External Linkage, But Are Not Visible:
Consider a global variable int internalVar; defined within a module's implementation. By default, it has external linkage within that module.
However, because it's not exported, its name is not visible to consumers of the module. You cannot import MyModule; and then try to access internalVar.
This is the core of module encapsulation: Modules restrict visibility, not necessarily the underlying linkage type. An entity can have external linkage, but if it's not exported, it behaves as if it has internal linkage from the perspective of external consumers.
ODR Enforcement is Enhanced:
Modules make ODR violations less likely because the compiler has a clearer understanding of what is defined where. If an entity is exported from a module, that module is the authoritative source for its definition. Trying to define the same entity (with the same name and signature) with external linkage in another translation unit would be an ODR violation the compiler is better equipped to detect.
Analogy:
Think of a module as a building with a front door and many internal rooms.
External Linkage: Is like having a unique street address for the building.
export Keyword: Is like painting a sign on the front door that says "This way to the exportedFunction room!"
Non-Exported Entities: Are like rooms within the building that don't have signs on the front door. They exist (and have an internal "address"), but you can't find them from the street unless you're inside the building (i.e., within the module's own implementation).
Summary of Interaction:
Modules don't replace external linkage; they provide a structured and explicit way to declare and manage entities that will have external linkage.
The export keyword is the bridge: it marks declarations whose corresponding definitions (which must have external linkage) are to be made visible to importing translation units.
Entities within a module that are not exported remain internal to the module's boundary, regardless of their actual linkage type (internal or external) within the module's own scope. This provides strong encapsulation and reduces the chance of name collisions and ODR violations.
In essence, C++26 modules allow you to leverage the power of external linkage for sharing code, while significantly improving encapsulation, build times, and the clarity of APIs compared to the traditional header-file model.