网上随便找了一个QT的小程序源码,编译时报错,
严重性 代码 说明 项目 路径 文件 行 禁止显示状态
错误 LNK2001 无法解析的外部符号 "public: virtual struct QMetaObject const * __cdecl MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UEBAPEBUQMetaObject@@XZ) EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp\mainwindow.obj 1
错误 LNK2001 无法解析的外部符号 "public: virtual void * __cdecl MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UEAAPEAXPEBD@Z) EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp\mainwindow.obj 1
错误 LNK2001 无法解析的外部符号 "public: virtual int __cdecl MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp\mainwindow.obj 1
错误 LNK2001 无法解析的外部符号 "public: static struct QMetaObject const MainWindow::staticMetaObject" (?staticMetaObject@MainWindow@@2UQMetaObject@@B) EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp E:\vQt\EasyOCRApp\EasyOCRApp\mainwindow.obj 1
错误 LNK1120 4 个无法解析的外部命令 EasyOCRApp E:\vQt\EasyOCRApp\x64\Debug E:\vQt\EasyOCRApp\x64\Debug\EasyOCRApp.exe 1
原因是qmake没有执行相应的moc指令。
问题是:要怎样才能让vs2019调用qmake呢?我查了一下,每一个QT继承类我都加了Q_OBJECT关键字,而VS2019在编译时并没有报错,只是在链接时报错;项目是vs的Qt工具创建的,配置应该也没有错。
在stackoverflow上找到一个贴子,具体可以参考附录,给了我一些提示。
解决办法:
把所以有头文件添加到c++项目。
这一点和普通的vs c++项目不一样,vs不对项目外的C++头文件进行编译,只是引用。
附录:
Unresolved symbols QMetaObject when building OpenCV with QT support - Stack Overflow
The Qt MOC (meta-object compiler) is not being run on your Qt class files.
Since you're building from the OpenCV release, I'm assuming that the UI classes already have a Q_OBJECT line within the class declaration.
To generate a .vcproj file that has the necessary build instructions to run MOC, run this command:
qmake -tp vc -r
If that doesn't work, right-click one of the .h files that contains a Qt class. Under Properties->Custom Build Step->General, you should see something like:
Command Line "$(QTDIR)\bin\moc.exe" -I".\GeneratedFiles\." -I"$(QTDIR)\include\." -I".\GeneratedFiles\$(ConfigurationName)\." -I"$(QTDIR)\include\QtCore\." -I"$(QTDIR)\include\QtGui\." .\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp" "-fstdafx.h" "-f[YourQtFile.h]"
Description Moc'ing $(InputFileName)...
Outputs ".\GeneratedFiles\$(ConfigurationName)\moc_$(InputName).cpp"
Additional Dependencies "$(QTDIR)\bin\moc.exe";$(InputPath)
另一位回答者给出了一些图,所以我截了个滚屏,如下,