- Extern linkage allows functions and variables to be accessed across different translation units.
- It enables the sharing of definitions between multiple files in a program.
- The
extern
keyword is used to declare variables or functions that are defined in another translation unit. - It informs the compiler that the variable or function exists, but its definition will be found elsewhere.
extern
can be used to declare global variables or functions in a header file, making them accessible across multiple source files.
- The
extern "C"
construct specifies that the enclosed declarations should use C linkage rather than C++ linkage. - This is essential for linking C++ code with C libraries or when calling C functions from C++.
extern "C"
should be used to prevent name mangling for functions or variables declared within it. This ensures compatibility with C code.
- When a function is declared, it has external linkage unless specified
otherwise (for example, if declared
static
). - This means the function can be called from other translation units
without needing the
extern
keyword.