cmake¶
1 生成项目构建系统¶
1.1 -S、-B 指定源目录和构建目录¶
涉及的选项如下:
- `-S <path-to-source>`:要生成的CMake项目的根目录的路径。
- `-B <path-to-build>`:CMake将用作生成目录的根的目录的路径。
通常我们将 cmake 生成的文件放入 build 目录中,方便管理。有 2 类方式确定 build 目录:
方式一(建议):手动创建 build 目录,再执行 cmake+顶级 CMakeLists.txt
所在目录
$ mkdir build ; cd build
$ cmake ../src
方式二:通过 -B build目录
指定构建目录(如果 build 目录不存在会自动创建),-S source目录
$ cmake -S . -B ../build
其中 -B
、-S
可用的组合如下(cwd 表示当前目录):
Command Line | Source Dir | Build Dir |
---|---|---|
cmake src |
src |
cwd |
cmake build (existing) |
loaded | build |
cmake -S src |
src |
cwd |
cmake -S src build |
src |
build |
cmake -S src -B build |
src |
build |
cmake -B build |
cwd | build |
cmake -B build src |
src |
build |
cmake -B build -S src |
src |
build |
1.2 -G 指定构建系统生成器¶
-G <generator-name>
:如Unix Makefiles
。可能的名称在 cmake-Generants 手册中有详细说明。 常见的如:Unix Makefiles
:生成标准的 UNIX makefiles 文件Ninja
:生成 build.ninja 文件
1.3 --install-prefix 指定安装目录¶
--install-prefix <directory>
:指定 CMAKE_INSTALL_PREFIX 变量使用的安装目录。必须是绝对路径。
1.4 --graphviz 生成依赖图¶
- Graphviz 网站
--graphviz=<file>.dot
[shw@localhost build]$ cmake ../common/math --graphviz=1.dot
-- Configuring done
-- Generating done
Generate graphviz: /home/shw/test/code/cmake/build/1.dot
-- Build files have been written to: /home/shw/test/code/cmake/build
[shw@localhost build]$
1.5 --log-level 设置 message 消息级别¶
--log-level=<level>
:Message()
命令将仅输出指定日志级别或更高级别的消息。可选的级别为ERROR
,WARNING
,NOTICE
,STATUS
(default),VERBOSE
,DEBUG
, orTRACE
. 示例
[shw@localhost build]$ cmake ../common/math --log-level=ERROR
-- Configuring done
-- Generating done
-- Build files have been written to: /home/shw/test/code/cmake/build
2 cmake --build¶
cmake --build <dir> [<options>] [-- <build-tool-options>]
功能相当于执行完 cmake 之后,再执行 make(如果是生成的 Makefile 文件)。以下所有选项都可以联系到 make 选项。
[shw@rocky build]$ cmake --build .
[ 50%] Building CXX object CMakeFiles/mymath.dir/mymath.cc.o
[100%] Linking CXX static library libmymath.a
[100%] Built target mymath
2.1 -j 、--parallel¶
-j [<jobs>], --parallel [<jobs>]
:指定编译时使用的进程数,加快编译速度。
[shw@rocky build]$ cmake --build . -j4
[ 50%] Building CXX object CMakeFiles/mymath.dir/mymath.cc.o
[100%] Linking CXX static library libmymath.a
[100%] Built target mymath
2.2 -t、 --target¶
-t <tgt>..., --target <tgt>...
:指定构建的目标,相当于 make 时指定构建哪一个目标。
3 cmake --install¶
cmake --install <dir> [<options>]
用来将构建生成的目标文件安装到指定位置。其中 <dir>
必须指定。
3.1 --prefix¶
--prefix <prefix>
:覆盖安装前缀, 实际上重写了CMAKE_INSTALL_PREFIX
变量。
3.2 --strip¶
--strip
:删除所有符号和重定位信息,可以有效减少二进制文件大小,通常用于 release 版本。gcc -s
选项或者strip -s
选项都可以达到相同目的。实际上 cmake 最终调用的是strip -s
。
$ file perf_test
perf_test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=88c7394ab7d4dbe738fe2dd226ecea2c0d7aa577, for GNU/Linux 3.2.0, not stripped
$ strip -s perf_test
$ file perf_test
perf_test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=88c7394ab7d4dbe738fe2dd226ecea2c0d7aa577, for GNU/Linux 3.2.0, stripped