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

最近在写一套基础类库用于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*)': 
../../sg_analyse_base/src/SgAnalyseStatBase.cpp:22: undefined reference to `vtable for CSgAnalyseStatBase' 
SgAnalyseStatBase.o(.text+0x117): In function `CSgAnalyseStatBase::CSgAnalyseStatBase[in-charge](CMmogAnalyseStatManager*)': 
../../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时也会出现这个链接错误。不管如何,问题解决了,再辛苦也是值得的,以后在写代码时一定要严谨。

C++:undefined reference to vtable 原因与解决办法[转]的更多相关文章

  1. sphinx :undefined reference to `libiconv' 报错解决办法

    sphinx :undefined reference to `libiconv' 报错解决办法   2013-11-30 21:45:39 安装sphinx时不停报错...郁闷在make时报错,错误 ...

  2. Qt的“undefined reference to `vtable for”错误解决(手动解决,加深理解)

    使用QT编程时,当用户自定义了一个类,只要类中使用了信号或槽. Code::Blocks编译就会报错(undefined reference to `vtable for). Google上有很多这个 ...

  3. cocos2d-x发生undefined reference to `XX'异常 一劳永逸解决办法

    cocos2d-x发生undefined reference to `XX'错误 一劳永逸解决方法 参考文章: http://blog.csdn.net/kafeidev/article/detail ...

  4. Linux下eclipse编译C/C++程序遇到 undefined reference to `pthread_create'的异常解决办法

    解决方法:右键点击的当前project—>properties—>C/C++ Build—>Settings—>Tool Settings选项卡—>GCC C Linke ...

  5. Qt错误:类中使用Q_OBJECT宏导致undefined reference to vtable for "xxx::xxx"错误的原因和解决方法

    在进行Qt编程的时候,有时候会将类的定义和实现都写在源文件中,如果同时在该类中使用信号/槽,那么可能就会遇到 undefined reference to vtable for "xxx:: ...

  6. (转) Qt 出现“undefined reference to `vtable for”原因总结

    由于Qt本身实现的机制所限,我们在使用Qt制作某些软件程序的时候,会遇到各种各样这样那样的问题,而且很多是很难,或者根本找不到原因的,即使解决了问题,如果有人问你为什么,你只能回答--不知道. 今天我 ...

  7. Linux+CLion+树莓派远程编译时,Cmake编译出现undefined reference to `vtable for MainWindow'的解决办法

    在win+CLion上进行远程qt开发时碰到以下错误: 错误提示: undefined reference to `vtable for MainWindow' 原因:源文件的目录结构有问题?? 解决 ...

  8. Qt经典出错信息之undefined reference to `vtable for classname

    原文链接:Qt经典出错信息之undefined reference to `vtable for classname 这个出错信息太常见了,用过Qt两个月以上的朋友基本上都能自己解决了,因为太经典了, ...

  9. Qt 出现“undefined reference to `vtable for”

    在QT中定义了一个线程类,继承自QThread, 在类中未加 Q_OBJECT 时编译正常,加入后报错如下: undefined reference to `vtable for myThread' ...

随机推荐

  1. C# 打印文件

    这几天做的功能用到了打印这个功能,直接在网上找了点demo,在这里做个备份. 1.直接打印DataTable using System; using System.Collections.Generi ...

  2. mysqld-nt: Out of memory (Needed 1677720 bytes)解决方法

    http://www.jb51.net/article/58726.htm 今天发现网站有点慢,发现mysql日志中提示mysqld-nt: Out of memory (Needed 1677720 ...

  3. IOS-UI-UIDynamic(二)

    UIPushBehavior :推动效果 UIAttachmentBehavior:附着效果 UISnapBehavior:迅速移动效果 一.重要的属性 UIPushBehavior :推动效果 ty ...

  4. HTML5之 离线数据存储

    --- Storage接口 无论是sessionStorage还是localStorage 属性/方法 返回值 描述 ----------------------------------------- ...

  5. Linux---vi编辑器必会操作

    移动光标: (1)基本的上下左右:通过箭头按键控制 (2)跳到一行的末尾:键盘"end" (3)跳到一行的开头:键盘"home" (4)跳到最后一行:shift ...

  6. JavaScript 继承的几种模式

    /** * Created by 2016 on 2016/6/5. */ //1.原型链继承 //把子类的原型,定义为超类的实例 通过原型来访问超类的方法和属性 function Person(){ ...

  7. lnmp 虚拟主机配置及重写

    lnmp安装与调试,请看 http://www.cnblogs.com/lin3615/p/4376224.html 虚拟主机的配置编辑nginx配置文件 nginx.conf此为主配置文件 vim ...

  8. ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order

    如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...

  9. struts2 修改action的后缀

    struts2 修改action的后缀 struts2 的默认后缀是 .action 虽然很直观,但是很烦琐.很多人喜欢将请求的后缀改为 .do 在struts2中修改action后缀有两种比较简单的 ...

  10. [转]内嵌WORD/OFFICE的WINFORM程序——DSOFRAMER使用小结

    最近一直想用VC#2005做个内嵌WORD/OFFICE的WINFORM程序,目前主要有以下解决途径: 1.直接通过API把WORD/OFFICE的窗口句柄给放到WINFORM中(感觉较为复杂): 2 ...