C++:undefined reference to vtable 原因与解决办法 [转]

本文详细阐述了在C++编程中遇到的关于虚析构函数导致的链接错误问题,通过分析错误信息并采取适当措施最终解决问题的过程。包括在析构函数后面添加必要的函数体,并解释了为什么这样做可以解决错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在写一套基础类库用于SG解包blob字段统计,在写完了所有程序编译时遇到一个郁闷无比的错误: 
MailBox.o(.text+0x124): In function `CMailBox::CMailBox[not-in-charge](CMmogAnalyseStatManager*)': 
../src/MailBox.cpp:27: undefined reference to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' 
MailBox.o(.text+0x182): In function `CMailBox::CMailBox[in-charge](CMmogAnalyseStatManager*)': 
../src/MailBox.cpp:27: undefined reference to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' 
MailBox.o(.gnu.linkonce.t._ZN8CMailBoxD1Ev+0x33): In function `CMailBox::~CMailBox [in-charge]()': 
../include/MailBox.hpp:22: undefined reference to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' 
MailBox.o(.gnu.linkonce.t._ZN8CMailBoxD1Ev+0x4f):../include/MailBox.hpp:22: undefined reference to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' 
MailBox.o(.gnu.linkonce.t._ZN8CMailBoxD0Ev+0x33): In function `CMailBox::~CMailBox [in-charge deleting]()': 
../include/MailBox.hpp:22: undefined reference to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' 
MailBox.o(.gnu.linkonce.t._ZN8CMailBoxD0Ev+0x4f):../include/MailBox.hpp:22: more undefined references to `CSgAnalyseStatBase::~CSgAnalyseStatBase [not-in-charge]()' follow 
MailBox.o(.gnu.linkonce.r._ZTI8CMailBox+0x8): undefined reference to `typeinfo for CSgAnalyseStatBase' 
SgAnalyseStatBase.o(.text+0x1d): In function `CSgAnalyseStatBase::CSgAnalyseStatBase[not-in-charge](CMmogAnalyseStatManager*)': 
http://www.cnblogs.com/sg_analyse_base/src/SgAnalyseStatBase.cpp:22: undefined reference to `vtable for CSgAnalyseStatBase' 
SgAnalyseStatBase.o(.text+0x117): In function `CSgAnalyseStatBase::CSgAnalyseStatBase[in-charge](CMmogAnalyseStatManager*)': 
http://www.cnblogs.com/sg_analyse_base/src/SgAnalyseStatBase.cpp:22: undefined reference to `vtable for CSgAnalyseStatBase' 
collect2: ld returned 1 exit status 
make: *** [MailBox] Error 1 
         这个问题困扰了我好几天,上班时间比较多人打扰,周末到了,决心一定要在这个周末将问题解决。搜索“vtable for”时总是搜到Qt出现的undefined reference to `vtable for`,找不到问题所在,一筹莫展。将编译环境从slack ware换到SLES,还是出现同样的错误。仔细看看,所有obj文件都已正常生成,是在链接成bin文件的时候出错的。再从错误信息中找没有搜索过的关 键词来搜索,尝试了许多关键词后终于在搜索“undefined reference to `typeinfo”时在

http://www.wellho.net/上看到: 
undefined reference to typeinfo - C++ error message 

There are some compiler and loader error messages that shout obviously as to their cause, but there are others that simply don't give the new user much of an indication as to what's really wrong. And most of those I get to know pretty quickly, so that I can whip around a room during a course, making suggestions to delegate to check for missing ; characters or double quotes, to check that they have used the right type of brackets for a list subscript and haven't unintentionally written a function call, etc. 

Here's one of the more obscure messages from the Gnu C++ compiler - or rather from the loader: 

g++ -o polygon shape.o circle.o square.o polygon.o 
circle.o(.gnu.linkonce.r._ZTI6Circle+0x8): undefined reference to `typeinfo for Shape' 
square.o(.gnu.linkonce.r._ZTI6Square+0x8): undefined reference to `typeinfo for Shape' 
polygon.o(.gnu.linkonce.t._ZN5ShapeC2Ev+0x8): In function `Shape::Shape()': 
: undefined reference to `vtable for Shape' 
collect2: ld returned 1 exit status 


And you can be scratching you head for hour over that one! 

The error? shape.o contains a base class from which classes are derived in circle.o and square.o .. but virtual function(s) in shape's definition are missing null bodies. 

The fix? You've got line(s) like 
virtual float getarea() ; 
that should read 
virtual float getarea() {} ; 

The complete (working) source code files for this example are available  here 

         按照文中所说稍微修改了一下,在析构函数后面添加了{},再make,成功了,高兴啊!问题终于解决了。我的所有虚函数都是有定义的,没想到就因为写基类 的这个虚析构函数大意,没写函数体就出现了一个困扰我几天的莫名其妙的错误。就virtual ~CSgAnalyseStatBase();和virtual ~CSgAnalyseStatBase() {};的区别,编译可以通过却搞出个莫名其妙的链接错误。链接器linker需要将虚函数表vtable 放入某个object file,但是linker无法找到正确的object文件。这个错误常见于刚刚创建一系列有继承关系的class的时候,这个时候很容易忘了给base class的virtual function加上函数实现。解决办法:给基类的virtual函数加上本来就应该有的function body。当含有虚函数的类未将析构函数声明为virtual时也会出现这个链接错误。不管如何,问题解决了,再辛苦也是值得的,以后在写代码时一定要严 谨。
这个错误信息 "undefined reference to `vtable for Shape'" 是在使用C++编译器时出现的。它通常表示编译器无法找到名为 "Shape" 的类的虚函数表(vtable)的引用。 在C++中,虚函数表(vtable)是用于实现多态性的重要机制。每个具有虚函数的类都有自己的虚函数表,用于存储该类的虚函数的地址。当调用一个虚函数时,编译器会通过虚函数表来确定要调用的函数的地址。 出现 "undefined reference to `vtable for Shape'" 的错误可能有几个原因: 1. 没有为Shape类提供实现的虚函数。在声明一个虚函数时,需要在类的定义中提供该虚函数的实现。 2. 忘记将虚函数的定义类的实现进行链接。在使用分离式编译时,需要将类的定义和实现以及使用该类的代码进行链接。 3. Shape类的定义存在错误,导致编译器无法生成虚函数表。 要解决这个问题,可以采取以下几个步骤: 1. 确保在Shape类中的声明和定义中正确地使用了关键字 "virtual" 来标识虚函数。 2. 检查Shape类的定义和实现是否正确地匹配,并确保链接所有相关的文件。 3. 检查是否存在其他Shape类相关的错误,例如缺少必要的头文件或语法错误。 如果问题仍然存在,可能需要进一步分析代码和编译器输出,以找出更具体的问题所在。 编译时vtable和变量初始值设定项 没有完整程序的汇编级编译 特性 .Net Core 3.0兼容 极小内存占用 可移植性强 很容易和C 互操作 编译期虚表和变量初始化 程序集等级编译,不需要整个程序全部编译 支持的CLR功能<span class="em">1</span> #### 引用[.reference_title] - *1* [natsu-clr:il2cpp编译器和运行时.Net Core兼容](https://download.csdn.net/download/weixin_42168750/15070490)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值