CMake Qt 配置

  1. CMake 模块
    • ConfigCxx.cmake
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


IF(WIN32)
	set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
ENDIF()
  • ConfigDll.cmake



set(OUTPUT_DIR ${PROJECT_SOURCE_DIR}/../${PREFIX_DIR})

message(STATUS "[${PROJECT_NAME}]" OUTPUT_DIR:${OUTPUT_DIR})

set_target_properties(${PROJECT_NAME} PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY_DEBUG              "${OUTPUT_DIR}"
    LIBRARY_OUTPUT_DIRECTORY_DEBUG              "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_DEBUG              "${OUTPUT_DIR}"
    PDB_OUTPUT_DIRECTORY_DEBUG                "${OUTPUT_DIR}"

    ARCHIVE_OUTPUT_DIRECTORY_RELEASE             "${OUTPUT_DIR}"
    LIBRARY_OUTPUT_DIRECTORY_RELEASE             "${OUTPUT_DIR}"
    RUNTIME_OUTPUT_DIRECTORY_RELEASE             "${OUTPUT_DIR}"
    PDB_OUTPUT_DIRECTORY_RELEASE               "${OUTPUT_DIR}"
)
  • ConfigQt.cmake
#-------------------------------------------------------------------------------
set(CFG_QT_VERSION 5.15.1)
if(WIN32)
    set(QT_INSTALL_PATH "C:/Qt/${CFG_QT_VERSION}")
else()
    set(QT_INSTALL_PATH "/Applications/Qt${CFG_QT_VERSION}/${CFG_QT_VERSION}")
endif()

#-------------------------------------------------------------------------------
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

#-------------------------------------------------------------------------------
if((NOT QT_DIR) AND (NOT IS_DIRECTORY "${QT_DIR}"))
    if(WIN32)
        if(${CMAKE_CL_64})
            set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${QT_INSTALL_PATH}/msvc2019_64")
        else()
            set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${QT_INSTALL_PATH}/msvc2019")
        endif()
    elseif(APPLE)
        set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${QT_INSTALL_PATH}/clang_64")
    endif()
endif()
  • 主要CMakeLists.txt
# CMakeList.txt: 顶层 CMake 项目文件,在此处执行全局配置
# 并包含子项目。
################################## 1、最低CMake版本
cmake_minimum_required (VERSION 3.8)

set(PROJECT_NAME  FrameWork)

project (${PROJECT_NAME})


################################# 2、项目名qtCmake
project(${PROJECT_NAME} LANGUAGES CXX)

################################# 3、set模块
set(CMAKE_INCLUDE_CURRENT_DIR ON)
##################系统设置
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(SYSTEM_NAME Win32)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    set(SYSTEM_NAME MacOS)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(SYSTEM_NAME Linux)
endif()
message(STATUS "[${PROJECT_NAME}] " System:${SYSTEM_NAME})
##################### 操作平台设置
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(BUILD_PLATFORM x64)
else()
    set(BUILD_PLATFORM x86)
endif()
message(STATUS "[${PROJECT_NAME}] BUILD_PLATFORM:${BUILD_PLATFORM}")

# default RelWithDebInfo
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()
message(STATUS "[${PROJECT_NAME}] BUILD_TYPE:${CMAKE_BUILD_TYPE}")

##############设置路径变量参数
set(PREFIX_DIR "Build/${SYSTEM_NAME}/${BUILD_PLATFORM}/${CMAKE_BUILD_TYPE}")

message(STATUS "[${PROJECT_NAME}] PREFIX_DIR:${PREFIX_DIR}")
#######################Qt 元对象设置

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)



#######################添加配置文件
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)

find_package(Qt5  COMPONENTS Widgets Core Gui  Quick QuickWidgets REQUIRED)

################配置C++环境
include(ConfigCXX)

include(ConfigQt)

# 设置exe生成路径。喜欢用CMAKE_CURRENT_SOURCE_DIR,指CMakeLists.txt所在的路径
# set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/)
# 设置c++语言支持
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ################################# 4、查找依赖的Qt文件
# find_package(Qt5 COMPONENTS Widgets Core Gui REQUIRED)    

# #########################包含当前目录
# set(CMAKE_INCLUDE_CURRENT_DIR ON)

message(STATUS "PROJECT_SOURCE_DIR:"${PROJECT_SOURCE_DIR})



# ################################添加子项目文件

add_subdirectory(Base)

add_subdirectory(UI)

add_subdirectory(MainWindow)





# #################################DependsOn  依赖
 add_dependencies(UI Base)
 add_dependencies(MainWindow UI Base)




  • 子CMakeLists.txt
cmake_minimum_required (VERSION 3.8)

project ("UI")
#######################���������ļ�
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/../CMake)



set(CMAKE_INCLUDE_CURRENT_DIR ON)

################################# 4������������Qt�ļ�
find_package(Qt5  COMPONENTS Widgets Core Gui  Quick QuickWidgets REQUIRED)   




set(INC_LIST 
	 ./inc/Export_Dll.h
     ./inc/BaseWidget.h
	 ./inc/FramelessWindow.hpp
	 ./inc/BaseQmlWidget.h
	 
	 )


set(SRC_LIST 
 ./src/BaseWidget.cpp
 ./src/BaseQmlWidget.cpp
 
 )

set(RES_LIST 
	./res/Control.qrc
	./res/qml.qrc
)

include_directories(${PROJECT_SOURCE_DIR}/inc)

# include_directories(${PROJECT_SOURCE_DIR}/../Base/inc)
add_definitions(-DDLL_EXPORTS)
# add_compile_definitions(DLL_EXPORTS)


add_library(${PROJECT_NAME} SHARED ${INC_LIST}  ${SRC_LIST} ${RES_LIST})

target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/../Base/inc)

include(configDll)

target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core 
	Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Widgets  Qt${QT_VERSION_MAJOR}::Quick  Qt${QT_VERSION_MAJOR}::QuickWidgets)



target_link_directories(${PROJECT_NAME} PRIVATE ${PROJECT_SOURCE_DIR}/../${PREFIX_DIR})


target_link_libraries(${PROJECT_NAME} PRIVATE Base.lib)


  • 该文件供使用Cmake 搭建Qt项目框架

你可能感兴趣的:(QT学习,qt,开发语言)