C++
Undefined Reference to Typeinfo Fix
The typeinfo object for a polymorphic class is emitted in exactly one place: the translation unit that defines the class's key function, alongside the vtable. If that translation unit was compiled with -fno-rtti, or the key function was never defined at all, every translation unit that names the type through typeid, dynamic_cast, or a derived class's own typeinfo is left holding a reference nothing defines.
Written November 26, 2026 · Tested with GCC 13.3.0 · 14 min read
- The error: the typeinfo alone is missing, because the key function was compiled with -fno-rtti
- The fix: compile the key function's translation unit with RTTI on
- The same missing typeinfo, reached directly: typeid and dynamiccast in the consumer
- The neighboring cause: the key function is never defined, and the vtable goes with it
Written November 26, 2026
Tested with GCC 13.3.0
What this guide is
This message is not about a missing function. typeinfo for X is a data object — the std::type_info object for a polymorphic class — and the linker is reporting that some object file refers to it and no object file defines it. That distinction decides where to look. A missing function is fixed in the file that should define the function; a missing typeinfo is fixed in the one translation unit that was supposed to emit it, which is not necessarily any of the files the error message names.
For a polymorphic class, GCC emits the typeinfo object together with the class's vtable, in the translation unit that defines the class's key function: the first non-inline, non-pure virtual function the class declares. Every other translation unit that needs the typeinfo — because it applies typeid to the class, performs a dynamic_cast that has to walk past it, or defines a derived class whose own typeinfo points at the base's — refers to that one copy. Two defects break the arrangement, and they produce visibly different errors.
The first defect is a flag mismatch. The key function's translation unit is compiled with -fno-rtti while the rest of the program is compiled with RTTI on (the default). The vtable is still emitted, because virtual dispatch does not need run-time type information; the typeinfo is not. The link then fails with the typeinfo line alone — no vtable line, no missing function. This is the form this guide leads with, because it is the one that cannot be explained by a missing definition: every function in the program is defined, and the build still fails.
The second defect is the missing key function itself. A virtual function is declared, its definition is never written — or the file containing it is left out of the link — so no translation unit emits the vtable or the typeinfo. The link fails with both lines together, alongside whatever else the missing file held. That form overlaps the plain missing-vtable error; what this guide adds is why the typeinfo line travels with it, and how to tell from the pair of lines that one absent definition explains both.
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: the typeinfo alone is missing, because the key function was compiled with -fno-rtti
Start with a small base class. Its first non-inline virtual function is key(), so key() is its key function, and the file that defines key() is where the vtable and the typeinfo for Base belong:
// base.h
#pragma once
struct Base {
virtual void key();
virtual ~Base() {}
};
// base.cpp
#include "base.h"
void Base::key() {}
The consumer defines a class derived from Base, overrides key(), calls it through a base reference, and asks typeid for the dynamic type of that reference. typeid on a glvalue of polymorphic type refers to the std::type_info object representing the dynamic type, and the derived class's own typeinfo object names the base class's typeinfo as its base — and that base entry in the derived class's typeinfo is this translation unit's one static reference to typeinfo for Base (the typeid query itself is answered at run time through the derived class's typeinfo):
// rtti_main.cpp
#include "base.h"
#include <iostream>
#include <typeinfo>
struct Derived : Base {
void key() override { std::cout << "derived key\n"; }
};
int main() {
Derived d;
Base& r = d;
r.key();
std::cout << (typeid(r) == typeid(Derived) ? "derived\n" : "other\n");
return 0;
}
Now compile the two translation units the way a mixed build compiles them — the library file with -fno-rtti, perhaps to save space in a component that never uses typeid or dynamic_cast itself, and the consumer with the default flag — and link:
$ g++ -std=c++20 -fno-rtti -c base.cpp -o base_nortti.o
$ g++ -std=c++20 -c rtti_main.cpp -o rtti_main.o
$ g++ rtti_main.o base_nortti.o -o rtti_app
/usr/bin/ld: rtti_main.o:(.data.rel.ro._ZTI7Derived[_ZTI7Derived]+0x10): undefined reference to `typeinfo for Base'
collect2: error: ld returned 1 exit status
Read the error closely, because its shape is the diagnosis. There is exactly one undefined symbol, and it is typeinfo for Base. There is no complaint about vtable for Base: the -fno-rtti object does define the vtable, since -fno-rtti disables generation of the information used by the run-time type identification features — dynamic_cast and typeid — and a vtable is not part of that information. There is also no complaint about Base::key(): it is defined, in the object being linked. The error's location line points into the typeinfo object for Derived (the mangled _ZTI7Derived), whose base-class entry is the address of typeinfo for Base — an address no object in the link supplies. GCC's own manual states this outcome directly: mixing code compiled with -frtti with code compiled with -fno-rtti may not work, and programs may fail to link if a class compiled with -fno-rtti is used as a base for a class compiled with -frtti. This program is that sentence, reproduced.
The fix: compile the key function's translation unit with RTTI on
The repair is a build repair, not a code repair: no source line changes. Recompile the file that defines the key function without -fno-rtti — RTTI is on by default, so the flag simply comes off that file — and the same object now carries all three symbols: the key function, the vtable, and the typeinfo that travels with the vtable. Relink the consumer unchanged:
$ g++ -std=c++20 -c base.cpp -o base_rtti.o
$ g++ rtti_main.o base_rtti.o -o rtti_app_fixed
$ ./rtti_app_fixed
derived key
derived
The program prints derived key and derived and exits with status 0. The first line is the virtual call dispatching to Derived::key(); the second is the typeid comparison succeeding, which is the part that needed the missing object: with typeinfo for Base present, the derived class's typeinfo is complete, and the run-time type query the consumer was written to make can be answered.
In a real build, the flag rarely sits on one command line where it can be seen. It hides in a per-target or per-directory setting: a library target built with -fno-rtti for size, linked into an application built with the default; a third-party object or archive compiled once, months ago, under different flags than the code that now derives from its classes. The search therefore starts from the class named in the error, not from the file named in the error: find which target defines that class's key function, and read that target's flags. If any class in a hierarchy is used with RTTI anywhere in the program — derived from in an RTTI translation unit, named by typeid, or crossed by a dynamic_cast — then the translation unit defining its key function must be compiled with RTTI on. The flags have to agree at the key function, specifically; aligning every other file changes nothing.
The same missing typeinfo, reached directly: typeid and dynamic_cast in the consumer
The derived-class route is not the only reference that can be left dangling. A consumer that names the base class's typeinfo directly fails the same way against the same -fno-rtti object. This program constructs a Base and prints its type name:
// typeid_main.cpp
#include "base.h"
#include <iostream>
#include <typeinfo>
int main() {
Base b;
std::cout << typeid(b).name() << "\n";
return 0;
}
$ g++ -std=c++20 -c typeid_main.cpp -o typeid_main.o
$ g++ typeid_main.o base_nortti.o -o typeid_app
/usr/bin/ld: typeid_main.o: warning: relocation against `_ZTI4Base' in read-only section `.text'
/usr/bin/ld: typeid_main.o: in function `main':
typeid_main.cpp:(.text+0x2a): undefined reference to `typeinfo for Base'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
The relocation and DT_TEXTREL warnings are side effects of the missing symbol, not separate defects; the undefined reference is the same one symbol as before, this time referred to from main itself (the mangled _ZTI4Base is typeinfo for Base). A dynamic_cast across the class fails identically: with a consumer that casts a Base* to Derived*, the link against the -fno-rtti object reports undefined reference to 'typeinfo for Base' twice — once from main, where the cast's run-time check needs the base typeinfo, and once from the typeinfo object for Derived. That is expected from what dynamic_cast does: it safely converts pointers and references up, down, and sideways along the inheritance hierarchy, and the down and sideways conversions are run-time checks against the type information of the classes involved. Relinking either consumer against the RTTI-compiled base_rtti.o fixes both: the typeid program prints 4Base — the mangled name typeid reports for Base on this implementation — and the cast program prints cast ok, each exiting with status 0.
One boundary is worth stating, because it separates this link error from a different failure with the same flag in it. Using typeid inside a translation unit that is itself compiled with -fno-rtti is not a link error at all; it is a compile error, reported at the line that uses it: error: cannot use 'typeid' with '-fno-rtti'. If the report in front of you is a link failure, the -fno-rtti is on some other file than the one using RTTI — which is precisely why the defect survives code review of the file that appears to cause it.
The neighboring cause: the key function is never defined, and the vtable goes with it
The second defect needs no flag mismatch. Declare a polymorphic class, make its first non-inline virtual function the key function, and then never define that function — or write the definition and leave its file out of the build. A second class derives from it in the consumer:
// widget.h
#pragma once
struct Widget {
virtual void draw();
virtual ~Widget() {}
};
// key_main.cpp
#include "widget.h"
#include <iostream>
#include <typeinfo>
struct ScreenWidget : Widget {
void draw() override { std::cout << "screen widget\n"; }
};
int main() {
ScreenWidget w;
Widget& r = w;
r.draw();
std::cout << (typeid(r) == typeid(ScreenWidget) ? "screen\n" : "other\n");
return 0;
}
Widget::draw() is declared and overridden, and the override is what actually runs — nothing in the consumer calls the base version. It would be natural to expect the link to succeed, or to fail only on the missing function. It fails on the two objects that were draw()'s other job to carry:
$ g++ -std=c++20 -c key_main.cpp -o key_main.o
$ g++ key_main.o -o key_app
/usr/bin/ld: key_main.o: warning: relocation against `_ZTV6Widget' in read-only section `.text._ZN6WidgetD2Ev[_ZN6WidgetD5Ev]'
/usr/bin/ld: key_main.o: in function `Widget::~Widget()':
key_main.cpp:(.text._ZN6WidgetD2Ev[_ZN6WidgetD5Ev]+0xf): undefined reference to `vtable for Widget'
/usr/bin/ld: key_main.o:(.data.rel.ro._ZTI12ScreenWidget[_ZTI12ScreenWidget]+0x10): undefined reference to `typeinfo for Widget'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
The pair of undefined references is the signature of this cause, and it is what separates this guide's error from the flag mismatch above. The vtable line (the mangled _ZTV6Widget) is referred to from the inline destructor the consumer instantiated; the typeinfo line is referred to from the typeinfo object for ScreenWidget, which names its base's typeinfo. Both objects live in the translation unit that defines the key function, and there is no such translation unit in the link, so both are missing together. Notably absent is any undefined reference to Widget::draw() itself: the only call to draw() in the program is a virtual call to the override, so the missing definition is never named by the linker at all. A report that says "the function is overridden, so it cannot be missing" has the mechanism backwards — the override is why the function's own symbol is not missed, and the key function role is why its absence still breaks the link twice over.
The fix for the missing key function: define it, and link its file
Give the key function a definition — an empty body is enough when the base class genuinely has nothing to do — in its own file, and link that file:
// widget.cpp
#include "widget.h"
void Widget::draw() {}
$ g++ -std=c++20 -c widget.cpp -o widget.o
$ g++ -std=c++20 key_main.cpp widget.cpp -o key_app_fixed
$ ./key_app_fixed
screen widget
screen
The program prints screen widget and screen and exits with status 0. The object file for widget.cpp defines typeinfo for Widget, typeinfo name for Widget, and vtable for Widget together — the three arrive as a set, from the one definition. Two practical notes follow from that. First, if the definition already exists in the project, the defect is in the build, not the source: the file is missing from the target's source list, and adding the definition a second time somewhere else trades this error for a duplicate-definition problem. Second, making the key function inline in the class body also removes the error, by removing the key function role — a class whose virtual functions are all inline has its vtable and typeinfo emitted wherever they are needed — but it changes the class's structure to fix a build defect, and it forfeits the control over where those objects live that the key function exists to provide. Defining the declared function, in the file the build already expects, is the smaller repair.
An alternative alignment: no RTTI anywhere, and no RTTI use anywhere
The flag mismatch has a second resolution, available only when nothing in the program actually needs run-time type information. If every translation unit is compiled with -fno-rtti and no translation unit uses typeid or a run-time-checked dynamic_cast, the program links and runs without any typeinfo for the base class at all. The consumer from the first shape, with the typeid line removed, builds against the same -fno-rtti base object and prints derived key, exiting with status 0: the virtual call works, because dispatch never needed the typeinfo. This is a legitimate configuration — it is the configuration -fno-rtti exists for — but it is an all-or-nothing one. The moment one file needs a type query or a checked downcast, that file must be compiled with RTTI on, and from that moment the key function rule in the fix above binds every base class it touches. Choosing this resolution therefore means auditing the whole program for RTTI use, not just the file in the error message; a partial version of it is the defect this guide started with.
The exact rule: one translation unit emits the typeinfo, and RTTI decides whether it is in it
GCC's documentation states the placement rule in two parts. On vague linkage: if a class declares any non-inline, non-pure virtual functions, the first one is chosen as the key method for the class, and the vtable is only emitted in the translation unit where the key method is defined; and for polymorphic classes, the type_info object is written out along with the vtable, so that dynamic_cast can determine the dynamic type of a class object at run time. On the flag, the C++ dialect options for GCC 13.3.0: -fno-rtti disables generation of information about every class with virtual functions for use by the run-time type identification features, dynamic_cast and typeid, and mixing code compiled with -frtti with code compiled with -fno-rtti may not work — programs may fail to link if a class compiled with -fno-rtti is used as a base for a class compiled with -frtti.
Put together, the two statements predict both shapes in this guide exactly. The typeinfo has one home — the key function's translation unit, with the vtable. Compile that home with -fno-rtti and the vtable is emitted while the typeinfo is withheld, so consumers fail on the typeinfo alone. Give the class no key function definition anywhere and neither object is emitted, so consumers fail on both together. And the references on the consumer side are the ordinary ones: typeid applied to a glvalue of polymorphic type refers to the std::type_info object representing the dynamic type, and dynamic_cast converts pointers and references along the inheritance hierarchy with a run-time check for the down and sideways cases — a check that has to read the type information of the classes it crosses. None of these references is optional or lazy: a derived class's typeinfo names its base's typeinfo whether or not the program ever writes typeid, which is why a program can hit this error in code that contains no RTTI syntax at all.
The fixes, ranked
- First, read the error's shape. Typeinfo missing alone points at a flag mismatch on the key function's translation unit; typeinfo and vtable missing together points at a key function that is never defined, or whose file is not in the link.
- Second, for the mismatch: find the target that defines the named class's key function and compile it with RTTI on — remove
-fno-rttifrom that target — then rebuild it and relink. No source change is needed, and changing the consumer's flags does not fix it. - Third, for the missing key function: define the declared function (an empty body is a complete definition) in its file, and confirm that file is in the target's source list. If the definition already exists, fix the source list, not the code.
- Fourth, if the program genuinely uses no run-time type information anywhere, the alternative alignment is available: compile every translation unit with
-fno-rttiand remove everytypeidand checkeddynamic_cast. Partial alignment is the defect, in either direction. - Fifth, do not chase the functions the error does not name. In the missing-key-function shape there is no undefined reference to the key function itself, and in the mismatch shape every function is defined; adding definitions, stubs, or explicit typeinfo objects treats a placement and flags problem as a missing-code problem.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
Key function's translation unit compiled with -fno-rtti, class used as a base in an RTTI translation unit |
undefined reference to 'typeinfo for Base' only; link exit status 1 |
Recompile the key function's file with RTTI on (remove -fno-rtti from that target) and relink |
Same mismatch, consumer names the typeinfo directly with typeid or crosses the class with dynamic_cast |
undefined reference to 'typeinfo for Base' — from main for the typeid consumer; from main and from the derived class's typeinfo for the dynamic_cast consumer; link exit status 1 |
Same fix; the reference site differs, the missing object and its home do not |
| Key function declared but never defined, or its file left out of the link | undefined reference to 'vtable for Widget' and undefined reference to 'typeinfo for Widget' together; link exit status 1 |
Define the key function and link its file; if it is already defined, add its file to the target |
typeid or a checked dynamic_cast used inside a -fno-rtti translation unit |
Compile error cannot use 'typeid' with '-fno-rtti' at the use site, not a link error |
Compile that translation unit with RTTI on, or remove the RTTI use from it |
| No RTTI use anywhere in the program | No error, under either consistent flag choice | Either alignment works; keep it consistent across every target that shares a class |
| ## Checklist |
- Classify the error before searching: count the undefined symbols. One typeinfo line is a flags problem; a typeinfo line paired with a vtable line is a missing-definition problem.
- Name the class in the message, then find its key function: the first non-inline, non-pure virtual function it declares. The translation unit that defines that function is where the missing object was supposed to come from.
- Read that translation unit's target flags, not the flags of the file the linker named. A per-target or per-directory
-fno-rttion a library is invisible from the consumer's build settings. - Remember that a derived class refers to its base's typeinfo even when no
typeidordynamic_castappears anywhere in the program; "we do not use RTTI" is a claim about the whole program, including its class hierarchies. - If the key function has no definition, write one — an empty body counts — before suspecting flags; if it has one, check the target's source list before writing anything.
- After the fix, confirm the program does the run-time work the error was blocking: the fixed programs here print the derived class's behavior and answer the
typeidcomparison with the derived type. - Keep the flags consistent as the build evolves: adding one RTTI-using file, or one derived class in an RTTI file, is enough to turn a working all-
-fno-rttibuild into this link error.
Related guides on this site
- Undefined Reference to Static Member Fix
- C++ Undefined Reference to Main Fix
- C++ Undefined Reference to Function Fix
- Fix "Undefined Reference to Vtable" — the Missing Virtual Function
Sources
- GCC, C++ Dialect Options (GCC 13.3.0) — for
-fno-rttidisabling generation of information about every class with virtual functions for use by the run-time type identification features (dynamic_castandtypeid), and for mixing code compiled with-frttiwith code compiled with-fno-rttipossibly failing to link if a class compiled with-fno-rttiis used as a base for a class compiled with-frtti. - GCC, Vague Linkage — for the first non-inline, non-pure virtual function being chosen as the key method, for the vtable being emitted only in the translation unit where the key method is defined, and for the
type_infoobject of a polymorphic class being written out along with the vtable sodynamic_castcan determine the dynamic type at run time. - cppreference, typeid operator — for the
typeidexpression referring to astd::type_infoobject, and fortypeidapplied to a glvalue of polymorphic type referring to thestd::type_infoobject representing the dynamic type of the expression. - cppreference, dynamic_cast conversion — for
dynamic_castsafely converting pointers and references to classes up, down, and sideways along the inheritance hierarchy, with a run-time check for the conversions that move down or sideways. - cppreference, Virtual function specifier — for a class with virtual functions being polymorphic, the property every class in this guide's examples relies on for its typeinfo to exist at all.
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.