C++:undefined reference to vtable 原因与解决办法 [转]
最近在寫一套基礎(chǔ)類庫用于SG解包blob字段統(tǒng)計,在寫完了所有程序編譯時遇到一個郁悶無比的錯誤:?
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出現(xiàn)的undefined reference to `vtable for`,找不到問題所在,一籌莫展。將編譯環(huán)境從slack ware換到SLES,還是出現(xiàn)同樣的錯誤。仔細看看,所有obj文件都已正常生成,是在鏈接成bin文件的時候出錯的。再從錯誤信息中找沒有搜索過的關(guān) 鍵詞來搜索,嘗試了許多關(guān)鍵詞后終于在搜索“undefined reference to `typeinfo”時在
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?
???????? 按照文中所說稍微修改了一下,在析構(gòu)函數(shù)后面添加了{},再make,成功了,高興啊!問題終于解決了。我的所有虛函數(shù)都是有定義的,沒想到就因為寫基類 的這個虛析構(gòu)函數(shù)大意,沒寫函數(shù)體就出現(xiàn)了一個困擾我?guī)滋斓哪涿畹腻e誤。就virtual ~CSgAnalyseStatBase();和virtual ~CSgAnalyseStatBase() {};的區(qū)別,編譯可以通過卻搞出個莫名其妙的鏈接錯誤。鏈接器linker需要將虛函數(shù)表vtable 放入某個object file,但是linker無法找到正確的object文件。這個錯誤常見于剛剛創(chuàng)建一系列有繼承關(guān)系的class的時候,這個時候很容易忘了給base class的virtual function加上函數(shù)實現(xiàn)。解決辦法:給基類的virtual函數(shù)加上本來就應(yīng)該有的function body。當含有虛函數(shù)的類未將析構(gòu)函數(shù)聲明為virtual時也會出現(xiàn)這個鏈接錯誤。不管如何,問題解決了,再辛苦也是值得的,以后在寫代碼時一定要嚴 謹。
總結(jié)
以上是生活随笔為你收集整理的C++:undefined reference to vtable 原因与解决办法 [转]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: warning: control rea
- 下一篇: google mock分享