Qt项目CMake构建出现vtable未定义引用问题及解决
Qt6 + CMake 构建EventCameraAnalysis项目的链接错误解决
问题描述
在使用CMake构建基于Qt6的EventCameraAnalysis项目时出现链接错误,相关代码及错误信息如下:
初始CMakeLists.txt
cmake_minimum_required(VERSION 3.16) project(EventCameraAnalysis LANGUAGES CXX C VERSION 0.1.0) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) find_package(Qt6 COMPONENTS Widgets Charts REQUIRED) add_subdirectory(libs/qcustomplot) add_executable(${PROJECT_NAME} src/EventCameraThread.cpp src/main.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Widgets Qt6::Charts qcustomplot) target_include_directories( EventCameraAnalysis PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include> $<INSTALL_INTERFACE:include> PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(${PROJECT_NAME} PRIVATE QCUSTOMPLOT_USE_LIBRARY)
EventCameraThread.h
#pragma once #include <QObject> #include <QThread> #include <QMutex> #include <QWaitCondition> #include <metavision/sdk/driver/camera.h> class EventCameraThread : public QThread { Q_OBJECT public: EventCameraThread(QObject *parent = nullptr); ~EventCameraThread(); void run() override; signals: void newFrame(const QImage &frame); void error(int socketError, const QString &message); private: QMutex mutex_; QWaitCondition cond_; bool quit_; Metavision::Camera camera_; };
EventCameraThread.cpp
#include <event_camera_analysis/EventCameraThread.h> EventCameraThread::EventCameraThread(QObject *parent) : QThread(parent), quit_{false} {} EventCameraThread::~EventCameraThread() {} void EventCameraThread::run() { }
构建错误信息
In file included from /usr/include/x86_64-linux-gnu/qt6/QtGui/QtGui:61, from /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport/QtPrintSupportDepends:4, from /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport/QtPrintSupport:3, from /data/repos/phd/event_camera_analysis/libs/qcustomplot/qcustomplot.h:97, from /data/repos/phd/event_camera_analysis/src/main.cpp:19: /usr/include/x86_64-linux-gnu/qt6/QtGui/qopenglcontext.h:56:2: warning: #warning qopenglfunctions.h is not compatible with GLEW, GLEW defines will be undefined [-Wcpp] 56 | #warning qopenglfunctions.h is not compatible with GLEW, GLEW defines will be undefined | ^~~~~~~ /usr/include/x86_64-linux-gnu/qt6/QtGui/qopenglcontext.h:57:2: warning: #warning To use GLEW with Qt, do not include <qopengl.h> or <QOpenGLFunctions> after glew.h [-Wcpp] 57 | #warning To use GLEW with Qt, do not include <qopengl.h> or <QOpenGLFunctions> after glew.h | ^~~~~~~ In file included from /usr/include/x86_64-linux-gnu/qt6/QtGui/qopenglextrafunctions.h:47, from /usr/include/x86_64-linux-gnu/qt6/QtGui/QtGui:62, from /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport/QtPrintSupportDepends:4, from /usr/include/x86_64-linux-gnu/qt6/QtPrintSupport/QtPrintSupport:3, from /data/repos/phd/event_camera_analysis/libs/qcustomplot/qcustomplot.h:97, from /data/repos/phd/event_camera_analysis/src/main.cpp:19: /usr/include/x86_64-linux-gnu/qt6/QtGui/qopenglfunctions.h:49:2: warning: #warning qopenglfunctions.h is not compatible with GLEW, GLEW defines will be undefined [-Wcpp] 49 | #warning qopenglfunctions.h is not compatible with GLEW, GLEW defines will be undefined | ^~~~~~~ /usr/include/x86_64-linux-gnu/qt6/QtGui/qopenglfunctions.h:50:2: warning: #warning To use GLEW with Qt, do not include <qopengl.h> or <QOpenGLFunctions> after glew.h [-Wcpp] 50 | #warning To use GLEW with Qt, do not include <qopengl.h> or <QOpenGLFunctions> after glew.h | ^~~~~~~ [100%] Linking CXX executable EventCameraAnalysis /usr/bin/ld: CMakeFiles/EventCameraAnalysis.dir/src/EventCameraThread.cpp.o: in function `EventCameraThread::EventCameraThread(QObject*)': /data/repos/phd/event_camera_analysis/src/EventCameraThread.cpp:4: undefined reference to `vtable for EventCameraThread' /usr/bin/ld: CMakeFiles/EventCameraAnalysis.dir/src/EventCameraThread.cpp.o: in function `EventCameraThread::~EventCameraThread()': /data/repos/phd/event_camera_analysis/src/EventCameraThread.cpp:6: undefined reference to `vtable for EventCameraThread' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/EventCameraAnalysis.dir/build.make:155: EventCameraAnalysis] Error 1 make[1]: *** [CMakeFiles/Makefile2:103: CMakeFiles/EventCameraAnalysis.dir/all] Error 2 make: *** [Makefile:91: all] Error 2
解决方案
使用CMake的qt_wrap_cpp工具处理包含Q_OBJECT宏的头文件,确保MOC(元对象编译器)正确生成元对象代码,修改后的CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.16) project(EventCameraAnalysis LANGUAGES CXX VERSION 0.1.0) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) find_package(Qt6 COMPONENTS Core Widgets Charts REQUIRED) find_package(MetavisionSDK COMPONENTS core driver ui REQUIRED) add_subdirectory(libs/qcustomplot) qt_wrap_cpp(SOURCES include/event_camera_analysis/EventCameraThread.h include/event_camera_analysis/QtGuiApplication.h) qt_add_executable(${PROJECT_NAME} src/EventCameraThread.cpp src/QtGuiApplication.cpp ${SOURCES} src/main.cpp) target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Widgets Qt6::Charts qcustomplot MetavisionSDK::core MetavisionSDK::driver MetavisionSDK::ui) target_include_directories( EventCameraAnalysis PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include> $<INSTALL_INTERFACE:include> PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17) target_compile_definitions(${PROJECT_NAME} PRIVATE QCUSTOMPLOT_USE_LIBRARY)
关键修改说明
- 新增
qt_wrap_cpp命令,显式指定需要MOC处理的头文件,解决包含Q_OBJECT类的虚表未定义问题。 - 添加
Qt6::Core组件依赖,确保Qt核心功能完整。 - 引入MetavisionSDK的依赖查找与链接,适配项目中使用的
Metavision::Camera类。 - 使用
qt_add_executable替代add_executable,符合Qt6的CMake规范。
内容的提问来源于stack exchange,提问作者Andreas Ziegler
相关产品推荐
相关产品推荐

