cmake配置qt项目

  • Post author:
  • Post category:build
  • Post comments:0评论
cmake_minimum_required(VERSION 3.14)
# name 
set(MY_PROJECT_NAME qtdialog)
# 
project(${MY_PROJECT_NAME} LANGUAGES CXX)
# 
message ("cmake:${PROJECT_NAME}")
# qt
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# debug suffix -d
set(CMAKE_DEBUG_POSTFIX -d)
# copy to
set(MY_SOURCE_LIB_DIR "${PROJECT_SOURCE_DIR}/../lib/")
# include
include_directories("../include") 
include_directories("../") 
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# lib
link_directories("${MY_SOURCE_LIB_DIR}")

# default Release
if (NOT CMAKE_BUILD_TYPE)
   set (CMAKE_BUILD_TYPE "Release")
   message (
      STATUS "${PROJECT_NAME} no CMAKE_BUILD_TYPE selected, defaulting to ${CMAKE_BUILD_TYPE} "
   )
endif()
# qt
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Network Gui Widgets WebEngineWidgets WebChannel)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Network Gui Widgets WebEngineWidgets WebChannel)
# .cpp .h .ui
FILE(GLOB SOURCEFILES "./*.cpp") 
FILE(GLOB HEADERFILES "./*.h") 
FILE(GLOB UIFILES "./*.ui") 
# 生成ui
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
 QT6_WRAP_UI(WRAPS ${UIFILES})
elseif(${QT_VERSION_MAJOR} GREATER_EQUAL 5)
 QT5_WRAP_UI(WRAPS ${UIFILES})
else()
QT_WRAP_UI(WRAPS ${UIFILES})
endif()
# qrc
SET(RCCFILES res.qrc)
# UI group
SOURCE_GROUP("UI" FILES ${UIFILES} ${WRAPS} )
# exec
add_executable(${PROJECT_NAME} 
  ${SOURCEFILES}
  ${HEADERFILES}
  ${UIFILES}
  res.qrc # qrc
)
# link my libs
if(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE
            debug mylib1-d.lib
            debug mylib2-d.lib
            optimized mylib1.lib  
            optimized mylib2.lib  
)
else()#(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE
            debug libmylib1-d.so
            debug libmylib2-d.so
            optimized libmylib1.so  
            optimized libmylib2.so  
            -lpthread
)
endif()#(WIN32)
# link qt
target_link_libraries(${PROJECT_NAME} PRIVATE 
  Qt${QT_VERSION_MAJOR}::Widgets # Widgets
  Qt${QT_VERSION_MAJOR}::Network # Network
  Qt${QT_VERSION_MAJOR}::WebEngineWidgets # WebEngine
  Qt${QT_VERSION_MAJOR}::WebChannel # WebChannel
)
# vs unicode
if(MSVC)
  add_definitions("-DUNICODE" "-D_UNICODE")
  target_compile_options(${PROJECT_NAME} PUBLIC "/ZI")
  target_link_options(${PROJECT_NAME} PUBLIC "/INCREMENTAL")
endif()
# 生成后事件
if (WIN32) 
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        # 生成后事件
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  #  不同就复制
        "$<$<CONFIG:Debug>:debug/${PROJECT_NAME}.exe>"      # debug copy file
        "$<$<CONFIG:Release>:release/${PROJECT_NAME}.exe>"      # release copy file
        "${MY_SOURCE_LIB_DIR}")                 # TO 
else() # (WIN32) 
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD        
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  
        "$<$<CONFIG:Debug>:${PROJECT_NAME}>"  
        "$<$<CONFIG:Release>:${PROJECT_NAME}>"       
        "${MY_SOURCE_LIB_DIR}")                
endif() # (WIN32)  
文章作者: 张拓
文章链接: http://www.xssl.online/cmake%e9%85%8d%e7%bd%aeqt%e9%a1%b9%e7%9b%ae/
版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议。转载请注明来自 张拓的博客
浏览次数: 582

张拓

陕西西安蓝田张拓QQ1070410059。一生所求不过“心安”二字。 然,尘世多纷扰。

发表回复