WRY

Where Are You?
You are on the brave land,
To experience, to remember...

0%

CMake 学习教程

CMake是跨平台、开源构建工具。产出的内容基于具体平台的makefile,可以理解是更高的一层封装

变量

设置自定义变量

可以通过SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])命令来定义变量,通过${NAME}来获取变量内容

1
SET(SRC_LST main.c other.c)

查看当前所有变量

预先设置好的变量可以查看官方文档,此外可以通过如下代码,查看当前系统的预设环境变量,完整演示

1
2
3
4
5
get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "environment")

常见预设变量汇总

  • Variables that Provide Information

    ................variableName................ 含义
    PROJECT_NAME Name of the project given to the project command.
    CMAKE_BINARY_DIR Full path to the top level of the build tree.
    PROJECT_BINARY_DIR Full path to build directory for project.
    <PROJECT-NAME>_BINARY_DIR Top level binary directory for the named project.
    CMAKE_SOURCE_DIR Full path to the top level of the source tree.
    PROJECT_SOURCE_DIR This is the source directory of the last call to the project() command made in the current directory scope or one of its parents.
    <PROJECT-NAME>_SOURCE_DIR Top level source directory for the named project.
    EXECUTABLE_OUTPUT_PATH , LIBRARY_OUTPUT_PATH
  • Variables that Describe the System

    ............variableName............ 含义
    CMAKE_MAJOR_VERSION First version number component of the CMAKE_VERSION variable.
    CMAKE_MINOR_VERSION Second version number component of the CMAKE_VERSION variable.
    CMAKE_PATCH_VERSION Third version number component of the CMAKE_VERSION variable.
    CMAKE_SYSTEM_NAME The name of the operating system for which CMake is to build.
    CMAKE_SYSTEM_VERSION The version of the operating system for which CMake is to build.
    CMAKE_SYSTEM ${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_VERSION}
    CMAKE_SYSTEM_PROCESSOR
    UNIX Set to True when the target system is UNIX or UNIX-like
    WIN32 Set to True when the target system is Windows, including Win64.