1)将单个源码文件编译为可执行文件
2)切换生成器
3)构建和连接静态库与动态库
4)用条件语句控制编译
5)向用户显示选项
6)指定编译器
7)切换构建类型
8)设置编译器选项
9)为语言设定标准
10)使用控制流进行构造
#include
#include char *say_hello() { return "Hello, CMake world!"; }int main() {printf("%s\n", say_hello());return EXIT_SUCCESS;
}
备注:将该文件与源文件 hello-world.cpp 放在相同的目录中。记住,它只能被命名为 CMakeLists.txt 。
# set minimum cmake version
//设置CMake所需的最低版本。如果使用的CMake版本低于该版本,则会发出致命错误:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)# project name and language
//声明了项目的名称( recipe-01 )和支持的编程语言(CXX代表C++,C代表C):
project(recipe-01 LANGUAGES C)//指示CMake创建一个新目标:可执行文件 hello-world 。
//这个可执行文件是通过编译和链接源文件 hello-world.cpp 生成的。
add_executable(hello-world hello-world.c)
1. $ mkdir -p build
2. $ cd build
3. $ cmake ..
4.
5. -- The CXX compiler identification is GNU 8.1.0
6. -- Check for working CXX compiler: /usr/bin/c++
7. -- Check for working CXX compiler: /usr/bin/c++ -- works
8. -- Detecting CXX compiler ABI info
9. -- Detecting CXX compiler ABI info - done
10. -- Detecting CXX compile features
11. -- Detecting CXX compile features - done
12. -- Configuring done
13. -- Generating done
14.
-- Build files have been written to: /home/user/cmake-cookbook/chapter01/recipe-01/cxx-example/build
①Makefile : make 将运行指令来构建项目。
②CMakefile :包含临时文件的目录,CMake用于检测操作系统、编译器等。此外,根据所选的生成器,它还包含特定的文件。
③cmake_install.cmake :处理安装规则的CMake脚本,在项目安装时使用。
④CMakeCache.txt :如文件名所示,CMake缓存。CMake在重新运行配置时使用这个文件。
③命令实现编译
$ cmake -H. -Bbuild
该命令是跨平台的,使用了 -H 和 -B 为CLI选项。 -H 表示当前目录中搜索
根 CMakeLists.txt 文件。 -Bbuild 告诉CMake在一个名为 build 的目录中生成所有的文件。
④CMake不强制指定构建目录执行名称或位置,我们完全可以把它放在项目路径之外。这样做同样有效:
1. $ mkdir -p /tmp/someplace
2. $ cd /tmp/someplace
3. $ cmake /path/to/source
4. $ cmake --build .
⑤查看cmake帮助
1. $ cmake --build . --target help
2.
3. The following are some of the valid targets for this Makefile:
4. ... all (the default if no target is provided)
5. ... clean
6. ... depend
7. ... rebuild_cache
8. ... hello-world
9. ... edit_cache
10. ... hello-world.o
11. ... hello-world.i
12. ... hello-world.s
定义
CMake是一个构建系统生成器,可以使用单个CMakeLists.txt为不同平台上的不同工具集配置项目。您可以在CMakeLists.txt中描述构建系统必须运行的操作,以配置并编译代码。基于这些指令,CMake将为所选的构建系统(Unix Makefile、Ninja、Visual Studio等等)生成相应的指令
解释
我们将重用前一节示例中的 hello-world.cpp 和 CMakeLists.txt 。惟一的区别在使用CMake时,因为现在必须显式地使用命令行方式,用 -G 切换生成器。
举例
---------------------配置项目
1. $ mkdir -p build
2. $ cd build
3. $ cmake -G Ninja ..
4.
5. -- The CXX compiler identification is GNU 8.1.0
6. -- Check for working CXX compiler: /usr/bin/c++
7. -- Check for working CXX compiler: /usr/bin/c++ -- works
8. -- Detecting CXX compiler ABI info
9. -- Detecting CXX compiler ABI info - done
10. -- Detecting CXX compile features
11. -- Detecting CXX compile features - done
12. -- Configuring done
13. -- Generating done
14.
-- Build files have been written to: /home/user/cmake-cookbook/chapter01/recipe-02/cxx-exampl---------------------构建项目
1. $ cmake --build .
2.
3. [2/2] Linking CXX executable hello-world
①build.ninja 和 rules.ninja :包含Ninja的所有的构建语句和构建规则。
②CMakeCache.txt :CMake会在这个文件中进行缓存,与生成器无关。
③CMakeFiles :包含由CMake在配置期间生成的临时文件。
④cmake_install.cmake :CMake脚本处理安装规则,并在安装时使用。
①:CMake中,C++是默认的编程语言。不过,我们还是建议使用 LANGUAGES
选项在 project 命令中显式地声明项目的语言
project(recipe-01 LANGUAGES CXX)
②涉及到的文件分类
①Makefile : make 将运行指令来构建项目。
②CMakefile :包含临时文件的目录,CMake用于检测操作系统、编译器等。此外,根据所选的生成器,它还包含特定的文件。
③cmake_install.cmake :处理安装规则的CMake脚本,在项目安装时使用。
④CMakeCache.txt :如文件名所示,CMake缓存。CMake在重新运行配置时使用这个文件。
③命令实现编译
$ cmake -H. -Bbuild
该命令是跨平台的,使用了 -H 和 -B 为CLI选项。 -H 表示当前目录中搜索
根 CMakeLists.txt 文件。 -Bbuild 告诉CMake在一个名为 build 的目录中生成所有的文件。
④CMake不强制指定构建目录执行名称或位置,我们完全可以把它放在项目路径之外。这样做同样有效:
1. $ mkdir -p /tmp/someplace
2. $ cd /tmp/someplace
3. $ cmake /path/to/source
4. $ cmake --build .