# 前言
案例源代码 (opens new window),每一步都有独立的子目录,其中包含了可能会用到的代码。教程示例是渐进式的, 因此每个步骤都为上一步提供了完整的解决方案。
# step1: 基本起点
# 练习 1 - 构建一个基本项目
目录结构:
\CMAKE-3.26.4-TUTORIAL-SOURCE\STEP1
CMakeLists.txt
tutorial.cxx
TutorialConfig.h.in
1
2
3
4
2
3
4
尽管 CMake 支持大写、小写和混合大小写命令,但首选小写命令,并将在整个教程中使用。
- 任何项目的最顶层 CMakeLists.txt 必须首先使用指定最低 CMake 版本
cmake_minimum_required()
命令。 - 要开始一个项目,我们使用
project()
命令设置项目名称。 add_executable()
命令告诉 CMake 使用指定的源代码文件创建可执行文件。
CMakeLists.txt
:
# TODO 1: Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)
# TODO 2: Create a project named Tutorial
project(Tutorial)
# TODO 7: Set the project version number as 1.0 in the above project command
# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
# and the variable CMAKE_CXX_STANDARD_REQUIRED to True
# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
# TutorialConfig.h
# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx
add_executable(Tutorial tutorial.cxx)
# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
构建并运行:
mkdir Step1_build
cd Step1_build
cmake -G "MinGW Makefiles" ../Step1
cmake --build . 或者 mingw32-make
./Tutorial.exe 10
1
2
3
4
5
2
3
4
5
cmake
在 windows 下默认产生 vs 的项目,所以需要指定产生其他编译器的 makefile ,需要用
-G
指定编译器,可使用命令 cmake -h
查看支持的生成器,如Visual Studio 17 2022
,CodeBlocks - MinGW Makefiles
等。
# 练习 2 - 指定 C++ 标准
CMake 有一些特殊变量,这些变量要么在幕后创建,要么在项目代码设置时对 CMake 有意义。
其中许多变量以 CMAKE_
开头在为您的项目创建变量时避免这种命名约定。
其中两个特殊的用户可设置变量是
CMAKE_CXX_STANDARD
和CMAKE_CXX_STANDARD_REQUIRED
。
这些可以一起使用来指定构建项目所需的 C++ 标准。
编辑 tutorial.cxx
:
//#include <cstdlib> // TODO 5: Remove this line
// TODO 4: Replace atof(argv[1]) with std::stod(argv[1])
//const double inputValue = atof(argv[1]);
const double inputValue = std::stod(argv[1]);
1
2
3
4
5
2
3
4
5
# TODO 1: Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)
# TODO 2: Create a project named Tutorial
project(Tutorial)
# TODO 7: Set the project version number as 1.0 in the above project command
# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
# and the variable CMAKE_CXX_STANDARD_REQUIRED to True
# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
# TutorialConfig.h
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx
add_executable(Tutorial tutorial.cxx)
# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在CMake中指定C++标准最简单的方法是使用
CMAKE_CXX_STANDARD
变量。
将 CMakeLists.txt 文件中的CMAKE_CXX_STANDARD
变量设置为 11,
并将 CMAKE_CXX_STANDARD_REQUIRED
设置为True。
确保将 CMAKE_CXX_STANDARD
声明添加到 add_executable
的上面。
# 练习 3 - 添加版本号并配置头文件
修改 CMakeLists.txt
:
# TODO 1: Set the minimum required version of CMake to be 3.10
cmake_minimum_required(VERSION 3.10)
# TODO 2: Create a project named Tutorial
project(Tutorial VERSION 1.0)
# TODO 7: Set the project version number as 1.0 in the above project command
# TODO 6: Set the variable CMAKE_CXX_STANDARD to 11
# and the variable CMAKE_CXX_STANDARD_REQUIRED to True
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# TODO 8: Use configure_file to configure and copy TutorialConfig.h.in to
# TutorialConfig.h
configure_file(TutorialConfig.h.in TutorialConfig.h)
# TODO 3: Add an executable called Tutorial to the project
# Hint: Be sure to specify the source file as tutorial.cxx
add_executable(Tutorial tutorial.cxx)
# TODO 9: Use target_include_directories to include ${PROJECT_BINARY_DIR}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21