C++

Undefined Reference to Static Member Fix

Declaring a static data member inside a class does not define it. The member exists as one object for the whole program, and that object has to be defined in exactly one translation unit — unless the member is declared inline, or the program only ever reads a compile-time value from it without odr-using it.

Written November 26, 2026 · Tested with GCC 13.3.0 · 10 min read

By the end of this guide
  • The error: a static member is declared, odr-used, and never defined
  • Fix 1: define the member out of line, in one source file
  • Fix 2: declare the member inline, and define it in the class (C++17)
  • The neighboring case: reading a const static value needs no definition — until its address is taken

Written November 26, 2026

Tested with GCC 13.3.0

What this guide is

This message is not about a missing variable declaration. The member named in the error is declared — it is written right there in the class, and the compiler accepted every file that used it. What is missing is the member's definition: the one place in the program where the object itself is created. A static data member is not associated with any object of its class; it is one independent variable with static storage duration for the whole program, and the declaration inside the class definition is not a definition of it. The static keyword appears on the declaration inside the class, and not on the definition outside it.

That split is invisible while the program only mentions the member in ways that need its value at compile time, or does not use it at all. It becomes a link error the moment the member is odr-used in a way that needs the object to exist: its address is taken, or a reference is bound to it. At that point the rule is exact — one and only one definition of every non-inline variable that is odr-used is required to appear in the entire program, and if an entity is odr-used, its definition must exist somewhere in the program; a violation of that is usually a link-time error. The linker is the first tool in the chain that can see the whole program, so the linker is where the missing definition is reported, in a file that may have nothing wrong in it.

The examples here use GCC 13.3.0 with C++20 sources. Each broken link reports that ld returned exit status 1; each fixed program builds, runs, and exits with status 0.

The error: a static member is declared, odr-used, and never defined

Start with a class that holds one configuration value as a static member, in the pre-C++17 style — a plain declaration in the class, no inline keyword, no initializer:

C++
// config.h
#pragma once

struct Config {
    static int limit;
};

The consumer takes the address of the member and prints the value through that address. Taking the address is an odr-use: the program needs the object itself, not just a value the compiler can supply from the declaration:

C++
// broken_main.cpp
#include "config.h"

#include <iostream>

int main() {
    const int* p = &Config::limit;
    std::cout << *p << "\n";
    return 0;
}

Compile and link:

Terminal
$ g++ -std=c++20 -c broken_main.cpp -o broken_main.o
$ g++ broken_main.o -o broken_app
/usr/bin/ld: broken_main.o: warning: relocation against `_ZN6Config5limitE' in read-only section `.text'
/usr/bin/ld: broken_main.o: in function `main':
broken_main.cpp:(.text+0xf): undefined reference to `Config::limit'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

Read the error closely, because its shape is the diagnosis. Compilation succeeded: the declaration in the header is complete enough to compile every use, including taking the member's address. The failure is only at link, and the one undefined symbol is the member itself (the mangled _ZN6Config5limitE). The relocation and DT_TEXTREL warnings are side effects of the missing symbol, not separate defects. Binding a reference to the member fails the same way, for the same reason: an object is odr-used if its value is read (unless it is a compile-time constant) or written, its address is taken, or a reference is bound to it. Any use from that list needs the one definition, and this program has none — no file defines Config::limit, so no object in the link supplies its address.

Fix 1: define the member out of line, in one source file

The classic repair — the form that works in every standard from C++11 onward — is to define the member once, at namespace scope in a single source file. The definition does not repeat the static keyword, and it is where an initializer belongs:

C++
// config.cpp
#include "config.h"

int Config::limit = 42;

Link that file with the unchanged consumer:

Terminal
$ g++ -std=c++20 broken_main.cpp config.cpp -o fixed_app
$ ./fixed_app
42

The program prints 42 and exits with status 0. Two constraints follow from the one-definition rule, and both matter in a real project. First, the definition goes in exactly one source file: putting it in the header, or in a second source file, gives every translation unit that includes it its own definition, and the error changes from undefined reference to multiple definition. Second, if the definition already exists somewhere in the project, the defect is in the build, not the source — the file containing it is missing from the target's source list — and adding a second definition elsewhere trades this error for the duplicate. Find the existing definition before writing a new one.

Fix 2: declare the member inline, and define it in the class (C++17)

Since C++17, a static data member can be declared inline. An inline variable may be defined in more than one translation unit as long as every definition is identical, it has the same address in every translation unit, and its definition must be reachable in every translation unit where it is used. For a static member, that means the in-class declaration itself can carry the initializer and be the definition — no out-of-line definition is needed at all:

C++
// inline_main.cpp
#include <iostream>

struct Settings {
    inline static int limit = 42;
};

int main() {
    const int* p = &Settings::limit;
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 inline_main.cpp -o inline_app
$ ./inline_app
42

The program prints 42 and exits with status 0, with the member odr-used in exactly the way that failed before — its address taken — and no second file in the build. The order of the keywords does not matter: inline static and static inline declare the same inline static member. This is the better form for a header-only class or a member whose value is part of the header's interface, because the definition travels with the declaration and cannot be left out of a target's source list. It requires C++17 or later; a project still compiled as C++11 or C++14 uses the out-of-line definition above. A constexpr static data member is implicitly inline since C++17, so it follows this same route and likewise needs no out-of-line definition.

The neighboring case: reading a const static value needs no definition — until its address is taken

There is one common shape that links with no definition at all, and it is the reason this error often appears late, when a program that has worked for months gains one new use of an old member. A static data member of integral or enumeration type that is declared const (and not volatile) can be initialized right inside the class definition:

C++
// const_read.cpp
#include <iostream>

struct Limits {
    static const int max = 100;
};

int main() {
    std::cout << Limits::max << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 const_read.cpp -o const_read_app
$ ./const_read_app
100

The program prints 100 and exits with status 0, with no definition of Limits::max anywhere. Reading the value here is not an odr-use: the member is usable in constant expressions, and the expression applies the lvalue-to-rvalue conversion to it, which yields the constant 100 without ever needing the object's address. The compiler supplies the value from the initializer in the declaration, and no reference to the object reaches the linker.

Take the member's address, and the same class in the same form fails:

C++
// const_addr.cpp
#include <iostream>

struct Limits {
    static const int max = 100;
};

int main() {
    const int* p = &Limits::max;
    std::cout << *p << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 -c const_addr.cpp -o const_addr.o
$ g++ const_addr.o -o const_addr_app
/usr/bin/ld: const_addr.o: warning: relocation against `_ZN6Limits3maxE' in read-only section `.text'
/usr/bin/ld: const_addr.o: in function `main':
const_addr.cpp:(.text+0xf): undefined reference to `Limits::max'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

The in-class initializer did not become a definition when the address was taken. If a const non-inline static data member is odr-used, a definition at namespace scope is still required — but it cannot have an initializer, because the initializer already lives in the class. The definition for this shape is therefore a bare redeclaration, const int Limits::max;, in one source file; with that line added, the address-taking program builds, prints 100, and exits with status 0. Passing the member to a function that takes a const int& is the same odr-use in disguise — a reference is bound to it — and is the usual way this shape breaks in practice: the call site looks like a plain read, and only the parameter type reveals that the object's address is being passed.

The exact rule: the declaration declares, one definition defines, and odr-use is what forces it

cppreference, static members states the split directly: the static keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member; the declaration inside the class body is not a definition; and a static data member declared inline can be defined in the class definition, may specify an initializer, and does not need an out-of-class definition. The same page states the const case: if a const non-inline static data member or a constexpr static data member (before C++17 made constexpr members implicitly inline) is odr-used, a definition at namespace scope is still required, but it cannot have an initializer.

cppreference, Definitions and ODR supplies the forcing rule and the exception in one place. A declaration of a non-inline static data member inside a class definition declares but does not define it, while inline static inside the class defines it. One and only one definition of every non-inline function or variable that is odr-used is required to appear in the entire program. And the informal definition of the term itself: an object is odr-used if its value is read (unless it is a compile-time constant) or written, its address is taken, or a reference is bound to it — and if an entity is odr-used, its definition must exist somewhere in the program, a violation of which is usually a link-time error.

Put together, the three statements predict every shape in this guide. A plain static member that is odr-used and never defined fails at link, because the declaration was never a definition. An out-of-line definition in exactly one file satisfies the rule for every standard version. An inline static member carries its definition in the class, so no out-of-line definition is required. And a const static integral member read only for its constant value is not odr-used, so it links with no definition — right up to the first address-taking or reference-binding use, which odr-uses it and brings the requirement, and the error, back.

The fixes, ranked

  • First, for any project: add the out-of-line definition in exactly one source file — int Config::limit = 42;, with no static keyword — and confirm that file is in the target's source list. This works in every standard version and changes no declarations.
  • Second, for C++17 and later, especially in a header: declare the member inline static (or static inline) with its initializer in the class, and remove any out-of-line definition. The definition then travels with the header and cannot be omitted from a build.
  • Third, for a const static integral member that broke when its address was taken or a reference was bound to it: add the bare definition without an initializer — const int Limits::max; — in one source file, or make the member inline or constexpr if the project targets C++17 or later.
  • Fourth, if the definition already exists in the project, fix the build instead of the code: the file holding it is not in the link, and a second definition written elsewhere creates a multiple-definition error.
  • Fifth, do not move a non-inline definition into the header to make the error go away. Every translation unit that includes the header then defines the member, and the undefined reference becomes a multiple definition; a definition belongs in a header only when the member is declared inline.

Which fix for which cause

Cause Message Fix
Non-inline static data member odr-used (address taken, reference bound, value read or written) with no definition in the program undefined reference to 'Config::limit'; link exit status 1 Define the member once at namespace scope in one source file, without the static keyword, and link that file
Same member, class is header-only or the project targets C++17 or later undefined reference to 'Config::limit'; link exit status 1 Declare the member inline static with its initializer in the class; no out-of-line definition
Const static integral member with an in-class initializer, read only for its value No error; no definition required None — the read is not an odr-use, because the lvalue-to-rvalue conversion yields the constant
Same const member, address taken or bound to a const reference undefined reference to 'Limits::max'; link exit status 1 Add the bare definition const int Limits::max; (no initializer) in one source file, or declare the member inline / constexpr in C++17 or later
Definition exists in the project but its file is not in the target undefined reference to 'Config::limit'; link exit status 1 Add the existing file to the target's source list; do not write a second definition
Non-inline definition placed in a header included by several files Multiple definition of Config::limit at link Move the definition to one source file, or declare the member inline (C++17 or later)
## Checklist
  • Classify the error before searching: the compiler accepted every file, and only the link failed on the member itself — that is a missing definition, not a missing declaration, a typo, or a library problem.
  • Find the member's declaration in the class, then search the project for its definition — the qualified name, such as Config::limit, at namespace scope in a source file. No match means no definition exists; a match means its file is not in the link.
  • Check how the member is used at the failing site. Address taken, reference bound, or value read or written are odr-uses that force the definition; a plain read of a const integral member's constant value is not.
  • Apply the definition form that matches the member: an initializer is allowed on a plain member's out-of-line definition, and forbidden on the definition of a const member that already has an in-class initializer.
  • Keep exactly one definition. One missing definition and two competing definitions are the same rule failing in opposite directions, and fixing one by creating the other is the common way this error mutates instead of closing.
  • In C++17 and later, prefer inline static for members defined in headers, and remember that constexpr static members are implicitly inline — neither needs, nor should keep, an out-of-line definition.
  • After the fix, confirm the program does the work the error was blocking: the fixed programs here print the member's value through the pointer that odr-used it.

Sources

  • cppreference, Static members — for static members not being associated with class objects, for the declaration inside the class not being a definition and the static keyword not appearing on the out-of-class definition, for an inline static data member being definable in the class with no out-of-class definition required, for const integral static members being initializable in the class, and for an odr-used const non-inline static member still requiring a namespace-scope definition without an initializer.
  • cppreference, Inline specifier — for a static data member declared constexpr being implicitly an inline variable, for an inline variable having the same address in every translation unit, for more than one identical definition of an inline variable being permitted across translation units, and for the definition of an inline variable having to be reachable in every translation unit where it is used.
  • cppreference, Definitions and ODR — for a non-inline static data member declaration inside a class not being a definition while inline static is one, for one and only one definition of every non-inline odr-used variable being required in the entire program, and for the definition of odr-use: an object is odr-used if its value is read (unless it is a compile-time constant) or written, its address is taken, or a reference is bound to it, and an odr-used entity's definition must exist somewhere in the program.

Keep Exploring