CMake语法—打印帮助(CMakePrintHelpers)

CMake语法—打印帮助(CMakePrintHelpers)

1 CMakePrintHelpers

1.1 cmake_print_variable

1.1.1 定义
cmake_print_variables(var1 var2 ..  varN)
1.1.2 说明

打印每个变量值

1.2 cmake_print_properties

1.2.1 定义
cmake_print_properties([TARGETS target1 ..  targetN]
                      [SOURCES source1 .. sourceN]
                      [DIRECTORIES dir1 .. dirN]
                      [TESTS test1 .. testN]
                      [CACHE_ENTRIES entry1 .. entryN]
                      PROPERTIES prop1 .. propN )
1.2.2 说明

打印属性值。支持TARGETS、SOURCES、DIRECTORIES、TESTS、CACHE_ENTRIES的属性打印

特别注意:PROPERTIES关键字不能丢

2 应用示例

2.1 代码结构

目录结构

  • learn_cmake:为根目录

  • build:为CMake配置输出目录(在此例中即生成sln解决方案的地方)

  • CMakeLists.txt:CMake脚本

  • cmake_config.bat:执行CMake配置过程的脚本(双击直接运行)

    @echo off
    set currentDir=%~dp0
    set buildDir=%currentDir%
    set cmakeOutputDir=%currentDir%\build
    cmake -S %buildDir% -B %cmakeOutputDir% -G"Visual Studio 16 2019" -T v140 -A x64
    pause
    
  • src:工程源文件目录

  • main.cpp:源文件

2.2 代码内容

  1. CMakeLists.txt

    cmake_minimum_required(VERSION 3.18)
    
    # 设置工程名称
    set(PROJECT_NAME KAIZEN)
    
    # 设置工程版本号
    set(PROJECT_VERSION "1.0.0.10" CACHE STRING "默认版本号")
    
    # 工程定义
    project(${PROJECT_NAME}
        LANGUAGES CXX C
        VERSION ${PROJECT_VERSION}
    )
    
    # 打印开始日志
    message(STATUS "########## BEGIN_TEST")
    
    include(CMakePrintHelpers)
    
    # 1.cmake_print_variables
    ## 1.1 打印CMAKE内置变量
    cmake_print_variables(CMAKE_VERSION)
    cmake_print_variables(CMAKE_GENERATOR)
    ## 若采用MSVC构建,可直接使用CMAKE_VS_PLATFORM_NAME
    if (MSVC)
        cmake_print_variables(CMAKE_VS_PLATFORM_NAME)
    endif()
    
    ## 1.2 打印普通变量
    cmake_print_variables(PROJECT_NAME)
    cmake_print_variables(${PROJECT_NAME})
    # 若采用Ninja构建,没有CMAKE_VS_PLATFORM_NAME,可通过CMAKE_SIZEOF_VOID_P变通处理
    if (CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(${PROJECT_NAME}_PLATFORM_NAME "x64")
    else()
        set(${PROJECT_NAME}_PLATFORM_NAME "Win32")
    endif()
    
    cmake_print_variables(${PROJECT_NAME}_PLATFORM_NAME)
    
    ## 1.3 打印缓存变量
    cmake_print_variables(PROJECT_VERSION)
    
    ## 1.4 同时打印多个变量
    set(var_1 "var1")
    set(var_2 "var2")
    cmake_print_variables(var_1 var_2)
    
    # 2.cmake_print_properties
    
    ## 2.1 打印属性
    
    set(TARGET_NAME component)
    
    set(${TARGET_NAME}_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
    
    # 提取所有源文件(include和src分开提取)
    file(GLOB_RECURSE ${TARGET_NAME}_HEADER_FILES
        LIST_DIRECTORIES False CONFIGURE_DEPENDS
        "${${TARGET_NAME}_SOURCE_DIR}/include/*.h*"
    )
    file(GLOB_RECURSE ${TARGET_NAME}_SRC_FILES
        LIST_DIRECTORIES False CONFIGURE_DEPENDS
        "${${TARGET_NAME}_SOURCE_DIR}/src/*.h*"
        "${${TARGET_NAME}_SOURCE_DIR}/src/*.c*"
    )
    
    # 为VS设置源代码文件夹
    source_group(
        TREE "${${TARGET_NAME}_SOURCE_DIR}/include"
        PREFIX "Header Files"
        FILES ${${TARGET_NAME}_HEADER_FILES}
    )
    source_group(
        TREE "${${TARGET_NAME}_SOURCE_DIR}/src/"
        PREFIX "Source Files" 
        FILES ${${TARGET_NAME}_SRC_FILES}
    )
    
    # 添加target
    add_library(${TARGET_NAME} SHARED)
    
    # 指定源文件
    target_sources(${TARGET_NAME}
        PRIVATE ${${TARGET_NAME}_SRC_FILES}
        PRIVATE ${${TARGET_NAME}_HEADER_FILES}
    )
    
    # 设置target属性
    set_target_properties(${TARGET_NAME} PROPERTIES
        WINDOWS_EXPORT_ALL_SYMBOLS  True  ##自动导出符号
        FOLDER  "libs"  ##设置VS路径
    )
    
    cmake_print_properties(TARGETS ${TARGET_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS FOLDER)
    
    # 打印结束日志
    message(STATUS "########## END_TEST\n")
    
  2. main.cpp

    int main()
    {
        return 0;
    }
    

    简单示例,此文件仅仅为了创建Target的完整性。

2.3 输出结果

  • 运行结果

    -- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.17763.
    -- The CXX compiler identification is MSVC 19.0.24245.0
    -- The C compiler identification is MSVC 19.0.24245.0
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- ########## BEGIN_TEST
    -- CMAKE_VERSION="3.18.2"
    -- CMAKE_GENERATOR="Visual Studio 16 2019"
    -- CMAKE_VS_PLATFORM_NAME="x64"
    -- PROJECT_NAME="KAIZEN"
    -- KAIZEN=""
    -- KAIZEN_PLATFORM_NAME="x64"
    -- PROJECT_VERSION="1.0.0.10"
    -- var_1="var1" ; var_2="var2"
    --
     Properties for TARGET component:
       component.WINDOWS_EXPORT_ALL_SYMBOLS = "True"
       component.FOLDER = "libs"
    
    -- ########## END_TEST
    
    -- Configuring done
    -- Generating done
    -- Build files have been written to: F:/learn_cmake/build
    请按任意键继续. . .
    

2.4 注意事项

2.4.1 使用前必须引入CMakePrintHelpers

使用两者前提必须引入CMakePrintHelpers,详见示例CMakeLists.txt代码第18行。

2.4.2 打印属性

本示例仅提供了TARGETS的属性打印,也是最常用之一。其余支持的属性,后期使用到会穿插在示例中演示。

特殊说明:示例CMakeLists.txt代码第53行~93行即定义一个Target对象的常规必备项内容,后期会专门分析TARGET内容。

在此只要记住,target必须是由add_executable或者add_library(如示例CMakeLists.txt中第81行)生成即可。

posted @ 2022-01-13 23:25  kaizenly  阅读(1557)  评论(0编辑  收藏  举报
打赏