Posted on 2010-12-14 23:39
RTY 閱讀(1938)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
QT相關(guān)資料收集
這個(gè)出錯(cuò)信息太常見了,用過Qt兩個(gè)月以上的朋友基本上都能自己解決了,因?yàn)樘?jīng)典了,可以給新手參考。
出錯(cuò)信息一般類似:undefined reference to `vtable for classname MyWidget` 在執(zhí)行make命令后出現(xiàn)。
出錯(cuò)原因是在定義類的時(shí)候?yàn)榱四苁褂胹ignals和slot,在類定義的后面加了Q_OBJECT引起。 因?yàn)镼_OBJECT是一個(gè)宏,在不同的類中展開是不同的代碼,例如在mywidgeth.h中生成的 public: template inline void qt_check_for_QOBJECT_macro(const T &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i; } static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); static inline QString tr(const char *s, const char *c = 0) { return staticMetaObject.tr(s, c); } static inline QString trUtf8(const char *s, const char *c = 0) { return staticMetaObject.trUtf8(s, c); } static inline QString tr(const char *s, const char *c, int n) { return staticMetaObject.tr(s, c, n); } static inline QString trUtf8(const char *s, const char *c, int n) { return staticMetaObject.trUtf8(s, c, n); } virtual int qt_metacall(QMetaObject::Call, int, void **); private: 可以看到以上的代碼聲明了3個(gè)從父類繼承的3個(gè)虛函數(shù)。本來這三個(gè)函數(shù)的實(shí)現(xiàn)應(yīng)該是由moc_mywidget.cpp來完成。 而當(dāng)前的錯(cuò)誤正是因?yàn)镸akefile里沒有將moc_mywidget.cpp加入編譯引起。
Makefile背后的原因是qmake,因?yàn)镼t的編譯系統(tǒng)是通過qmake將.pro文件轉(zhuǎn)換成Makefile文件。當(dāng)qmake掃描.h代碼時(shí)發(fā)現(xiàn)有Q_OBJECT這樣字眼的代碼時(shí),會(huì)將一個(gè)用moc生成moc_xxx.cpp代碼的依賴關(guān)系寫到Makefile里。如果掃描時(shí)沒有找到 Q_OBJECT就不會(huì)生成額外的moc_xxx.cpp這樣的文件。
出現(xiàn)最初一幕的原因是在執(zhí)行qmake的時(shí)候.h代碼里并沒有O_OBJECT這樣的代碼。而執(zhí)行make的時(shí)候.h里已經(jīng)有Q_OBJECT了,解決的方法就是重新執(zhí)行qmake,然后執(zhí)行make.
本站所有文章由本站和原作者保留一切權(quán)力,僅在保留本版權(quán)信息、原文鏈接、原文作者的情況下允許轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)勿刪改原文內(nèi)容, 并不得用于商業(yè)用途。 謝謝合作。 原文鏈接:http://www.cuteqt.com/blog/?p=116 |