xref: /llvm-project/lld/docs/missingkeyfunction.rst (revision 136a6a612a564516ac50505e961f4fc26b36b373)
1ccebc7e3SHans WennborgMissing Key Function
2ccebc7e3SHans Wennborg====================
3f747971bSFangrui Song
4f747971bSFangrui SongIf your build failed with a linker error something like this::
5f747971bSFangrui Song
6f747971bSFangrui Song  foo.cc:28: error: undefined reference to 'vtable for C'
75945ad5cSRui Ueyama  the vtable symbol may be undefined because the class is missing its key function
85945ad5cSRui Ueyama  (see https://lld.llvm.org/missingkeyfunction)
9f747971bSFangrui Song
10f747971bSFangrui Songit's likely that your class C has a key function (defined by the ABI as the first
1181c0880cSRui Ueyamanon-pure, non-inline, virtual function), but you haven't actually defined it.
12f747971bSFangrui Song
13f747971bSFangrui SongWhen a class has a key function, the compiler emits the vtable (and some other
14f747971bSFangrui Songthings as well) only in the translation unit that defines that key function. Thus,
15f747971bSFangrui Songif you're missing the key function, you'll also be missing the vtable. If no other
1681c0880cSRui Ueyamafunction calls your missing function, you won't see any undefined reference errors
17f747971bSFangrui Songfor it, but you will see undefined references to the vtable symbol.
18f747971bSFangrui Song
1981c0880cSRui UeyamaWhen a class has no non-pure, non-inline, virtual functions, there is no key
2081c0880cSRui Ueyamafunction, and the compiler is forced to emit the vtable in every translation unit
21f747971bSFangrui Songthat references the class. In this case, it is emitted in a COMDAT section,
22f747971bSFangrui Songwhich allows the linker to eliminate all duplicate copies. This is still
23f747971bSFangrui Songwasteful in terms of object file size and link time, so it's always advisable to
2481c0880cSRui Ueyamaensure there is at least one eligible function that can serve as the key function.
25f747971bSFangrui Song
26f747971bSFangrui SongHere are the most common mistakes that lead to this error:
27f747971bSFangrui Song
28f747971bSFangrui SongFailing to define a virtual destructor
29f747971bSFangrui Song--------------------------------------
30f747971bSFangrui Song
31f747971bSFangrui SongSay you have a base class declared in a header file::
32f747971bSFangrui Song
33f747971bSFangrui Song  class B {
34f747971bSFangrui Song  public:
35f747971bSFangrui Song    B();
36f747971bSFangrui Song    virtual ~B();
37f747971bSFangrui Song    ...
38f747971bSFangrui Song  };
39f747971bSFangrui Song
4081c0880cSRui UeyamaHere, ``~B`` is the first non-pure, non-inline, virtual function, so it is the key
4181c0880cSRui Ueyamafunction. If you forget to define ``B::~B`` in your source file, the compiler will
42f747971bSFangrui Songnot emit the vtable for ``B``, and you'll get an undefined reference to "vtable
43f747971bSFangrui Songfor B".
44f747971bSFangrui Song
45f747971bSFangrui SongThis is just an example of the more general mistake of forgetting to define the
46f747971bSFangrui Songkey function, but it's quite common because virtual destructors are likely to be
47f747971bSFangrui Songthe first eligible key function and it's easy to forget to implement them. It's
48f747971bSFangrui Songalso more likely that you won't have any direct references to the destructor, so
49f747971bSFangrui Songyou won't see any undefined reference errors that point directly to the problem.
50f747971bSFangrui Song
5181c0880cSRui UeyamaThe solution in this case is to implement the missing function.
52f747971bSFangrui Song
5381c0880cSRui UeyamaForgetting to declare a virtual function in an abstract class as pure
54*136a6a61SHans Wennborg---------------------------------------------------------------------
55f747971bSFangrui Song
56f747971bSFangrui SongSay you have an abstract base class declared in a header file::
57f747971bSFangrui Song
58f747971bSFangrui Song  class A {
59f747971bSFangrui Song  public:
60f747971bSFangrui Song    A();
61f747971bSFangrui Song    virtual ~A() {}
62f747971bSFangrui Song    virtual int foo() = 0;
63f747971bSFangrui Song    ...
64f747971bSFangrui Song    virtual int bar();
65f747971bSFangrui Song    ...
66f747971bSFangrui Song  };
67f747971bSFangrui Song
68f747971bSFangrui SongThis base class is intended to be abstract, but you forgot to mark one of the
6981c0880cSRui Ueyamafunctions pure. Here, ``A::bar``, being non-pure, is nominated as the key function,
70f747971bSFangrui Songand as a result, the vtable for ``A`` is not emitted, because the compiler is
71f747971bSFangrui Songwaiting for a translation unit that defines ``A::bar``.
72f747971bSFangrui Song
73f747971bSFangrui SongThe solution in this case is to add the missing ``= 0`` to the declaration of
74f747971bSFangrui Song``A::bar``.
75f747971bSFangrui Song
7681c0880cSRui UeyamaKey function is defined, but the linker doesn't see it
77*136a6a61SHans Wennborg------------------------------------------------------
78f747971bSFangrui Song
79f747971bSFangrui SongIt's also possible that you have defined the key function somewhere, but the
8081c0880cSRui Ueyamaobject file containing the definition of that function isn't being linked into
81f747971bSFangrui Songyour application.
82f747971bSFangrui Song
83f747971bSFangrui SongThe solution in this case is to check your dependencies to make sure that
84f747971bSFangrui Songthe object file or the library file containing the key function is given to
85f747971bSFangrui Songthe linker.
86