使用Q_OBJECT宏遇到的LINK2001错误

时间:2021-10-01 04:48:50

使用了Q_OBJECT宏之后在编译的时候会遇到下面的错误:

--------------------Configuration: testqt - Win32 Debug--------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyMainWindow@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
test.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyMainWindow::qt_metacast(char const *)" (?qt_metacast@MyMainWindow@@UAEPAXPBD@Z)
test.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyMainWindow::metaObject(void)const " (?metaObject@MyMainWindow@@UBEPBUQMetaObject@@XZ)
debug/testqt.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

testqt.exe - 4 error(s), 0 warning(s)

 

LINK2001的错误就是找不到函数的实现。

在文件末尾加入:#include "test.moc"

*.moc   qmake生成的moc文件。

 

有关moc的东西请参考:

http://www.qiliang.net/qt/moc.html

http://doc.trolltech.com/4.0/moc.html