Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The extern specifier

Reference for C++ extern: declaring variables defined elsewhere, external versus internal linkage, extern C for C interop, and extern template.

extern is about linkage: whether a name used in one translation unit (one .cpp file after preprocessing) refers to the same entity as that name in another. A name with external linkage can be shared between files. extern is used in three situations: to declare a variable without defining it, to give a block of declarations C linkage (extern "C"), and to suppress implicit template instantiation (extern template).

The distinction between a declaration and a definition is central. extern int x; says "an int named x exists somewhere" and allocates nothing. int x; or extern int x = 5; defines it. Exactly one definition may exist in the whole program (the One Definition Rule).

C++
extern int counter;        // declaration: defined in another file
extern "C" int c_func(int);   // C linkage: no C++ name mangling

Try it

C++ Declaration versus definition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ extern C and extern template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
extern type name;                 // declaration of a variable defined elsewhere
extern "C" declaration;           // one declaration with C linkage
extern "C" { declarations }      // a block of C-linkage declarations
extern template class-or-function-specialization;   // C++11
Form Meaning
extern T x; Declares x with external linkage without defining it.
extern T x = v; Defines x (the initializer makes it a definition) with external linkage, even if T is const.
extern "C" Gives functions and variables C language linkage. Use it for functions you export to C or load by name.
extern "C++" The default; useful to nest C++ declarations inside an extern "C" block.
extern template Explicit instantiation declaration: the template is not instantiated in this translation unit because another one provides it.

Usage notes

  • Put extern declarations in a header and the single definition in one .cpp. Including the declaration in the defining file lets the compiler check that both match.
  • Global non-const variables and all functions have external linkage by default; const and constexpr namespace-scope variables have internal linkage unless declared extern. Since C++17, inline variables are the simple way to share a constant defined in a header.
  • extern "C" functions cannot be overloaded (two overloads would have the same C name), cannot be members, and should not let exceptions cross into C code.
  • A typical C/C++ shared header wraps its declarations in #ifdef __cplusplus extern "C" { ... } #endif.
  • Linkage affects the symbol name only. The function's type still uses C++ types, so passing a std::string to an extern "C" function compiles but is a bad idea.
  • A mismatch between declaration and definition (different types for extern int x; and double x;) is undefined behavior without diagnostic across translation units.
  • extern template prevents duplicate instantiation and can speed up builds, for example for a heavily used std::basic_string<char>, which the standard library already instantiates.
  • Do not confuse linkage with scope. A name can be in scope but unresolved at link time ("undefined reference").