| Hans Wennborg | ccebc7e | 2019-02-07 12:39:35 +0000 | [diff] [blame] | 1 | Missing Key Function |
| 2 | ==================== |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 3 | |
| 4 | If your build failed with a linker error something like this:: |
| 5 | |
| 6 | foo.cc:28: error: undefined reference to 'vtable for C' |
| Rui Ueyama | 5945ad5 | 2019-02-23 00:24:18 +0000 | [diff] [blame] | 7 | the vtable symbol may be undefined because the class is missing its key function |
| 8 | (see https://lld.llvm.org/missingkeyfunction) |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 9 | |
| Fangrui Song | f874871 | 2018-12-21 22:40:10 +0000 | [diff] [blame] | 10 | it's likely that your class C has a key function (defined by the ABI as the first |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 11 | non-pure, non-inline, virtual function), but you haven't actually defined it. |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 12 | |
| Fangrui Song | f874871 | 2018-12-21 22:40:10 +0000 | [diff] [blame] | 13 | When a class has a key function, the compiler emits the vtable (and some other |
| 14 | things as well) only in the translation unit that defines that key function. Thus, |
| 15 | if you're missing the key function, you'll also be missing the vtable. If no other |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 16 | function calls your missing function, you won't see any undefined reference errors |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 17 | for it, but you will see undefined references to the vtable symbol. |
| 18 | |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 19 | When a class has no non-pure, non-inline, virtual functions, there is no key |
| 20 | function, and the compiler is forced to emit the vtable in every translation unit |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 21 | that references the class. In this case, it is emitted in a COMDAT section, |
| 22 | which allows the linker to eliminate all duplicate copies. This is still |
| 23 | wasteful in terms of object file size and link time, so it's always advisable to |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 24 | ensure there is at least one eligible function that can serve as the key function. |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 25 | |
| 26 | Here are the most common mistakes that lead to this error: |
| 27 | |
| 28 | Failing to define a virtual destructor |
| 29 | -------------------------------------- |
| 30 | |
| 31 | Say you have a base class declared in a header file:: |
| 32 | |
| 33 | class B { |
| 34 | public: |
| 35 | B(); |
| 36 | virtual ~B(); |
| 37 | ... |
| 38 | }; |
| 39 | |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 40 | Here, ``~B`` is the first non-pure, non-inline, virtual function, so it is the key |
| 41 | function. If you forget to define ``B::~B`` in your source file, the compiler will |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 42 | not emit the vtable for ``B``, and you'll get an undefined reference to "vtable |
| 43 | for B". |
| 44 | |
| 45 | This is just an example of the more general mistake of forgetting to define the |
| Fangrui Song | f874871 | 2018-12-21 22:40:10 +0000 | [diff] [blame] | 46 | key function, but it's quite common because virtual destructors are likely to be |
| 47 | the first eligible key function and it's easy to forget to implement them. It's |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 48 | also more likely that you won't have any direct references to the destructor, so |
| 49 | you won't see any undefined reference errors that point directly to the problem. |
| 50 | |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 51 | The solution in this case is to implement the missing function. |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 52 | |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 53 | Forgetting to declare a virtual function in an abstract class as pure |
| Hans Wennborg | 136a6a6 | 2019-04-11 07:31:03 +0000 | [diff] [blame] | 54 | --------------------------------------------------------------------- |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 55 | |
| 56 | Say you have an abstract base class declared in a header file:: |
| 57 | |
| 58 | class A { |
| 59 | public: |
| 60 | A(); |
| 61 | virtual ~A() {} |
| 62 | virtual int foo() = 0; |
| 63 | ... |
| 64 | virtual int bar(); |
| 65 | ... |
| 66 | }; |
| 67 | |
| 68 | This base class is intended to be abstract, but you forgot to mark one of the |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 69 | functions pure. Here, ``A::bar``, being non-pure, is nominated as the key function, |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 70 | and as a result, the vtable for ``A`` is not emitted, because the compiler is |
| 71 | waiting for a translation unit that defines ``A::bar``. |
| 72 | |
| 73 | The solution in this case is to add the missing ``= 0`` to the declaration of |
| 74 | ``A::bar``. |
| 75 | |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 76 | Key function is defined, but the linker doesn't see it |
| Hans Wennborg | 136a6a6 | 2019-04-11 07:31:03 +0000 | [diff] [blame] | 77 | ------------------------------------------------------ |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 78 | |
| Fangrui Song | f874871 | 2018-12-21 22:40:10 +0000 | [diff] [blame] | 79 | It's also possible that you have defined the key function somewhere, but the |
| Rui Ueyama | 81c0880 | 2019-02-22 23:59:51 +0000 | [diff] [blame] | 80 | object file containing the definition of that function isn't being linked into |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 81 | your application. |
| 82 | |
| 83 | The solution in this case is to check your dependencies to make sure that |
| Fangrui Song | f874871 | 2018-12-21 22:40:10 +0000 | [diff] [blame] | 84 | the object file or the library file containing the key function is given to |
| Rui Ueyama | afe9673 | 2018-12-21 19:28:49 +0000 | [diff] [blame] | 85 | the linker. |