QT undefined reference to vtable for myWidget

cmake 报错:

1
2
3
4
5
6
7
8
9
10
====================[ Build | QT | Debug ]======================================
/opt/clion/bin/cmake/linux/bin/cmake --build /home/peeweep/CLionProjects/QT/cmake-build-debug --target QT -- -j 8
[ 33%] Linking CXX executable QT
/usr/bin/ld: CMakeFiles/QT.dir/mywidget.cpp.o: in function `myWidget':
/home/peeweep/CLionProjects/QT/mywidget.cpp:6: undefined reference to `vtable for myWidget'
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [CMakeFiles/QT.dir/build.make:102: QT] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/QT.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/QT.dir/rule] Error 2
make: *** [Makefile:118: QT] Error 2

这是因为在 mywidget.h 里使用了 Q_OBJECT
解决方法1: 生成对应的moc

1
$ moc mywidget.h > mywidget.moc

然后在mywidget.cpp 里引用

1
#include "mywidget.moc"

解决方法2: 让CMake自动生成moc

CMakeLists.txt

1
set(CMAKE_AUTOMOC ON)
# , ,
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×