环境:
Linux szdpu13 3.10.0-693.el7.x86_64, Qt 5.9.2
文件:
//main.cc mainwindow.cc/.h CMakeLists.txt
CMakeLists:
cmake_minimum_required(VERSION 3.1)
project(qt5Test)
# Find Qt5
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
find_package(X11 REQUIRED)
include_directories(${X11_INCLUDE_DIRS})
# Source files
set(SOURCE_FILES main.cpp mainwindow.cpp)
# Header files (including those with Q_OBJECT macro)
set(HEADER_FILES mainwindow.h)
# Generate moc files
qt5_wrap_cpp(MOC_SOURCES ${HEADER_FILES})
# Add executable
add_executable(qt5Test ${SOURCE_FILES} ${MOC_SOURCES})
# Link against Qt5 libraries
target_link_libraries(qt5Test Qt5::Core Qt5::Widgets ${X11_LIBRARIES})
# Set include directories for Qt5
target_include_directories(qt5Test PRIVATE ${Qt5Widgets_INCLUDE_DIRS})
注意:编译时后报错 undefined reference to `vtable for MainWindow
可能原因1:要加qt5_warp_cpp宏调用moc功能生成moc文件,后面参数跟带Q_OBJECT的头文件
原因2:链接Qt库少了Gui模块