变量简介和常用变量¶
在一个CMakeLists.txt文件中定义的变量, 会传递给其通过add_subdirectory或include调用的子项目或脚本.
如果在子项目或脚本中定义了同名的变量, 按照同名覆盖的原则, 局部变量有效.
cmake变量有两种定义方式:
- 在
CMakeLists.txt或*.cmake脚本中, 使用set指令定义;- 在命令行中, 以
-D开头定义, 后接VAR=VALUE的方式定义.
Variables That Change Behavior¶
BUILD_SHARED_LIBSGlobal flag to cause
add_libraryto create shared libraries if on.If present and true, this will cause all libraries to be built shared unless the library was explicitly added as a static library. This variable is often added to projects as an Option so that each user of a project can decide if they want to build the project using shared or static libraries.
CMAKE_BUILD_TYPESpecifies the build type for make based generators.
This specifies what build type will be built in this tree. Possible values are
empty,Debug,Release,RelWithDebInfoandMinSizeRel. This variable is only supported for make based generators.注解
常用的构建类型是:
Debug/Release.CMAKE_INSTALL_PRRFIXInstall directory used by install.
Variables That Describe System¶
CMAKE_HOST_SYSTEMName of system CMake is running on.
CMAKE_HOST_SYSTEM_NAMEName of the OS CMake is running on.
CMAKE_HOST_SYSTEM_PROCESSORThe name of the CPU CMake is running on.
CMAKE_HOST_STSTEM_VERSIONOS version CMake is running on.
CMAKE_HOST_UNIXTrue for UNIX and UNIX like operating system.
CMAKE_HOST_WIN32True on windows systems, including win64.
CMAKE_SYSTEM目标机器的系统.
CMAKE_SYSTEM_NAME目标机器的系统名.
CMAKE_SYSTEM_PROCESSOR目标机器的CPU类型.
CMAKE_SYSTEM_VERSION目标系统的版本.
UNIXTrue 如果目标系统是Unix类Unix-like系统.
Win32True 如果目标系统是Windows系统.
Variables That Control the Build¶
CMAKE_ARCHIVE_OUTPUT_DIRECTORYWhere to put all
ARCHIVEtargets when build.CMAKE_LIBRARY_OUTPUT_DIRECTORYWhere to put all the
LIBRARYtargets when build.CMAKE_RUNTIME_OUTPUT_DIRECTORYWhere to put all teh
RUNTIMEtargets when built.CMAKE_LIBRARY_PATH_FLAGThe flag used to add a library search path to a compiler. The flag used to specify a library directory to the compiler. On most compilers this is
-L.CMAKE_LINK_LIBRARY_FLAGFlag used to link a library into an executable. The flag used to specify a library to link to an executable. On most compilers this is
-l;CMAKE_INCLUDE_CURRENT_DIRAutomaticallly add the current source and build directories to the include path. If this variable is enables, CMake automatically adds in each directory
${CMAKE_CURRENT_SOURCE_DIR}and${CMAKE_CURRENT_BINARY_DIR}to the include path for this directory. These addtional include directories do not propagate to subdirectories. This is useful mainly for out-of-source builds, where files generated into the build tree are included by files located in the source tree. By defaultCMAKE_INCLUDE_CURRENT_DIRis OFF.
Variables That Provide Information¶
Variables defined by CMake, that give information about the project, and CMake.
CMAKE_PROJECT_NAME- The name of the current project.注解
指 top-level project 的项目名称;
如果项目中包含 sub project, 在 sub project 的 CMakeLists.txt 中指的也是 top-level project 的项目名.
PROJECT_NAME- Name of the project given to the project comamnd.指当前
CMakeLists.txt所对应的项目的名称:- 如果在
top-level的CMakeLists.txt中定义, 指top-level project的项目名; - 如果在
sub project的CMakeLists.txt中定义, 指sub project的项目名.
- 如果在
CMAKE_SOURCE_DIR- The path to the top level of the source tree.指
top-level project的源码目录, 即使项目中包含sub project, 在sub project的CMakeLists.txt中指的也是top-level project的源码目录.PROJECT_SOURCE_DIR- Top level source directory for the current project.指当前
CMakeLists.txt所对应的项目的源码目录:- 如果在
top-level project的CMakeLists.txt中定义, 指的是top-level project的源码目录; - 如果在
sub project的CMakeLists.txt中定义, 指的是sub project的源码目录.
- 如果在
CMAKE_CURRENT_SOURCE_DIR- The path to the source directory currently being processed.指cmake当前正在处理的项目的源码目录:
- 在 top-level project 的 CMakeLists.txt 中指 top-level project 的源码目录;
- 在 sub project 的 CMakeLists.txt 中指 sub project 的源码目录;
[Project_name]_SOURCE_DIR- Top level source directory for the named project.A variable is created with the name used in the
projectcommand, and is the source directory for the project. This can be useful whenadd_subdirectoryis used to connect several projects.CMAKE_BINARY_DIR- The path to the top level of the build tree.指顶层项目的构建目录. 如果顶层项目中包含子项目, 即使在子项目中也是指顶层项目的构建目录.
PROJECT_BINARY_DIR- Full path to build directory for project.指当前项目的构建目录:
- 在
top-level project中, 指top-level project的构建目录; - 在
sub project中, 指sub project的构建目录.
- 在
CMAKE_CURRENT_BINARY_DIR- The path to the binary directory currently being processed.This is the full path to the build directory that is being process by cmake. Each directory added by
add_subdirectorywill create a binary directory in the build tree, and as it is being processed this variable wil be set.[Project_name]_BINARY_DIR- Top level binary directory for the named project.A variable is created with the name used int the
projectcommand, and is the binary directory for the project. This can be useful whenSUBDIRis used to connect several projects.CMAKE_VERBOSE_MAKEFILE- Create verbose Makefiles if on.This variable defaults to false. You can set this variable to true to make CMake produce verbose Makefiles that show each comamnd line as it is used.
CMAKE_VERSION- The full version of cmake in major.minor.patch format.CMAKE_MAJOR_VERSION- The major version of cmake.CMAKE_MINOR_VERSION- The minor version of cmake.CMAKE_PATCH_VERSION- The patch version of cmake.CMAKE_CURRENT_LIST_FILE- 表示定义这个变量的 CMakeLists.txt 的完整路径.CMAKE_CURRENT_LIST_LINE- 表示定义这个变量所在的行.