离线虚拟方法 [英] Out-of-Line Virtual Method

查看:75
本文介绍了离线虚拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是离线虚拟方法,为什么它会影响链接时间?

What exactly is a out-of-line virtual method and why does it affect link times?

LLVM编码标准

如果某个类在头文件中定义并且具有vtable( 虚拟方法或它从具有虚拟方法的类派生), 该类中必须始终至少有一个离线虚拟方法. 否则,编译器会将vtable和RTTI复制到每个.o中. #包含标头的文件,.o文件的大小膨胀并增加 链接时间.

If a class is defined in a header file and has a vtable (either it has virtual methods or it derives from classes with virtual methods), it must always have at least one out-of-line virtual method in the class. Without this, the compiler will copy the vtable and RTTI into every .o file that #includes the header, bloating .o file sizes and increasing link times.

推荐答案

编译器必须为具有虚拟方法的类发出vtable.它包含这些方法的指针.如果所有虚拟方法都是内联的(在标头中定义),则编译器不知道要在其中转换vtable的转换单元(.cpp文件),因此它在所有虚拟方法中均会复制一个副本,链接程序将开始处理用它.这在目标文件中造成了额外的工作和负担.另一方面,如果离线定义了虚函数(在.cpp中),则可以将vtable在那里发出,因此将只发出一个副本.这同样适用于RTTI.

The compiler must emit a vtable for classes with virtual methods. This contains the pointers to these methods. If all the virtual methods are inline (defined in the header), then the compiler doesn't know which translation unit (.cpp file) to emit the vtable within, so it emits a copy in all of them and the linker gets to deal with it. This makes extra work and bloat in the object files. If, on the other hand, a virtual function is defined out-of-line (in a .cpp), the vtable can be emitted there and thus only one copy will be emitted. The same applies to the RTTI.

这篇关于离线虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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