In C++, what does extern int frame_count; declare, and where must the matching definition of frame_count live?
answer
- a promise, not storage
- exactly one definition somewhere
- the header declares, one .cpp defines
- an initializer turns it into a definition
- const at namespace scope stays file-local
basics
~20 sIt declares a variable with external linkage without defining it, so no storage is created. Exactly one .cpp file must define it, as int frame_count = 0;, and every file that sees the declaration refers to that one variable.
solid answer
~50 s`extern int frame_count;` is a **declaration only**: it says a variable of type `int` named `frame_count` exists somewhere in the program, with **external linkage**, and creates no storage. It goes in a header so every file can use the variable. Exactly one `.cpp` file must hold the **definition**, `int frame_count = 0;`. With no definition anywhere, each file still compiles and the link typically fails with an undefined symbol; with the definition in the header, every includer defines it and the link fails the other way. An initializer makes even an `extern` declaration a definition. A `const` variable at namespace scope has internal linkage by default, so sharing one needs `extern` on its declarations and on the definition that one file provides. On a function declaration `extern` is redundant, since namespace-scope functions already have external linkage.
code
cpp · 16 lines#include <cstdio>
// What frames.h would hold: declarations only.
extern int frame_count; // declares; no storage here
extern const double tick_hz; // declares a const with external linkage
void on_frame();
// What frames.cpp would hold: the one definition of each.
int frame_count = 0; // defines it, initialized to 0
extern const double tick_hz = 60.0; // with an initializer, extern still defines
void on_frame() { ++frame_count; }
int main() {
for (int i = 0; i < 3; ++i) on_frame();
std::printf("%d frames at %.0f Hz\n", frame_count, tick_hz);
}go deeper
Recall that extern declares a variable defined elsewhere, that one .cpp file must define it, and which link error each mistake produces: undefined symbol for no definition, duplicate symbol for several.
Explain why const at namespace scope is file-local by default, why sharing one const object needs extern on the definition too, and why extern on a function changes nothing.
Read link errors quickly in terms of declarations and definitions, and challenge new mutable globals: one owner, one definition, and an explicit dependency is usually the better design.
Set the rule for shared state across modules: which globals are allowed, who owns their single definition, and how initialization order across files is kept from biting.
## What extern says A C++ program is compiled one **translation unit** at a time: a `.cpp` file plus everything it includes. When `main.cpp` uses a global variable defined in `frames.cpp`, the compiler of `main.cpp` needs to know the variable's name and type, and the **linker** later connects that use to the variable's storage. `extern` is how the first part is said without doing the second: ```cpp extern int frame_count; // declaration: exists somewhere, external linkage int frame_count = 0; // definition: storage, in exactly one .cpp file ``` The standard lists the declarations that are not definitions, and a declaration with the `extern` specifier **and no initializer** is one of them. Everything else about the variable, such as its type and its linkage, is fully stated, so code can read and write it. The declaration may be repeated any number of times, in any number of files, as long as every repetition agrees on the type. ## The usual layout - **`frames.h`** holds `extern int frame_count;` so that any file can use the counter. - **`frames.cpp`** includes `frames.h` and holds the one definition, `int frame_count = 0;`. - **Every other file** includes `frames.h` and uses `frame_count`, always reaching the same object. Including the header in `frames.cpp` matters: the compiler then checks that the declaration and the definition agree on the type. ## The two ways to get it wrong 1. **No definition anywhere.** Every file compiles, because a declaration is enough to compile a use. The program uses a variable that nothing defines, and the link typically fails. Apple's linker reports `Undefined symbols for architecture arm64` and names `_frame_count`; other linkers word it differently. 2. **The definition in the header.** `int frame_count = 0;` in `frames.h` is a definition, so every `.cpp` file that includes it defines the variable again. A non-inline variable may be defined only once in the program, and the link fails with a duplicate or multiple definition error. Since C++17 an `inline` variable is the one exception to this, and it is its own subject. ## When extern changes the meaning | Declaration at namespace scope | Definition? | Linkage | |---|---|---| | `extern int n;` | no | external | | `int n;` | yes, zero-initialized | external | | `extern int n = 0;` | yes, the initializer wins | external | | `const int k = 5;` | yes | **internal** | | `extern const int k = 5;` | yes | external | | `static int n;` | yes | internal | The `const` row is the trap. A non-volatile, non-template, non-inline `const` variable at namespace scope has **internal linkage** unless it is declared `extern` or an earlier declaration gave it external linkage. That rule lets `const int k = 5;` sit in a header without a duplicate-definition error, because each file gets its own private copy. To share **one** `const` object instead, declare it `extern const int k;` in the header and define it `extern const int k = 5;` in one `.cpp` file; the `extern` on the definition is what keeps it external when no earlier declaration is visible. ## extern on functions A function declared at namespace scope already has external linkage, and a declaration without a body is never a definition. `extern void on_frame();` therefore means exactly what `void on_frame();` means, and most code omits the keyword. The related spelling `extern "C"` is a different feature: it sets **language linkage**, which governs how the name is presented to the linker for C code, and belongs with name mangling. ## Why it matters - **Shared state across files** needs exactly one owner, and `extern` is how every other file names it without creating a second one. - **Link errors become readable**: an undefined symbol means a declaration with no definition, while a duplicate symbol means a definition where only a declaration belonged. - **Every user touches the same object.** Because there is exactly one `frame_count`, a debugger watch, a test that resets it and the code that increments it all reach the same storage; a per-file copy would silently break that. - **Mutable globals remain a design smell.** `extern` makes them possible; a function returning a reference to a function-local static, or an object passed explicitly, is often the better design, and the initialization-order issues of globals are a separate subject.
- Is extern int frame_count = 0; a declaration or a definition?A definition. A declaration with `extern` is a mere declaration only when it has no initializer; with one, it creates and initializes the variable. Putting it in a header therefore defines the variable in every file that includes it, and the link fails just as it would with `int frame_count = 0;`.
- Why can const int k_max = 5; sit in a header that many files include?A non-volatile `const` variable at namespace scope that is not inline, not a template and not declared `extern` has internal linkage. Each file that includes the header gets its own private `k_max`, so there is no duplicate definition. The cost is one copy per file, each with its own address; sharing one object takes `extern` or, since C++17, `inline`.
- extern int x; allocates storage for x in every file that includes it.
- extern makes a variable visible only inside the current file.
- Putting int x = 0; in a header is fine with include guards.
- A missing definition is reported by the compiler, not the linker.
- extern on a function declaration makes it callable from C.