本清单提供了对 CMake 的入门简要概述,以及 CMake 常用示例
CMake 是一个用于配置跨平台源代码项目应该如何配置的工具建立在给定的平台上。
├┈ CMakeLists.txt # 希望运行的 CMake 命令
╰┈ main.cpp # 带有 main 的源文件
在此项目上运行 CMake
时,系统会要求您提供二进制目录,运行 CMake
不会创建最终的可执行文件,而是会为 Visual Studio
、XCode
或 makefile
生成项目文件。 使用这些工具构建该项目
# 设置可以使用的最低 CMake 版本
cmake_minimum_required(VERSION 3.5)
# 设置项目名称
project (hello_cmake)
# 添加可执行文件
add_executable(hello_cmake main.cpp)
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello CMake!" << std::endl;
return 0;
}
$ mkdir build # 创建 build 目录
$ cd build # 进入目录
$ cmake .. # 目录的上一级目录运行命令
$ ./hello_cmake # 运行生成的 hello_cmake
Hello CMake!
生成项目构建系统
$ cmake [<options>] <path-to-source | path-to-existing-build>bash
$ cmake [<options>] -S <path-to-source> -B <path-to-build>
建立一个项目
$ cmake --build <dir> [<options>] [-- <build-tool-options>]
安装项目
$ cmake --install <dir> [<options>]
打开一个项目
$ cmake --open <dir>
运行脚本
$ cmake [-D <var>=<value>]... -P <cmake-script-file>
运行命令行工具
$ cmake -E <command> [<options>]
运行查找包工具
$ cmake --find-package [<options>]
运行工作流预设
$ cmake --workflow [<options>]
查看帮助
$ cmake --help[-<topic>]
- CMake Examples (ttroy50.github.io)