CMake Error: undefined reference to 'vtable for IDenoise'

  /Users/XXXXXXX/git/xxxxx/libraries/mediacore/src/main/cpp/audio/webrtc/IDenoise.h:12: error: undefined reference to 'vtable for IDenoise'
  /Users/XXXXXXX/AndroidDev/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: the vtable symbol may be undefined because the class is missing its key function (see go/missingkeymethod)audio/CMakeFiles/audio.dir/webrtc/DenoiseWebrtcImpl.cpp.o:/Users/XXXXXXX/git/xxxxx/libraries/mediacore/src/main/cpp/audio/webrtc/DenoiseWebrtcImpl.cpp:typeinfo for DenoiseWebrtcImpl: error: undefined reference to 'typeinfo for IDenoise'

  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


原因是定义的接口没有定义成纯虚函数,

将以下代码:

class IDenoise
{
public:
    /* 功能:创建及销毁对象 **/
    static IDenoise* CreateObject();
    static void DestroyObject(IDenoise** pObject);
public:
    virtual    int init(int samplerate, int channel);
    virtual    float get_latency();
    virtual    void reset();
    virtual    int process(float * buffer, int len);
    virtual    void uninit() ;
};

改为:

class IDenoise
{
public:
    /* 功能:创建及销毁对象 **/
    static IDenoise* CreateObject();
    static void DestroyObject(IDenoise** pObject);
public:
    virtual    int init(int samplerate, int channel) = 0;
    virtual    float get_latency() = 0;
    virtual    void reset() = 0;
    virtual    int process(float * buffer, int len) = 0;
    virtual    void uninit() = 0;
};

参考:

https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable


Undefined reference to vtable may occur due to the following situation also. Just try this:
Class A Contains:
virtual void functionA(parameters)=0; 
virtual void functionB(parameters);
Class B Contains:
1. The definition for the above functionA.
2. The definition for the above functionB.
Class C Contains: Now you're writing a Class C in which you are going to derive it from Class A.
Now if you try to compile you will get Undefined reference to vtable for Class C as error.
Reason:
functionA is defined as pure virtual and its definition is provided in Class B. functionB is defined as virtual (NOT PURE VIRTUAL) so it tries to find its definition in Class A itself but you provided its definition in Class B.
Solution:
1. Make function B as pure virtual (if you have requirement like that) virtual void functionB(parameters) =0; (This works it is Tested)
2. Provide Definition for functionB in Class A itself keeping it as virtual . (Hope it works as I didn't try this)




你可能感兴趣的:(C/C++)