当前位置:   article > 正文

cmake 使用include 分层加载_add_includes

add_includes

include命令
说到cmake,可能最先想到的就是CmakeLists.txt文件,但是在很多情况下,也会看到.cmake文件。
cmake文件是干什么的,甚至会想.cmake文件是不是cmake的正统文件,而CmakeLists.txt并不是。但其实,CmakeLists.txt才是cmake的正统文件,而.cmake文件是一个模块文件,可以被include到CMakeLists.txt中。

include指令一般用于语句的复用,也就是说,如果有一些语句需要在很多CMakeLists.txt文件中使用,为避免重复编写,可以将其写在.cmake文件中,然后在需要的CMakeLists.txt文件中进行include操作就行了。

include指令的结构为:
include(<file|module> [OPTIONAL] [RESULT_VARIABLE ] [NO_POLICY_SCOPE])
注意:
为了使CMakeLists.txt能够找到该文件,需要指定文件完整路径(绝对路径或相对路径),当然如果指定了CMAKE_MODULE_PATH,就可以直接include该目录下的.cmake文件了。

举例说明:
在这里插入图片描述

默认的cmakelists

#工程名
project(uXXXXSoftware)

#cmake最低版本
cmake_minimum_required(VERSION 3.17)

#设定cmake模块的路径
set(CMAKE_MODULE_PATH
        ${PROJECT_SOURCE_DIR}/cmake # 项目cmake
        ${PROJECT_SOURCE_DIR}/cmake/base # 通用cmake
        ${CMAKE_MODULE_PATH})

#加载配置项
include(set_env)

#加载项目配置
include(init_project)

#加载编译配置
include(set_compile_arg)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

base/set_env.cmake

message(STATUS "====================set_env.cmake===================")

# 处理编译字符集问题
add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

#C++标准
set(CMAKE_CXX_STANDARD 11)

# 开启QT用于预处理的组件
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

#设置全局系统变量
set(PROJECT_EXTERNAL_DIR "D:/MyLib")

# 设置webview2变量
set(wixlabrary_INCLUDE_DIRS "${PROJECT_EXTERNAL_DIR}/include/external/WixLibrary")
set(webview2_INCLUDE_DIRS "${PROJECT_EXTERNAL_DIR}/include/external/WebView2")
set(webview2_LIBS "${PROJECT_EXTERNAL_DIR}/lib/external")
message(STATUS "wixlabrary_INCLUDE_DIRS is ${wixlabrary_INCLUDE_DIRS}")
message(STATUS "webview2_INCLUDE_DIRS is ${webview2_INCLUDE_DIRS}")
message(STATUS "webview2_LIBS is ${webview2_LIBS}")

#配置QT位置
message(STATUS "CMAKE_SYSTEM_NAME is ${CMAKE_SYSTEM_NAME}")
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    #下面是Windows下Qt的默认前缀
    set(CMAKE_PREFIX_PATH "D:/Library/Qt5.14.2/5.14.2/msvc2017_64/lib/cmake")
elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" AND CMAKE_SYSTEM_NAME MATCHES "Linux")
    #下面是aarch64 Linux下Qt的默认前缀
    set(CMAKE_PREFIX_PATH /opt/Qt5.14.2_aarch64/)
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    #下面是Linux x64下Qt的默认前缀
    set(CMAKE_PREFIX_PATH /opt/Qt5.14.2/5.14.2/gcc_64)
endif ()
message(STATUS "====================set_env.cmake end===================")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

base/init_project.cmake

# 查找QT的模块
find_package(Qt5 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

#加载webview2依赖的头文件
include_directories(SYSTEM ${wixlabrary_INCLUDE_DIRS} ${webview2_INCLUDE_DIRS})

#设置要搜索库文件的路径
link_directories(${webview2_LIBS})

# 把目录Packages/MainFrame/src下面的所有文件作为变量存储
aux_source_directory(Packages/MainFrame/src SOURCES)
# 添加源文件
add_executable(uChangAnSoftware ${SOURCES})

# 添加模块
target_link_libraries(uChangAnSoftware
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        WebView2LoaderStatic
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

set_compile_arg.cmake

# 默认的编译配置(可选)
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/292945
推荐阅读
相关标签
  

闽ICP备14008679号