**************************************CMake用法 cmakelist.txt 与 .cmake的关系 你可以使用include命令来包含它,就像其他语言那样分离源文件和头文件,使他们可以在多个CmakeLists中重用 See https://cmake.org/cmake/help/v3.0/command/include.html 请注意,其实你可以包含任何文件,但是.cmake拓展名是常用的。
CMake 概述
构建系统描述如何 使用构建工具来自动化这个过程,从源代码编译一个工程的可执行文件与库。 例如,构建系统可能是一个Makefile 使用命令行工具make 或者是ide的工程文件 为了避免管理多个构建系统,一个工程可以抽象地用CMAKE语言写的文件 指定他的构建系统。从这些文件,cmake通过叫generator的后端为每个用户产生一个好的构建系统。为了用CMake产生构建系统,下面必须被选择
源码树 The top-level directory containing source files provided by the project. The project specifies its buildsystem using 顶层目录包含工程提供的源代码,工程使用在cmake-language 手册中描述的文件指定它的构建系统 files as described in the cmake-language(7) manual, starting with a top-level file named CMakeLists.txt. These files specify build targets and their dependencies as described in the cmake-buildsystem(7) manual. 开始于顶层叫CMakeLists.txt的文件,这个文件指定了编译的目标和他们的依赖,这些在cmake-buildsystem(7)手册中有描述。
编译树 The top-level directory in which buildsystem files and build output artifacts (e.g. executables and libraries) are to be stored. 系统的文件和编译输出 是存储在顶层的目录, CMake will write a CMakeCache.txt file to identify the directory as a build tree and store persistent information such as buildsystem configuration options. Cmake将写CMakeCache.txt 文件来标识这个目录作为编译树,并存储永久的信息,例如构建系统的配置选项
To maintain a pristine source tree, perform an out-of-source build by using a separate dedicated build tree. 为了管理原始的源码树,通过使用一个分开的专用构建树来执行一个源外的编译 An in-source build in which the build tree is placed in the same directory as the source tree is also supported, but discouraged. 构建树放在和源码树相同的目
产生器
This chooses the kind of buildsystem to generate. See the cmake-generators(7) manual for documentation of all generators. 这个选择哪种构建系统产生,参考cmake-generators(7)手册关于所有的generators Run cmake --help to see a list of generators available locally. Optionally use the -G option below to specify a generator, 运行 cmake --help来看一系列可用的generators。使用-G来指定generator or simply accept the default CMake chooses for the current platform. 或者接受默认的CMake为当前平台选的 When using one of the Command-Line Build Tool Generators CMake expects that the environment needed by the compiler toolchain is already configured in the shell. 当使用基于命令行的工具Generators,CMake期望 编译工具链需要的环境已经配置到shell。 When using one of the IDE Build Tool Generators, no particular environment is needed.
会生成 cmake_install.cmake CMakeCache.txt Makefile
单行注释:使用“#” 多行注释:使用“#[[ ]]”
*****8命令行的option -D :=, -D = Create or update a CMake CACHE entry. //创造或更新一个CMake CACHE 体 *************************88变量***************88888888 &&&&&&&&&&&&&&环境变量 &&&&&&&&&&&&&&CMAKE变量 CMAKE_BINARY_DIR The path to the top level of the build tree. //编译树的顶级路径
CMAKE_PROJECT_NAME The name of the top level project. //顶层项目的名字
PROJECT_SOURCE_DIR This is the source directory of the last call to the project() command //最后调用project() 命令的源目录 ***************8command
CMAKE_INSTALL_PREFIX This variable defaults to /usr/local on UNIX //可以使用它来指定安装目录
//在安裝時刻指定规则来运行 install(); //Specify rules to run at install time 例如 cmake -DCMAKE_INSTALL_PREFIX=/home/username INSTALL(FILES COPYRIGHT README.md DESTINATION share/doc/cmake/hello-world-clear)
这样COPYRIGHT README.md经过make install 后会安装在/home/username/share/doc/cmake/hello-world-clear
&&&&&& add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) Add a subdirectory to the build.//添加一个子目录来编译
The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. source_dir指定了源CMakeLists.txt和code 文件所在的目录, If it is a relative path it will be evaluated with respect to the current directory (the typical usage) 如果是相对路径,它是相对于当前目录的
&&&&&&&project Set the name of the project. 设置工程的名字
&&&&&&&&&&&&set Set a normal, cache, or environment variable to a given value 为普通,cache,环境变量设置给定的值
set(ENV{} []) //设置环境变量
cmake-env-variables
&&&&&&&&&&add_definitions
Add -D define flags to the compilation of source files. //添加-D 定义flags来编译源文件 add_definitions(-DFOO -DBAR …) Adds definitions to the compiler command line for targets in the current directory //在当前目录为目标添加定义到命令行
&&&&&&&&&&& if Conditionally execute a group of commands. 有条件的执行一组命令
&&&&&& include_directories Add include directories to the build. //添加一个目录来编译 include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 …])
Add the given directories to those the compiler uses to search for include files. //添加给定的目录给编译器用来搜寻include 文件 Relative paths are interpreted as relative to the current source directory. 相对路径被解释为相对于当前源目录
&&&&&& add_executable Normal Executables Imported Executables Alias Executables
Add an executable to the project using the specified source files. 使用指定的源文件来添加可执行文件到工程
&&&&&&include 从文件或模块来 装载或运行CMake 代码 Load and run CMake code from a file or module. include(<file|module> [OPTIONAL] [RESULT_VARIABLE ] [NO_POLICY_SCOPE] if a module is specified instead of a file, the file with name .cmake is 如果module被指定而不是文件,名字为.cmake的文件首先被搜索,然后是CMAKE 模块目录 searched first in CMAKE_MODULE_PATH, then in the CMake module directory.
&&&&&&&&&&&&&file File manipulation command. 文件操作命令
&&&&&&&&&&&&&&&configure_file Copy a file to another location and modify its contents. 复制一个文件到另一个位置,并修改他的内容 Copies an file to an file and substitutes variable values referenced as @VAR@ or ${VAR} in the input file content 复制文件到文件,并替换变量值在输入文件内容中的@VAR@ or ${VAR}
&&&&&&&&&&&&execute_process Execute one or more child processes. 执行一个或多个子进程 COMMAND A child process command line. //子进程的命令行
|