Basic 06-build-type
# 介绍 CMake
CMake 有许多内置的定义的构建配置组合,可以用于帮助编译项目。这些配置可以用来指定了优化级别 ( -O0 -O1 -O2 ),以及是否在二进制文件中的包含调试信息 (等价于 GCC 中的 -g )。
(当然你也可以单独指定某个编译选项,下一节中介绍)
CMake 提供以下几种构建类型:
Release (发行版本): 添加
-O3和-DNDEBUG标志
-DNDEBUG告诉编译器去除所有的assert()语句Debug (构建调试版本): 添加
-g标志
在目标文件中加入调试需要的信息供调试器 (如 gdb) 使用MinSizeRel (最小化版本): 添加
-O2、-g和-DNDEBUG标志
用于最小尺寸优化代码并去除调试信息RelWithDebInfo (奇奇怪怪的组合👀): 添加
-O2、-g和-DNDEBUG标志
用于带有调试信息的速度和尺寸优化代码。
本例的目录🌲:
.
├── CMakeLists.txt
├── main.cpp
1
2
3
2
3
# 概念解释
# 给构建指定构建类型
在构建时传入:
cmake .. -DCMAKE_BUILD_TYPE=Release
1
# 修改默认构建类型
默认的 CMake 构建类型不带有任何的 compiler flags。对于某些工程,可能需要修改配置模型构建类型。
修改方法举例:通过在 CMakeLists.txt 中加入下面的代码将模式的构建类型改为 RelWithDebInfo :
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message("Setting build type to 'RelWithDebInfo' as none was specified.")
set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif()
1
2
3
4
5
6
7
2
3
4
5
6
7
# 构建本例
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/lib/ccache/cc
-- Check for working C compiler: /usr/lib/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build
$ make VERBOSE=1 # 重点看编译器用了-O3 -DNDEBUG
/usr/bin/cmake -S/home/tartarus/cmake/cmake-examples/01-basic/F-build-type -B/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/depend
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
cd /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/tartarus/cmake/cmake-examples/01-basic/F-build-type /home/tartarus/cmake/cmake-examples/01-basic/F-build-type /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake --color=
Dependee "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal".
Dependee "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal".
Scanning dependencies of target cmake_examples_build_type
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/build
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
[ 50%] Building CXX object CMakeFiles/cmake_examples_build_type.dir/main.cpp.o
/usr/lib/ccache/c++ -O3 -DNDEBUG -o CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -c /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/main.cpp
[100%] Linking CXX executable cmake_examples_build_type
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_build_type.dir/link.txt --verbose=1
/usr/lib/ccache/c++ -O3 -DNDEBUG CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -o cmake_examples_build_type
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
[100%] Built target cmake_examples_build_type
make[1]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles 0
$ rm -rf * # 重新使用CMakeLists指定的方法进行构建
$ cmake ..
Setting build type to 'RelWithDebInfo' as none was specified.
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/lib/ccache/cc
-- Check for working C compiler: /usr/lib/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build
$ make VERBOSE=1 # 重点看编译器使用了-O2 -g -DNDEBUG编译选项
/usr/bin/cmake -S/home/tartarus/cmake/cmake-examples/01-basic/F-build-type -B/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/depend
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
cd /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/tartarus/cmake/cmake-examples/01-basic/F-build-type /home/tartarus/cmake/cmake-examples/01-basic/F-build-type /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake --color=
Dependee "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/DependInfo.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal".
Dependee "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles/cmake_examples_build_type.dir/depend.internal".
Scanning dependencies of target cmake_examples_build_type
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
make -f CMakeFiles/cmake_examples_build_type.dir/build.make CMakeFiles/cmake_examples_build_type.dir/build
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
[ 50%] Building CXX object CMakeFiles/cmake_examples_build_type.dir/main.cpp.o
/usr/lib/ccache/c++ -O2 -g -DNDEBUG -o CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -c /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/main.cpp
[100%] Linking CXX executable cmake_examples_build_type
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmake_examples_build_type.dir/link.txt --verbose=1
/usr/lib/ccache/c++ -O2 -g -DNDEBUG CMakeFiles/cmake_examples_build_type.dir/main.cpp.o -o cmake_examples_build_type
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
[100%] Built target cmake_examples_build_type
make[1]: Leaving directory '/home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build'
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/01-basic/F-build-type/build/CMakeFiles 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
上次更新: 12/27/2023, 8:55:47 AM