clang编译器, __declspec(novtable)修饰的class 如果虚函数不声明为纯虚则链接失败.

遇到一个和novtable有关的编译链接问题。

class __declspec(novtable) Base
{
public:
    // clang 3.1 cannot build without following "=0". But VC can build.
    virtual bool function(int i)/*=0*/;
};
 
class Child : public Base
{
public:
    virtual bool function(int i);
};
 
bool Child::function(int i)
{
    return true;
}
以上代码, Base的function()函数无论是否纯虚,在VC上都能Build过,但是用clang,纯虚可以Link过,不是纯虚Link不过。出错信息如下:

===========================================================

Undefined symbols forarchitecture x86_64:

  "typeinfo forBase", referenced from:

      typeinfofor Child in main.o

ld: symbol(s) not foundfor architecture x86_64

clang: error: linkercommand failed with exit code 1 (use -v to see invocation)

===========================================================

需要仔细研究一下novtable. 研究一下,稍后发原因。

你可能感兴趣的:(clang编译器, __declspec(novtable)修饰的class 如果虚函数不声明为纯虚则链接失败.)