Basic 01-hello-cmake
# 介绍
这部分主要介绍一个最基本的 CMake 构建过程。
本例的目录🌲:
A-hello-cmake$ tree
.
├── CMakeLists.txt
├── main.cpp
2
3
4
# 概念解释
# CMakeLists.txt
CMakeLists.txt 中包含了你构建和组成工程的全部命令。
# Minimum CMake version
当创建一个 CMake 时,必须首先指定你的 CMakeLists.txt 支持的 CMake 的最低版本。
cmake_minimum_required(VERSION 3.5)
# Projects
一个 CMake 构建可以包括一个项目名称,以便在使用多个项目时更轻松地引用某些变量。通过在 CMake 中指定一个项目名称,可以设置项目的名称并初始化一些有用的变量,如 ${PROJECT_NAME} 和 ${PROJECT_SOURCE_DIR} 。另外,还可以在项目中指定项目的版本号。在这个例子中主版本号为 1,次版本号为 2,修订版本号为 3。
project (hello_cmake VERSION 1.2.3)
# 创建一个可执行文件
add_executable() 命令指定应该从指定的源文件(在本例中为 main.cpp )构建可执行文件。 add_executable() 函数的第一个参数是要构建的可执行文件的名称,第二个参数是要编译的源文件列表。
add_executable(hello_cmake main.cpp)
有些人使用的一种简写方式是将项目名称和可执行文件名称设为相同。这样可以使用以下方式来指定 CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(hello_cmake)
add_executable(${PROJECT_NAME} main.cpp)
2
3
# 二进制目录 (Binary Directory)
运行 cmake 命令的自定义目录或顶级文件夹 (顶级 CMakeLists.txt 存在的文件夹) 被称为你的 CMAKE_BINARY_DIR ,它是所有二进制文件的根目录。 CMake 支持在源文件夹 (顶级文件夹) 和外部文件夹 (你指定的根目录) 构建和生成二进制文件。
# 内部构建
在与源代码相同的目录结构下直接构建会生成很多临时构建文件 (所有的 Makefile 和目标文件), 这些构建文件会与正常的源文件代码混合在一起。
要创建一个原地构建目标,直接在根目录中运行 cmake 命令。
(🧱裂不推荐这样搞!!!!这会搞得源文件文件夹变成一坨💩)
A-hello-cmake$ cmake .
-- 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/A-hello-cmake
A-hello-cmake$ tree
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.16.3
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ ├── CMakeCCompilerId.c
│ │ │ └── tmp
│ │ └── CompilerIdCXX
│ │ ├── a.out
│ │ ├── CMakeCXXCompilerId.cpp
│ │ └── tmp
│ ├── cmake.check_cache
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeOutput.log
│ ├── CMakeTmp
│ ├── hello_cmake.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── DependInfo.cmake
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ └── progress.make
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── main.cpp
├── Makefile
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
# 源文件夹外构建 (Out of Source Build)
外部构建允许创建一个单独的构建文件夹,可以位于文件系统中的任何位置。所有临时构建和对象文件都位于此目录中,使的源代码目录保持清洁。要创建外部构建,在构建文件夹中运行 cmake 命令,并将其指向包含根 CMakeLists.txt 文件的目录。这样做的优点是:如果想要从头开始重新创建 cmake 环境,则只需删除构建目录,然后重新运行 cmake 即可使用外部构建。
A-hello-cmake$ mkdir build
A-hello-cmake$ cd build/
A-hello-cmae/build$ cmake..
-- 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/A-hello-cmake/build
A-hello-cmae/build$ cd ..
A-hello-cmae$ tree
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.16.3
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeOutput.log
│ │ ├── CMakeTmp
│ │ ├── hello_cmake.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ └── progress.make
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ └── Makefile
├── CMakeLists.txt
├── main.cpp
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
# 构建本例
构建这个例子后,就可以直接使用 make 命令运行 (这是在类 Unix 电脑上吧!)。
A-hello-cmake$ cd build
A-hello-cmake$ make
Scanning dependencies of target hello_cmake
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
A-hello-cmake$ ./hello_cmake
Hello CMake!
2
3
4
5
6
7
8
9
10
11