clang的-web-vtables的意思是什么? [英] What is the meaning of clang's -Wweak-vtables?

查看:217
本文介绍了clang的-web-vtables的意思是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上不明白clang的 -Wweak-vtables 。这是我到目前为止观察到的:

I basically do not understand clang's -Wweak-vtables. Here is what I observed so far:

情况一:(触发警告)

class A {
    public:
    virtual ~A(){}        
};

class B : public A {
    public:
    virtual ~B(){}
};

int main(){}

/ strong>(不触发警告)

Case two: (Does not trigger warning)

class A {
    public:
    virtual ~A(){}        
};   

int main(){}

/ strong>(不触发警告)

Case three: (Does not trigger warning)

class A {
    public:
    virtual ~A();

};

A::~A(){}

class B : public A {
    public:
    virtual ~B(){}
};

int main(){}

/ strong>(触发警告)

Case four: (Triggers warning)

class A {
    public:
    virtual ~A(){}
    virtual void fun(){}        
};    

class B : public A {
    public:
    virtual ~B(){}
};

int main(){}

/ strong>(不触发警告)

Case five: (Does not trigger warning)

class A {
    public:
    virtual ~A(){}
    virtual void fun();      
};    

class B : public A {
    public:
    virtual ~B(){}
};

int main(){}

/ strong>(不触发警告)

Case six: (Does not trigger warning)

class A {
    public:
    virtual ~A(){}
    virtual void fun(){}
};    

class B : public A {};

int main(){}

/ strong>(不触发警告)

Case seven: (Does not trigger warning)

class A {
    public:
    virtual ~A(){}
    virtual void fun(){}
};    

class B : public A {
    public:
    virtual void fun(){}
};

int main(){}

确切的警告是

warning: 'A' has no out-of-line virtual method definitions; its vtable 
will be emitted in every translation unit [-Wweak-vtables]

,如果我不在类中声明一个非内联的虚函数,它会导致一些
种问题,如果且只有从它派生而派生类有一个虚拟析构函数。

So apparently, if I do not declare a non-inline virtual function in a class, it causes some kind of problem if and only if I derive from it and the derived class has a virtual destructor.

问题:


  1. 为什么会出现问题?

  2. 为什么通过声明虚函数来修复它? (警告说
    的定义)

  3. 为什么当我不是从课堂派生时不会发生警告?

  4. 当派生类没有虚拟析构函数时,不会发生警告。

  1. Why is this a problem?
  2. Why does this get fixed by declaring a virtual function? (Warning speaks of definitions)
  3. Why does the warning not occur when I do not derive from the class?
  4. Why does the warning not occur when the derived class does not have a virtual destructor?


推荐答案

类的 virtual 方法是内联的,编译器没有办法选择在其中放置vtable的单个共享副本的转换单元;相反,必须将vtable的副本放在需要它的每个目标文件中。在许多平台上,链接器能够统一这些多个副本,通过丢弃重复的定义或将所有引用映射到一个副本,所以这只是一个警告。

If all of a class's virtual methods are inline, the compiler has no way to select a translation unit in which to place a single shared copy of the vtable — instead, a copy of the vtable has to be placed in each object file that needs it. On many platforms the linker is able to unify these multiple copies, either by discarding duplicate definitions or by mapping all references to one copy, so this is only a warning.

实现一个 virtual 外部函数使编译器能够选择实现该外部方法的翻译单元作为类的实现细节的home,将vtable的单个共享副本放置在同一翻译单元中。如果多个方法不在线,编译器可以选择任意方法,只要该选择只由类的声明决定即可。例如,GCC以声明顺序选择第一个非内联方法。

Implementing a virtual function out-of-line enables the compiler to select the translation unit that implements that out-of-line method as a "home" for the class's implementation details, and places the single shared copy of the vtable in the same translation unit. If multiple methods are out-of-line, an arbitrary choice of method may be made by the compiler so long as that choice is determined only by the class's declaration; for example, GCC chooses the first non-inline method in declaration order.

如果不重写任何类的方法,则 virtual 关键字没有可观察到的效果,因此编译器不需要为类发出vtable。如果你不是从 A 派生,或者你没有声明一个派生类的析构函数 virtual A 中的方法,因此省略了 A 的vtable。如果你声明一个额外的外部 virtual 方法来抑制警告,并且做一些覆盖 A ,需要在链接的翻译单元中提供非内联 virtual (及其附带的vtable副本)的实现,否则链接将失败,因为vtable缺少。

If you don't override any method of a class, the virtual keyword has no observable effect, so there's no need for the compiler to emit a vtable for the class. If you do not derive from A, or if you fail to declare a derived class's destructor virtual, there are no overridden methods in A and thus A's vtable is omitted. If you declare an additional out-of-line virtual method to suppress the warning and also do something that overrides a method in A, the implementation of the non-inline virtual (and its accompanying copy of the vtable) needs to be supplied in a linked translation unit, otherwise linking will fail because the vtable is missing.

这篇关于clang的-web-vtables的意思是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆