1*b5893f02SDimitry AndricMissing Key Function
2*b5893f02SDimitry Andric====================
3*b5893f02SDimitry Andric
4*b5893f02SDimitry AndricIf your build failed with a linker error something like this::
5*b5893f02SDimitry Andric
6*b5893f02SDimitry Andric  foo.cc:28: error: undefined reference to 'vtable for C'
7*b5893f02SDimitry Andric  the vtable symbol may be undefined because the class is missing its key function (see https://lld.llvm.org/missingkeyfunction)
8*b5893f02SDimitry Andric
9*b5893f02SDimitry Andricit's likely that your class C has a key function (defined by the ABI as the first
10*b5893f02SDimitry Andricnon-pure, non-inline, virtual method), but you haven't actually defined it.
11*b5893f02SDimitry Andric
12*b5893f02SDimitry AndricWhen a class has a key function, the compiler emits the vtable (and some other
13*b5893f02SDimitry Andricthings as well) only in the translation unit that defines that key function. Thus,
14*b5893f02SDimitry Andricif you're missing the key function, you'll also be missing the vtable. If no other
15*b5893f02SDimitry Andricfunction calls your missing method, you won't see any undefined reference errors
16*b5893f02SDimitry Andricfor it, but you will see undefined references to the vtable symbol.
17*b5893f02SDimitry Andric
18*b5893f02SDimitry AndricWhen a class has no non-pure, non-inline, virtual methods, there is no key
19*b5893f02SDimitry Andricmethod, and the compiler is forced to emit the vtable in every translation unit
20*b5893f02SDimitry Andricthat references the class. In this case, it is emitted in a COMDAT section,
21*b5893f02SDimitry Andricwhich allows the linker to eliminate all duplicate copies. This is still
22*b5893f02SDimitry Andricwasteful in terms of object file size and link time, so it's always advisable to
23*b5893f02SDimitry Andricensure there is at least one eligible method that can serve as the key function.
24*b5893f02SDimitry Andric
25*b5893f02SDimitry AndricHere are the most common mistakes that lead to this error:
26*b5893f02SDimitry Andric
27*b5893f02SDimitry AndricFailing to define a virtual destructor
28*b5893f02SDimitry Andric--------------------------------------
29*b5893f02SDimitry Andric
30*b5893f02SDimitry AndricSay you have a base class declared in a header file::
31*b5893f02SDimitry Andric
32*b5893f02SDimitry Andric  class B {
33*b5893f02SDimitry Andric  public:
34*b5893f02SDimitry Andric    B();
35*b5893f02SDimitry Andric    virtual ~B();
36*b5893f02SDimitry Andric    ...
37*b5893f02SDimitry Andric  };
38*b5893f02SDimitry Andric
39*b5893f02SDimitry AndricHere, ``~B`` is the first non-pure, non-inline, virtual method, so it is the key
40*b5893f02SDimitry Andricmethod. If you forget to define ``B::~B`` in your source file, the compiler will
41*b5893f02SDimitry Andricnot emit the vtable for ``B``, and you'll get an undefined reference to "vtable
42*b5893f02SDimitry Andricfor B".
43*b5893f02SDimitry Andric
44*b5893f02SDimitry AndricThis is just an example of the more general mistake of forgetting to define the
45*b5893f02SDimitry Andrickey function, but it's quite common because virtual destructors are likely to be
46*b5893f02SDimitry Andricthe first eligible key function and it's easy to forget to implement them. It's
47*b5893f02SDimitry Andricalso more likely that you won't have any direct references to the destructor, so
48*b5893f02SDimitry Andricyou won't see any undefined reference errors that point directly to the problem.
49*b5893f02SDimitry Andric
50*b5893f02SDimitry AndricThe solution in this case is to implement the missing method.
51*b5893f02SDimitry Andric
52*b5893f02SDimitry AndricForgetting to declare a virtual method in an abstract class as pure
53*b5893f02SDimitry Andric-------------------------------------------------------------------
54*b5893f02SDimitry Andric
55*b5893f02SDimitry AndricSay you have an abstract base class declared in a header file::
56*b5893f02SDimitry Andric
57*b5893f02SDimitry Andric  class A {
58*b5893f02SDimitry Andric  public:
59*b5893f02SDimitry Andric    A();
60*b5893f02SDimitry Andric    virtual ~A() {}
61*b5893f02SDimitry Andric    virtual int foo() = 0;
62*b5893f02SDimitry Andric    ...
63*b5893f02SDimitry Andric    virtual int bar();
64*b5893f02SDimitry Andric    ...
65*b5893f02SDimitry Andric  };
66*b5893f02SDimitry Andric
67*b5893f02SDimitry AndricThis base class is intended to be abstract, but you forgot to mark one of the
68*b5893f02SDimitry Andricmethods pure. Here, ``A::bar``, being non-pure, is nominated as the key function,
69*b5893f02SDimitry Andricand as a result, the vtable for ``A`` is not emitted, because the compiler is
70*b5893f02SDimitry Andricwaiting for a translation unit that defines ``A::bar``.
71*b5893f02SDimitry Andric
72*b5893f02SDimitry AndricThe solution in this case is to add the missing ``= 0`` to the declaration of
73*b5893f02SDimitry Andric``A::bar``.
74*b5893f02SDimitry Andric
75*b5893f02SDimitry AndricKey method is defined, but the linker doesn't see it
76*b5893f02SDimitry Andric----------------------------------------------------
77*b5893f02SDimitry Andric
78*b5893f02SDimitry AndricIt's also possible that you have defined the key function somewhere, but the
79*b5893f02SDimitry Andricobject file containing the definition of that method isn't being linked into
80*b5893f02SDimitry Andricyour application.
81*b5893f02SDimitry Andric
82*b5893f02SDimitry AndricThe solution in this case is to check your dependencies to make sure that
83*b5893f02SDimitry Andricthe object file or the library file containing the key function is given to
84*b5893f02SDimitry Andricthe linker.
85