linux cmakelist.txt使用qt库

原因:同事使用qt .pro链接库资源路径都写死了,更换机器非常麻烦,想到使用cmake方式来代替.pro方式,查阅了很多方式,解决了更换机器每次都要调整路径的麻烦……

cmakelist.txt

cmake_minimum_required(VERSION 2.8)

set(CMAKE_BUILD_TYPE Debug)

project(xxx)

find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
SET(QT_USE_QTOPENGL TRUE)

find_package(VTK REQUIRED)
include_directories(${VTK_INCLUDE_DIRS})
include_directories(${VTK_LIBRARY_DIRS})

set(CMAKE_AUTOMOC on)
set(CMAKE_AUTOUIC on)
set(CMAKE_AUTORCC on)

#继承qt界面资源的.h文件
set(moc_file
    include/xxx.h
    include/xxx1.h
    include/xxx2.h
    include/xxx3.h
    include/xxx4.h
    include/xxx5.h)

set(ui_file xxxx.ui)

#moc file
qt5_wrap_cpp(moc_wrap ${moc_file})

#ui file
qt5_wrap_ui(ui_wrap ${ui_file})

#resources file
qt5_add_resources(rcc_wrap resources/xxx.qrc)

add_executable(${PROJECT_NAME}
    ${moc_wrap}
    ${ui_wrap}
    ${rcc_wrap}
    src/main.cpp
    src/mainWindow.cpp    
    )

include_directories(
    include/
    ${OPENGL_INCLUDE_DIRS}
    )

target_link_libraries(${PROJECT_NAME}
    ${OPENGL_LIBRARIES}
    ${VTK_LIBRARIES}
    ${Qt5Widgets_LIBRARIES}
    ${Qt5OpenGL_LIBRARIES}
    )

结果:编译运行成功!

你可能感兴趣的:(C++)