C++
C++ Redefinition Error Fix
One name, defined twice in one file - a local variable in one form, a global in another, a function, a class, and an enum in the rest, each with a different first line from GCC. This guide separates the five shapes, reads the note that points back to the first definition in each one, and matches each shape to the same repair: keep one definition, and make the second one a use, an assignment, or a new name.
Written November 24, 2026 · Tested with GCC 13.3.0 · 10 min read
- The rule underneath: one definition, as many declarations as you need
- How to read the diagnostic
- The first failure: a local variable declared twice
- The second failure: a global variable defined twice
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
A name defined twice in one file stops the compile, and the fastest way through it is to read two lines: the error, which names the second definition, and the note under it, which points at the first one. Between those two lines sits the whole repair - one of the two definitions has to stop being a definition.
The five forms in this guide each reproduce the failure with a small, complete program, and the entity being defined is different in each one. A local variable declared twice in the same scope. A variable defined twice at namespace scope. A free function defined twice with the same signature. A class defined twice. And an enum defined twice, which GCC words differently from the other four. In each family the failing program is small, the full compiler output is shown, and the fix keeps exactly one definition.
GCC's wording is not uniform across the five forms, and this guide does not pretend it is. The local variable earns "redeclaration". The global variable, the function, and the class earn "redefinition". The enum earns "multiple definition of 'enum Color'" - the word redefinition never appears in that diagnostic. Four neighboring shapes in the traps are not this error at all: a type alias redeclared for a different type is a conflicting declaration, a member function defined twice inside its class cannot be overloaded with itself, a macro redefined to a different value is only a warning, and an identical redefinition is not diagnosed at all, and the same name in an inner block is shadowing, which compiles. One further program repeats declarations - a function prototype twice, an extern variable twice - with one definition each, and compiles: that is the boundary of this error.
Build every program in this guide with g++ -std=c++20. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: one definition, as many declarations as you need
Definitions and declarations are not the same act. A definition fully defines the entity it introduces - it gives a variable its storage, and an initial value when an initializer is present, a function its body, a class its member list, an enum its enumerators. Most other declarations only announce the entity: a function prototype, an extern variable declaration without an initializer, a forward declaration of a class, and an alias-declaration are all declarations that are not definitions.
The One Definition Rule then states the limit this guide is about: only one definition of any variable, function, class type, or enumeration type is allowed in any one translation unit - a single source file, as compiled - although some of these may have multiple declarations. That asymmetry is the whole rule. Declarations may repeat; definitions may not. The boundary program in the traps is the rule read from the permitted side: two prototypes and two extern declarations compile, because only one definition of each entity is present.
Scope decides whether two declarations are even about the same entity in the same place. Per cppreference on scope, each declaration is visible only in some scope, and a name declared in an inner block belongs to that block. Two variables with the same name in two different scopes are two entities - the shadowing program in the traps - while two definitions of one name in the same scope are one entity defined twice, which is every family in this guide.
One stage boundary matters here, because the words collide. This guide is about a compile error in one translation unit: the compiler sees both definitions in one file and rejects the second. A different failure happens across files, when a non-inline variable or function is defined in a header that two source files include: each file compiles on its own, and the linker, ld, reports "multiple definition of 'x'" because two object files each carry a definition. Same phrase family, different stage, different tool, different repair - for the header case the definition moves out of the header, or the entity is made inline, which per cppreference on the inline specifier permits more than one definition of a function or variable in a program as long as each appears in a different translation unit and the definitions are identical. Keep the two apart when you read an error: a diagnostic with a file and line number from g++ is the compiler in one file; a "multiple definition" line from ld with object files named is the linker across files. The enum family below is the awkward middle case in wording only - its "multiple definition" is a compiler error in one file, not a linker error, and the next sections keep it labeled that way.
How to read the diagnostic
Every family here has the same two-part shape. The error line sits on the second definition and names it: the line number is where the repeat is. Under it, a note sits on the first definition and says so - "previously declared here" for the local variable, "previously defined here" for the global variable and the function, "previous definition of 'struct Point'" for the class, "previous definition here" for the enum. Reading the note's line is how you find the first definition: do not search the file by name and guess which occurrence came first, because the compiler has already told you, with a line number, in the note.
So the edit starts from the pair, not from the error alone. Open the note's line, decide which of the two definitions is the one the program means to keep - usually the first, with the fuller member list, the intended initial value, or the real function body - and then change the other one. What the second one becomes depends on what it was trying to do, and the families below show the three common answers: it was a second value for the same variable, so it becomes an assignment; it was a second, distinct value, so it gets its own name; or it was half of one entity split in two, as with the class and the enum, so the two halves merge into the single kept definition.
The wording on the error line tells you which kind of entity you are looking at before you read any code. "Redeclaration" in these programs is the local variable. "Redefinition" is the global variable, the function, and the class. "Multiple definition of 'enum ...'" is the enum - and only the enum, in this guide's compiler diagnostics; the same words from the linker are the across-files failure described above, not this one.
The first failure: a local variable declared twice
Two declarations of x, both in the body of main, in the same scope:
#include <iostream>
int main() {
int x = 1;
int x = 2;
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_local fail_local.cpp
fail_local.cpp: In function ‘int main()’:
fail_local.cpp:5:9: error: redeclaration of ‘int x’
5 | int x = 2;
| ^
fail_local.cpp:4:9: note: ‘int x’ previously declared here
4 | int x = 1;
| ^
The wording is "redeclaration", not redefinition: error: redeclaration of 'int x', with the note 'int x' previously declared here pointing at line 4, the first declaration. A local variable's declaration is its definition - there is no separate prototype form for it inside a function - so the second line tries to create a second x in a scope that already has one.
What the second line usually means in real code is one of two things: a new value for the same variable, or a second variable that happened to reuse the name. The fix below takes the second reading - the program wants both values at once, so the second variable gets its own name:
#include <iostream>
int main() {
int x = 1;
int y = 2;
std::cout << x << ' ' << y << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_local fix_local.cpp
$ ./fix_local
1 2
The output is 1 2: both values, distinctly named, both alive at the print. If the intent had been the first reading - a new value for the same x - the fix is smaller still: drop the type from the second line and leave x = 2;, an assignment to the one variable. Either way, one declaration remains.
The second failure: a global variable defined twice
The same shape one scope out. Both definitions are at namespace scope, each with an initializer, so each one is a full definition of total:
#include <iostream>
int total = 1;
int total = 2;
int main() {
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_global fail_global.cpp
fail_global.cpp:4:5: error: redefinition of ‘int total’
4 | int total = 2;
| ^~~~~
fail_global.cpp:3:5: note: ‘int total’ previously defined here
3 | int total = 1;
| ^~~~~
The wording changes with the scope: error: redefinition of 'int total', and the note now reads 'int total' previously defined here. Two definitions of one namespace-scope variable in one translation unit is exactly what the One Definition Rule allows only one of.
The fix keeps the first definition and turns the second value into an assignment inside main, where assignments belong:
#include <iostream>
int total = 1;
int main() {
std::cout << total << ' ';
total = 2;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_global fix_global.cpp
$ ./fix_global
1 2
The output is 1 2: the defined value first, then the assigned value, from one variable with one definition. Note what did not happen here: the second line was not changed into an extern declaration by adding a keyword in front of the initializer - an extern declaration with an initializer is still a definition, per the definitions page. An extern declaration repeats legally only without an initializer, as the boundary program shows; per cppreference on storage class specifiers, a declaration with the extern specifier and no initializer declares the variable without defining it.
The third failure: a function defined twice
Two definitions of score, with the same signature and different bodies:
#include <iostream>
int score() {
return 10;
}
int score() {
return 20;
}
int main() {
std::cout << score() << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_func fail_func.cpp
fail_func.cpp:7:5: error: redefinition of ‘int score()’
7 | int score() {
| ^~~~~
fail_func.cpp:3:5: note: ‘int score()’ previously defined here
3 | int score() {
| ^~~~~
error: redefinition of 'int score()', note 'int score()' previously defined here. The compiler does not compare the bodies and does not care that they return different values: one signature, two definitions, in one translation unit, is already the violation. This is also not overloading - two functions overload only when their parameter lists differ, and identical signatures are one function, not two candidates.
In real code this shape is usually a copy of a function edited in place while the original survived further down the file, or two genuinely different functions that were given the same name. The fix for the first case is to keep one definition - here, the first:
#include <iostream>
int score() {
return 10;
}
int main() {
std::cout << score() << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_func fix_func.cpp
$ ./fix_func
10
The output is 10, the kept body's return value. For the second case - two different jobs under one name - the repair is a rename of one function and its call sites, or a different parameter list that makes them real overloads, or moving one into a namespace so the two names are different qualified names. What never works is keeping both bodies and hoping the compiler picks one; it picks neither and stops.
The fourth failure: a class defined twice
Two definitions of Point, the second with a member the first does not have:
#include <iostream>
struct Point {
int x;
};
struct Point {
int x;
int y;
};
int main() {
Point p{1};
std::cout << p.x << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_class fail_class.cpp
fail_class.cpp:7:8: error: redefinition of ‘struct Point’
7 | struct Point {
| ^~~~~
fail_class.cpp:3:8: note: previous definition of ‘struct Point’
3 | struct Point {
| ^~~~~
error: redefinition of 'struct Point', and the note reads previous definition of 'struct Point'. A class definition is the full member list between the braces; writing it twice, even identically, defines the class twice in one translation unit. A class may be declared repeatedly - struct Point; is a declaration, not a definition - but defined once.
The split shape is the common one: the second definition is not a rival class, it is the rest of the same class - the members added later, written as a fresh definition instead of edited into the first. The fix merges them into the single definition that carries every member the program uses:
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p{1, 2};
std::cout << p.x << ' ' << p.y << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_class fix_class.cpp
$ ./fix_class
1 2
The output is 1 2, both members read from one object of the one class. When two same-named classes in one file really are different types - a geometry point and a chart point, say - merging is wrong; rename one, or put each in its own namespace, so the program has two names for two types instead of one name defined twice.
The fifth failure: an enum defined twice
Two definitions of Color, the second adding an enumerator:
#include <iostream>
enum Color {
Red,
Green
};
enum Color {
Red,
Green,
Blue
};
int main() {
std::cout << static_cast<int>(Green) << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fail_enum fail_enum.cpp
fail_enum.cpp:8:6: error: multiple definition of ‘enum Color’
8 | enum Color {
| ^~~~~
fail_enum.cpp:3:6: note: previous definition here
3 | enum Color {
| ^~~~~
Read the first line carefully, because it breaks the pattern of the last three families: error: multiple definition of 'enum Color', with the note previous definition here. GCC does not call this one a redefinition - the guide states that plainly because searching your error output for the word "redefinition" will miss this family entirely. It is still a compiler error, in one file, at the second definition: this is not the linker's "multiple definition" from the header-in-two-files case, even though the words overlap. The stage gives it away - this diagnostic carries a source line and a caret from g++; the linker's carries object file names from ld.
The fix is the class fix in enum form: one definition holding every enumerator the program uses, including the one that only existed in the second definition:
#include <iostream>
enum Color {
Red,
Green,
Blue
};
int main() {
std::cout << static_cast<int>(Red) << ' ' << static_cast<int>(Green) << ' ' << static_cast<int>(Blue) << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o fix_enum fix_enum.cpp
$ ./fix_enum
0 1 2
The output is 0 1 2: Red, Green, and Blue, in declaration order, from the single enum. As with the class, if two same-named enums are really two different types, the answer is two names - rename one, or scope one inside a class or namespace, with enum class as the form that keeps its enumerators out of the enclosing scope.
The fixes in reach order
- A local variable: keep one declaration. Make the repeat an assignment (
x = 2;) if it is the same variable, or give it a new name if both values are needed at once. - A namespace-scope variable: keep one definition. Move later values into assignments inside a function, or declare the variable
externwithout an initializer in the other places that only need to name it. - A function: keep one definition for each signature. Merge duplicate bodies, rename one function if the two do different jobs, or change a parameter list only if the two are genuinely meant to overload.
- A class or an enum: keep one definition containing every member or enumerator the program uses. If two distinct types share a name, rename one or put them in different namespaces.
- In every case: work from the note's line number to find the first definition, and decide which definition the program means before deleting anything.
Traps
- A type alias redeclared for a different type is a conflicting declaration, not a redefinition. An alias-declaration is a declaration, not a definition - per the definitions page,
using S2 = S;declares but does not define the alias - so repeating it for the same type is legal, and repeating it for a different type fails on the conflict between the two declarations:
#include <iostream>
using Count = int;
using Count = long;
int main() {
Count c = 3;
std::cout << c << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_alias_diff probe_alias_diff.cpp
probe_alias_diff.cpp:4:7: error: conflicting declaration ‘using Count = long int’
4 | using Count = long;
| ^~~~~
probe_alias_diff.cpp:3:7: note: previous declaration as ‘using Count = int’
3 | using Count = int;
| ^~~~~
conflicting declaration 'using Count = long int', and the note gives the previous declaration as using Count = int. There is no second definition to delete here; one name was promised to two different types, and the fix is to choose which type Count means - or give the second type its own alias name.
- A member function defined twice inside the class body is not called a redefinition either. This class defines get twice, with the same signature, inside one class definition:#include <iostream>
struct Meter {
int get() {
return 1;
}
int get() {
return 2;
}
};
int main() {
Meter m;
std::cout << m.get() << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_member_dup probe_member_dup.cpp
probe_member_dup.cpp:8:9: error: ‘int Meter::get()’ cannot be overloaded with ‘int Meter::get()’
8 | int get() {
| ^~~
probe_member_dup.cpp:4:9: note: previous declaration ‘int Meter::get()’
4 | int get() {
| ^~~
GCC words it as an overload failure: 'int Meter::get()' cannot be overloaded with 'int Meter::get()'. Inside a class, the two definitions are read as two declarations competing for one signature, and a function cannot overload with itself - overloading needs differing parameter lists. The repair matches the free-function family in substance: one definition per signature, so merge the two bodies, or rename one, or give one a parameter list that makes it a real overload.
- A macro redefined to a different value is a warning, not an error, and redefining it to exactly the same value is not diagnosed at all. Macros are preprocessor text, handled before the compiler sees the program, and no definition rule for C++ entities applies to them:
#include <iostream>
#define MAX 10
#define MAX 20
int main() {
std::cout << MAX << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_macro probe_macro.cpp
probe_macro.cpp:4: warning: "MAX" redefined
4 | #define MAX 20
|
probe_macro.cpp:3: note: this is the location of the previous definition
3 | #define MAX 10
|
$ ./probe_macro
20
The diagnostic is warning: "MAX" redefined, with a note naming the location of the previous definition - and the program then compiles, runs, and prints 20, the second value. The build does not stop. That does not make the shape safe: which value a use of the macro gets depends on which definition came last before the use, silently, so a redefined macro in a real build is worth removing even though the compiler lets it through - keep one #define, or #undef the name deliberately before defining it again.
- The same name in an inner block is shadowing, and it compiles. The inner value belongs to the inner block scope; the outer value belongs to the enclosing block, per the scope rules - two scopes, two variables, no second definition of anything:
#include <iostream>
int main() {
int value = 1;
{
int value = 2;
std::cout << value << '\n';
}
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o probe_shadow probe_shadow.cpp
$ ./probe_shadow
2
1
The output is 2 then 1: inside the block the inner variable is the one the name means, and after the block the outer variable is back, unchanged. Shadowing is legal and sometimes deliberate, but it is the shape most often confused with this guide's error in the other direction - code that looks like a redefinition and is not. The reverse confusion matters too: removing the braces, so both declarations land in one scope, turns this exact program into the first family.
- Declarations may repeat; definitions may not. This program declares add twice and total twice, and defines each once:
#include <iostream>
int add(int a, int b);
int add(int a, int b);
extern int total;
extern int total;
int total = 5;
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(2, 3) << ' ' << total << '\n';
return 0;
}
$ g++ -std=c++20 -g -Wall -Wextra -o boundary_decl boundary_decl.cpp
$ ./boundary_decl
5 5
The output is 5 5: the sum from the one definition of add, and the value from the one definition of total. The repeated lines are a prototype - a function declaration without a body, which the definitions page lists as a declaration that is not a definition - and extern declarations without initializers, which are likewise declarations only. This is the boundary of the error, and the practical test for any repeat you meet: ask first whether the line is one of the declaration-only forms — a function prototype with no body, an extern declaration with no initializer, a forward declaration of a class, an opaque enum declaration, or a type alias. Those may repeat. Anything else that supplies a body, an initializer, a member list, or an enumerator list is a definition, and there may be only one — and an ordinary variable declaration such as int x; is a definition too, even though it supplies no initializer.
Checklist
- The error line and the note read as a pair: the error names the second definition, the note's line number locates the first one.
- The wording identified before editing: "redeclaration" for the local variable, "redefinition" for the global variable, function, and class, "multiple definition of 'enum ...'" for the enum - and no single wording assumed for all five.
- A "multiple definition" diagnostic attributed to the right stage: a
g++diagnostic with a source line in one file is the compiler; anldline naming object files is the linker across files, with a different fix. - Exactly one definition kept per entity, with the kept one carrying every member, enumerator, or initial value the program uses.
- Each repeat converted, not just deleted blindly: an assignment for a new value, a rename for a distinct entity, a merge for a split class or enum, a namespace when two types share a name.
- An alias conflict, a member that cannot overload with itself, a macro warning, and an inner-block shadow each recognized as their own shape, not forced into the redefinition fix.
- Repeated prototypes and
externdeclarations left alone: declarations may repeat, and removing them fixes nothing. - The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- R_X86_64_PC32 Relocation Error Fix
- R_X86_64_32 Relocation Error Fix
- C++ Narrowing Conversion Error Fix
- C++ Expected Semicolon Error Fix
Sources
- cppreference, Definitions and declarations - for definitions fully defining the entity they introduce, for the declarations that are not definitions (a function declaration without a body, an
externdeclaration without an initializer, a class forward declaration, an alias-declaration), and for the One Definition Rule: only one definition of any variable, function, class type, or enumeration type in any one translation unit, with multiple declarations allowed. - cppreference, Scope - for each declaration being visible only in some scope, for block scope and the variables that belong to it, and for why the same name in an inner block is a separate entity rather than a second definition.
- cppreference, inline specifier - for an inline function or inline variable being permitted more than one definition in a program when each definition appears in a different translation unit and the definitions are identical, the repair relevant to the linker form of multiple definition.
- cppreference, Storage class specifiers - for the
externspecifier declaring a variable without defining it when no initializer is present, and for linkage as the property that lets a name be redeclared in another translation unit.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.