首页 > 解决方案 > 未定义符号:SomeClass 的 vtable

问题描述

刚刚遇到这 2 个 Clang 错误:

ld.lld: error: undefined symbol: vtable for HashFn
>>> referenced by hashFn.h:40 ......
HashFn::HashFn(int, bool)

ld.lld: error: undefined symbol: vtable for HashFn
>>> referenced by hashFn.h:36 ......
HashFn::HashFn(HashFn const&)

hashFn.h -->

#ifndef HASHFN_H_
#define HASHFN_H_

#include "./base.h"

typedef uint64_t uint64Array[30];
static int precomputedArraySize = sizeof(uint64Array) / sizeof(uint64_t);

inline uint64_t customPow(uint64Array *precomputedPowers, bool usePrecomputed,
    uint64_t base, int exp) {
  if (usePrecomputed && exp < precomputedArraySize) {
    return (*precomputedPowers)[exp];
  }

  // TOOD: Optimization possible here when passed in toSize which is bigger
  // than precomputedArraySize, we can start from the value of the last
  // precomputed value.
  uint64_t result = 1;
  while (exp) {
    if (exp & 1)
      result *= base;
    exp >>= 1;
    base *= base;
  }
  return result;
}


// Functor for a hashing function
// Implements a Rabin fingerprint hash function
class HashFn {
 public:
  // Initialize a HashFn with the prime p which is used as the base of the Rabin
  // fingerprint algorithm
  explicit HashFn(int p, bool precompute = true) {
    this->p = p;
    this->precompute = precompute;
    if (precompute) {
      uint64_t result = 1;
      for (int i = 0; i < precomputedArraySize; i++) {
        precomputedPowers[i] = result;
        result *= p;
      }
    }
  }
  //virtual ~HashFn(){}
  ~HashFn(){}

  virtual uint64_t operator()(const char *input, int len,
      unsigned char lastCharCode, uint64_t lastHash);

  virtual uint64_t operator()(const char *input, int len);

 private:
  int p;
  bool precompute;
  uint64Array precomputedPowers;
};

#endif  // HASHFN_H_

hashFn.cc -->

#include "hashFn.h"

uint64_t HashFn::operator()(const char *input, int len,
      unsigned char lastCharCode, uint64_t lastHash) {
    // See the abracadabra example:
    // https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
    return (lastHash - lastCharCode *
      customPow(&precomputedPowers, precompute, p, len - 1)) *
      p + input[len - 1];
  }

uint64_t HashFn::operator()(const char *input, int len) {
    uint64_t total = 0;
    for (int i = 0; i < len; i++) {
      total += input[i] *
        customPow(&precomputedPowers, precompute, p, len - i - 1);
    }
    return total;
  }

已经有一个来自 HashFn 的派生类:

class HashFn2Byte : public HashFn {
 public:
  HashFn2Byte() : HashFn(0, false) {
  }

  uint64_t operator()(const char *input, int len,
      unsigned char lastCharCode, uint64_t lastHash) override;

  uint64_t operator()(const char *input, int len) override;
};
......

什么地方出了错?我可以隐约理解这与虚拟析构函数有关,但不确定为什么 vtable 未定义(如果我定义了所有声明,那么 vtable 应该自动存在吗?)。

另外,我现在正在玩 Chromium,所以我不知道所有文件都被编译成“巨型”对象这一事实会如何影响结果。此代码的独立版本(一个 Node 原生模块)可以正常编译和运行。

任何输入表示赞赏!谢谢。

标签: c++virtual-functions

解决方案


推荐阅读