Qt經典出錯資訊之undefined reference to `vtable for classname

來源:互聯網
上載者:User

 

這個出錯資訊太常見了,用過Qt兩個月以上的朋友基本上都能自己解決了,因為太經典了,可以給新手參考。

出錯資訊一般類似:undefined reference to `vtable for classname MyWidget`
在執行make命令後出現。

出錯原因是在定義類的時候為了能使用signals和slot,在類定義的後面加了Q_OBJECT引起。 因為Q_OBJECT是一個宏,在不同的類中展開是不同的代碼,例如在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個從父類繼承的3個虛函數。本來這三個函數的實現應該是由moc_mywidget.cpp來完成。
而當前的錯誤正是因為Makefile裡沒有將moc_mywidget.cpp加入編譯引起。

Makefile背後的原因是qmake,因為Qt的編譯系統是通過qmake將.pro檔案轉換成Makefile檔案。當qmake掃描.h代碼時發現有Q_OBJECT這樣字眼的代碼時,會將一個用moc產生moc_xxx.cpp代碼的依賴關係寫到Makefile裡。如果掃描時沒有找到 Q_OBJECT就不會產生額外的moc_xxx.cpp這樣的檔案。

出現最初一幕的原因是在執行qmake的時候.h代碼裡並沒有O_OBJECT這樣的代碼。而執行make的時候.h裡已經有Q_OBJECT了,解決的方法就是重新執行qmake,然後執行make.

本站所有文章由本站和原作者保留一切權力,僅在保留本著作權資訊、原文連結、原文作者的情況下允許轉載,轉載請勿刪改原文內容, 並不得用於商業用途。 謝謝合作。
原文連結:http://www.cuteqt.com/blog/?p=116

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.