最简单的VS-Qt-CMake项目框架

Posted 酷熊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最简单的VS-Qt-CMake项目框架相关的知识,希望对你有一定的参考价值。

使用qtcreator新建一个空工程,可以得到main.cpp,mainwindow.cpp,mainwindow.h和mainwindow.ui四个文件

下面主要介绍CMakeLists.txt的内容

cmake_minimum_required(VERSION 3.0)

SET(PRODUCT_NAME ReadS3C)

SET(PRODUCT_HEADER_FILES
    mainwindow.h
)

SET(PRODUCT_UI_FILES
    mainwindow.ui
)

SET(PRODUCT_FILES
    main.cpp
    mainwindow.cpp
    ${PRODUCT_HEADER_FILES}
)

SET(PRODUCT_DATA_FILES
)

SET(QT_FOUND FALSE)
IF(${CMAKE_VERSION} VERSION_LESS "3.2.0")
    FIND_PACKAGE(Qt5Widgets)
    IF(Qt5Widgets_FOUND)
        ADD_DEFINITIONS(-DUSE_QT_VERSION=5)
        SET(QT_FOUND TRUE)
    ELSE(Qt5Widgets_FOUND)
        MESSAGE("*** Qt5 not found, which may be caused by missing/wrong CMake module files. "
                "You should set Qt5Widgets_DIR to $(QT_DIR)/lib/cmake/Qt5Widgets/ to find variables.")
    ENDIF(Qt5Widgets_FOUND)
ELSE()
    FIND_PACKAGE(Qt5 COMPONENTS Widgets)
    IF(Qt5_FOUND)
        ADD_DEFINITIONS(-DUSE_QT_VERSION=5)
        SET(QT_FOUND TRUE)
    ENDIF(Qt5_FOUND)
ENDIF()

QT5_WRAP_UI(UI_FILES ${PRODUCT_UI_FILES}) 
QT5_WRAP_CPP(MOC_FILES ${PRODUCT_HEADER_FILES})

SET(PRODUCT_FILES ${PRODUCT_FILES} ${UI_FILES} ${MOC_FILES})
IF(WIN32)
    #SET(PRODUCT_FILES ${PRODUCT_FILES} my.rc)
ENDIF(WIN32)

IF(QT_FOUND)
    #QT5_ADD_RESOURCES(RES_FILES skin/qdarkstyle/style.qrc)
ENDIF()
SET(PRODUCT_FILES ${PRODUCT_FILES} ${RES_FILES})

SOURCE_GROUP("auto generated files" FILES ${UI_FILES} ${MOC_FILES} ${RES_FILES})
SOURCE_GROUP("ui files" FILES ${PRODUCT_UI_FILES})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
#LINK_DIRECTORIES(${OSG_LIB_DIR})
    
SET(WITH_CONSOLE 1)
IF(${WITH_CONSOLE})
    ADD_EXECUTABLE(${PRODUCT_NAME} ${PRODUCT_FILES})
ELSE(${WITH_CONSOLE})
    ADD_EXECUTABLE(${PRODUCT_NAME} WIN32 ${PRODUCT_FILES})
ENDIF(${WITH_CONSOLE})

SET_TARGET_PROPERTIES(${PRODUCT_NAME} PROPERTIES DEBUG_POSTFIX "${CMAKE_DEBUG_POSTFIX}")

TARGET_LINK_LIBRARIES(${PRODUCT_NAME}
    ${OPENGL_LIBRARIES} ${THIRD_PARTY_LIBRARIES} ${EXTERNAL_LIBRARIES}
)

SET_TARGET_PROPERTIES(${PRODUCT_NAME} PROPERTIES 
    ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib"
    ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG "${PROJECT_BINARY_DIR}/bin"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE "${PROJECT_BINARY_DIR}/bin")

INSTALL(TARGETS ${PRODUCT_NAME} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
INSTALL(FILES ${PRODUCT_DATA_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin/data)

IF(QT_FOUND)
    QT5_USE_MODULES(${PRODUCT_NAME} Widgets Network OpenGL)
ENDIF(QT_FOUND)

 

以上是关于最简单的VS-Qt-CMake项目框架的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段

常用Javascript代码片段集锦

代码片段 - Golang 实现简单的 Web 服务器

神级Java程序员 开车教你基础开发,最简单 微型Java Web框架

使用片段上的按钮更改片段

Android片段和依赖注入