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

时间:2021-05-01 12:18:03

最简单的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=)
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=)
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 )
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)