CMake语法—内置变量
1 CMake变量分类
1.1 普通变量
详情可参见随笔《普通变量与函数》、《普通变量与子目录》、《普通变量与包含、宏》
1.2 缓存变量
详情可参见随笔《缓存变量》
1.3 环境变量
详情可参见随笔《环境变量》
1.4 内置变量
2 CMake内置变量分类
2.1 提供信息的变量
详情可参见官方文档《Variables that Provide Information》
2.2 改变行为的变量
详情可参见官方文档《Variables that Change Behavior》
2.3 描述系统的变量
详情可参见官方文档《Variables that Describe the System》
2.4 控制构建的变量
详情可参见官方文档《Variables that Control the Build》
2.5 语言变量
详情可参见官方文档《Variables for Languages》
2.6 CTest变量
详情可参见官方文档《Variables for CTest》
2.7 CPack变量
详情可参见官方文档《Variables for CPack》
2.8 内部变量(不推荐再使用)
CMake还有很多内部变量,他们中的大多数都无正式文档记录。
然而,其中一些变量在某些时候被描述为普通变量,因此可能会在遗留代码中遇到。
它们意思可能已经发生变化,不建议在项目代码中使用。
详情可参见官方文档《Internal Variables》
3 示例代码
3.1 代码结构
-
learn_cmake:为根目录
-
build:为CMake配置输出目录(在此例中即生成sln解决方案的地方)
-
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
-
CMakeLists.txt:CMake主脚本
3.2 代码
下面打印部分内置变量的值,示例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_INTERNAL_VAR")
## 1 Variables that Provide Information
message(STATUS "1 CMAKE_CACHE_MAJOR_VERSION: ${CMAKE_CACHE_MAJOR_VERSION}")
message(STATUS "1 CMAKE_CACHE_MINOR_VERSION: ${CMAKE_CACHE_MINOR_VERSION}")
message(STATUS "1 CMAKE_CACHE_PATCH_VERSION: ${CMAKE_CACHE_PATCH_VERSION}")
message(STATUS "1 CMAKE_CURRENT_LIST_DIR: ${CMAKE_CURRENT_LIST_DIR}")
message(STATUS "1 CMAKE_VS_PLATFORM_TOOLSET: ${CMAKE_VS_PLATFORM_TOOLSET}")
message(STATUS "1 CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}\n")
## 2 Variables that Change Behavior
message(STATUS "2 CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "2 CMAKE_FIND_ROOT_PATH: ${CMAKE_FIND_ROOT_PATH}")
message(STATUS "2 CMAKE_INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}")
message(STATUS "2 CMAKE_SYSTEM_INCLUDE_PATH: ${CMAKE_SYSTEM_INCLUDE_PATH}\n")
## 3 Variables that Describe the System
message(STATUS "3 MSVC: ${MSVC}")
message(STATUS "3 CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message(STATUS "3 MSVC_VERSION: ${MSVC_VERSION}")
message(STATUS "3 MSVC_TOOLSET_VERSION: ${MSVC_TOOLSET_VERSION}\n")
## 4 Variables that Control the Build
message(STATUS "4 CMAKE_AUTOMOC: ${CMAKE_AUTOMOC}")
message(STATUS "4 CMAKE_DEFAULT_BUILD_TYPE: ${CMAKE_DEFAULT_BUILD_TYPE}")
message(STATUS "4 CMAKE_LIBRARY_OUTPUT_DIRECTORY: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
message(STATUS "4 CMAKE_INCLUDE_CURRENT_DIR: ${CMAKE_INCLUDE_CURRENT_DIR}")
message(STATUS "4 CMAKE_MSVCIDE_RUN_PATH: ${CMAKE_MSVCIDE_RUN_PATH}\n")
## 5 Variables for Languages
message(STATUS "5 CMAKE_C_STANDARD: ${CMAKE_C_STANDARD}")
message(STATUS "5 CMAKE_CXX_STANDARD: ${CMAKE_CXX_STANDARD}")
message(STATUS "5 CMAKE_COMPILER_IS_GNUCC: ${CMAKE_COMPILER_IS_GNUCC}")
# 打印结束日志
message(STATUS "########## END_TEST_INTERNAL_VAR\n")
3.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_INTERNAL_VAR
-- 1 CMAKE_CACHE_MAJOR_VERSION:
-- 1 CMAKE_CACHE_MINOR_VERSION:
-- 1 CMAKE_CACHE_PATCH_VERSION:
-- 1 CMAKE_CURRENT_LIST_DIR: F:/learn_cmake
-- 1 CMAKE_VS_PLATFORM_TOOLSET: v140
-- 1 CMAKE_CURRENT_SOURCE_DIR: F:/learn_cmake
-- 2 CMAKE_BUILD_TYPE:
-- 2 CMAKE_FIND_ROOT_PATH:
-- 2 CMAKE_INCLUDE_PATH:
-- 2 CMAKE_SYSTEM_INCLUDE_PATH:
-- 3 MSVC: 1
-- 3 CMAKE_SYSTEM_NAME: Windows
-- 3 MSVC_VERSION: 1900
-- 3 MSVC_TOOLSET_VERSION: 140
-- 4 CMAKE_AUTOMOC:
-- 4 CMAKE_DEFAULT_BUILD_TYPE:
-- 4 CMAKE_LIBRARY_OUTPUT_DIRECTORY:
-- 4 CMAKE_INCLUDE_CURRENT_DIR:
-- 4 CMAKE_MSVCIDE_RUN_PATH:
-- 5 CMAKE_C_STANDARD:
-- 5 CMAKE_CXX_STANDARD:
-- 5 CMAKE_COMPILER_IS_GNUCC:
-- ########## END_TEST_INTERNAL_VAR
-- Configuring done
-- Generating done
-- Build files have been written to: F:/learn_cmake/build
请按任意键继续. . .