xref: /openbsd-src/gnu/llvm/lld/docs/missingkeyfunction.rst (revision ece8a53058b183aa0a669e21c57225431117089d)
1*ece8a530SpatrickMissing Key Function
2*ece8a530Spatrick====================
3*ece8a530Spatrick
4*ece8a530SpatrickIf your build failed with a linker error something like this::
5*ece8a530Spatrick
6*ece8a530Spatrick  foo.cc:28: error: undefined reference to 'vtable for C'
7*ece8a530Spatrick  the vtable symbol may be undefined because the class is missing its key function
8*ece8a530Spatrick  (see https://lld.llvm.org/missingkeyfunction)
9*ece8a530Spatrick
10*ece8a530Spatrickit's likely that your class C has a key function (defined by the ABI as the first
11*ece8a530Spatricknon-pure, non-inline, virtual function), but you haven't actually defined it.
12*ece8a530Spatrick
13*ece8a530SpatrickWhen a class has a key function, the compiler emits the vtable (and some other
14*ece8a530Spatrickthings as well) only in the translation unit that defines that key function. Thus,
15*ece8a530Spatrickif you're missing the key function, you'll also be missing the vtable. If no other
16*ece8a530Spatrickfunction calls your missing function, you won't see any undefined reference errors
17*ece8a530Spatrickfor it, but you will see undefined references to the vtable symbol.
18*ece8a530Spatrick
19*ece8a530SpatrickWhen a class has no non-pure, non-inline, virtual functions, there is no key
20*ece8a530Spatrickfunction, and the compiler is forced to emit the vtable in every translation unit
21*ece8a530Spatrickthat references the class. In this case, it is emitted in a COMDAT section,
22*ece8a530Spatrickwhich allows the linker to eliminate all duplicate copies. This is still
23*ece8a530Spatrickwasteful in terms of object file size and link time, so it's always advisable to
24*ece8a530Spatrickensure there is at least one eligible function that can serve as the key function.
25*ece8a530Spatrick
26*ece8a530SpatrickHere are the most common mistakes that lead to this error:
27*ece8a530Spatrick
28*ece8a530SpatrickFailing to define a virtual destructor
29*ece8a530Spatrick--------------------------------------
30*ece8a530Spatrick
31*ece8a530SpatrickSay you have a base class declared in a header file::
32*ece8a530Spatrick
33*ece8a530Spatrick  class B {
34*ece8a530Spatrick  public:
35*ece8a530Spatrick    B();
36*ece8a530Spatrick    virtual ~B();
37*ece8a530Spatrick    ...
38*ece8a530Spatrick  };
39*ece8a530Spatrick
40*ece8a530SpatrickHere, ``~B`` is the first non-pure, non-inline, virtual function, so it is the key
41*ece8a530Spatrickfunction. If you forget to define ``B::~B`` in your source file, the compiler will
42*ece8a530Spatricknot emit the vtable for ``B``, and you'll get an undefined reference to "vtable
43*ece8a530Spatrickfor B".
44*ece8a530Spatrick
45*ece8a530SpatrickThis is just an example of the more general mistake of forgetting to define the
46*ece8a530Spatrickkey function, but it's quite common because virtual destructors are likely to be
47*ece8a530Spatrickthe first eligible key function and it's easy to forget to implement them. It's
48*ece8a530Spatrickalso more likely that you won't have any direct references to the destructor, so
49*ece8a530Spatrickyou won't see any undefined reference errors that point directly to the problem.
50*ece8a530Spatrick
51*ece8a530SpatrickThe solution in this case is to implement the missing function.
52*ece8a530Spatrick
53*ece8a530SpatrickForgetting to declare a virtual function in an abstract class as pure
54*ece8a530Spatrick---------------------------------------------------------------------
55*ece8a530Spatrick
56*ece8a530SpatrickSay you have an abstract base class declared in a header file::
57*ece8a530Spatrick
58*ece8a530Spatrick  class A {
59*ece8a530Spatrick  public:
60*ece8a530Spatrick    A();
61*ece8a530Spatrick    virtual ~A() {}
62*ece8a530Spatrick    virtual int foo() = 0;
63*ece8a530Spatrick    ...
64*ece8a530Spatrick    virtual int bar();
65*ece8a530Spatrick    ...
66*ece8a530Spatrick  };
67*ece8a530Spatrick
68*ece8a530SpatrickThis base class is intended to be abstract, but you forgot to mark one of the
69*ece8a530Spatrickfunctions pure. Here, ``A::bar``, being non-pure, is nominated as the key function,
70*ece8a530Spatrickand as a result, the vtable for ``A`` is not emitted, because the compiler is
71*ece8a530Spatrickwaiting for a translation unit that defines ``A::bar``.
72*ece8a530Spatrick
73*ece8a530SpatrickThe solution in this case is to add the missing ``= 0`` to the declaration of
74*ece8a530Spatrick``A::bar``.
75*ece8a530Spatrick
76*ece8a530SpatrickKey function is defined, but the linker doesn't see it
77*ece8a530Spatrick------------------------------------------------------
78*ece8a530Spatrick
79*ece8a530SpatrickIt's also possible that you have defined the key function somewhere, but the
80*ece8a530Spatrickobject file containing the definition of that function isn't being linked into
81*ece8a530Spatrickyour application.
82*ece8a530Spatrick
83*ece8a530SpatrickThe solution in this case is to check your dependencies to make sure that
84*ece8a530Spatrickthe object file or the library file containing the key function is given to
85*ece8a530Spatrickthe linker.
86