C++: undefined reference to `vtable for X'
This is not a missing call to the named function. The compiler emits a class's vtable alongside the definition of its first non inline virtual function, so if that function is declared and never defined, or its translation unit is missing from the link, the vtable is never emitted. A virtual destructor declared in the header and never written is the classic case.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
// Declared, never defined: the vtable has nowhere to be emitted
class Shape {
public:
virtual ~Shape(); // needs a definition somewhere
virtual void draw() = 0; // pure virtual is fine, no definition needed
};
// Either define it in the .cpp
Shape::~Shape() = default;
// Or define it inline in the header
virtual ~Shape() = default;
# Or the definition exists but the object file was never linked
nm -C build/shape.o | grep -i vtable
ls build/CMakeFiles/app.dir/*.o
# CMake: the source is missing from the target
# add_executable(app main.cpp shape.cpp)
# Qt classes need moc output linked in too: rerun the generator
cmake --build build --clean-first
How to diagnose C++ errors
C++ errors fall into two very different worlds. Compile and link errors (undefined reference, multiple definition, template substitution failure) are deterministic and are almost always about declarations, the One Definition Rule, or link order. Runtime memory errors (segfaults, double free, use-after-free) are non-deterministic and should never be debugged by reading code alone; a sanitizer will find in seconds what code review misses for days.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Rebuild with sanitizers before anything else:
-fsanitize=address,undefined -fno-omit-frame-pointer -g. AddressSanitizer reports the exact allocation and free sites for use-after-free and double-free. - For undefined-reference errors, check link order, remembering that with GNU ld libraries must come after the objects that use them, and check for a C/C++ linkage mismatch that needs
extern "C". - Use
nm -C libfoo.a | grep symbolto confirm the symbol is actually present and to see the demangled signature. A signature that differs by aconstis a different symbol. - Enable core dumps (
ulimit -c unlimited) and open them in gdb withgdb ./binary core, thenbt full. A stack trace beats speculation. - For template errors, read the message from the bottom up. The final line is usually the real constraint that failed; everything above is instantiation context.
Tools worth reaching for
-fsanitize=address,undefinedvalgrindgdb / lldbnm -Cldd
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related C++ errors
- C++: relocation R_X86_64_32S can not be used when making a shared objectA shared library must be loadable at any address, so every object in it needs position…
- C++: runtime error: signed integer overflow (UBSan)Signed integer overflow is undefined behaviour in C and C++. The program may appear to work…
- C++: Segmentation fault (core dumped)Program attempted to access memory it doesn't own (e.g., dereferencing null pointer, buffer…
- CMake Error: Could not find a package configuration file provided by Xfind_package looked for a config file the library installs, XConfig.cmake or x-config.cmake…
- double free or corruptionHeap memory was freed twice, corrupting the memory allocator. Often caused by manual delete…
- heap-use-after-freeAccessing memory after it has been freed. Detected by AddressSanitizer. Causes undefined…
- multiple definition of 'symbol'The same symbol is defined in multiple translation units. Usually caused by defining…
- stack-buffer-overflowA write exceeded the bounds of a stack-allocated buffer. Detected by AddressSanitizer or…
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.