undefined reference to vtable C/C++ Origin: Application & API Last updated 15 Oct 2026

C++: undefined reference to `vtable for X' — virtual function not defined

The linker cannot find the class's virtual table, which means at least one virtual member function was declared but never defined (or the file defining it isn't linked).

Exact message you may see undefined reference to `vtable forundefined reference to `typeinfo forvtable for X not definedUndefined symbols: vtable forundefined reference to `typeinfo name for
Seen on: C/C++ GCC Clang

Meaning

The compiler emits the vtable in the file that defines the class's first non-inline virtual function (the "key function"). If that function has no body, or its .cpp is missing from the build, you get undefined reference to vtable for X and often typeinfo for X too.

Common causes

  • Virtual function declared but never defined
  • The .cpp containing the key function is not part of the target
  • Pure virtual function called through a base that has no definition
  • Missing virtual destructor body (virtual ~X(); never defined)

⚡ Quick fix

  1. Define every non-pure virtual function (or = default)
  2. Add the missing .cpp to the build
  3. Define the destructor: virtual ~X() = default;

Detailed fix by platform

C++

  1. Write virtual ~Base() = default; in the header
  2. Check that every override has a body or = 0

CMake

  1. Re-run CMake after adding files; globbed sources are not detected automatically: cmake -S . -B build

Code examples

Key function missing

cpp
// widget.h
struct Widget {
    virtual void draw();               // declared ...
    virtual ~Widget() = default;
};

// widget.cpp
#include "widget.h"
void Widget::draw() {}                 // ... and defined here, so the vtable exists

How to diagnose

  1. Class — Which class is in the message?
  2. Definitions — Does each virtual function have a body?
  3. Build — Is its .cpp compiled and linked?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…LNK1104 — MSVC: fatal error LNK1104: cannot open file 'foo.lib' (or 'app.exe' while it is running) (C/C++)LNK1104multiple definition of — C/C++: multiple definition of `x'; first defined here — ld returned 1 exit status (C/C++)multiple definition ofpure virtual method called — C++: pure virtual method called — terminate called without an active exception (C/C++)pure virtual method c…undefined reference to — C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status (C/C++)undefined reference toundefined reference to main — C/C++: undefined reference to `main' — (.text+0x20) in crt1.o (C/C++)undefined reference t…undefined reference to pthread_create — C/C++: undefined reference to `pthread_create' — missing -pthread (also `dlopen`, `clock_gettime`) (C/C++)undefined reference t…undefined…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.