知识框架
基础概念
- CMake: 一个跨平台的管理软件构建过程的解决方案
第一个实验: 跨平台开发
在Windows
- 我使用的编译器是MinGW
遇到的第一个错误:‘nmake’ ‘-?’
cmake --help 查阅帮助手册
看到:
Generators
The following generators are available on this platform (* marks default):
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
......
Borland Makefiles = Generates Borland makefiles.
* NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
...
- 结论: CMake在Win环境默认使用nmake程序
- 解决方案: 用户可以使用 -G 选项覆盖默认生成器
cmake . -G "MinGW Makefiles"
- 构建一般配合
-B
参数存放生成的文件存放目录cmake . -B ./build -G "MinGW Makefiles"
- 构建一般配合
- 如果在Vscode中安装了CMake Tool插件,配置
"Cmake.Generator"
和“Cmake: Build Directory”
- 生成可执行文件
./> cd build
build/ > make
[ 50%] Building CXX object CMakeFiles/HelloAppBinary.dir/main.cpp.obj
[100%] Linking CXX executable HelloAppBinary.exe
[100%] Built target HelloAppBinary
# 又或者可以使用cmake --build . (更加推荐)
build/ > cmake --build .
[100%] Built target HelloAppBinary
build/ > .\HelloAppBinary.exe
Hello from C++ 20 with CMake on Linux/Windows
The sum is: 12
- 工作区(相关代码)
Linux
- 运行同样的命令,注意看最后的输出,不是exe文件,相应的是Linux平台的可执行文件
- 这就确定了通过CMake,我们可以搭建一个跨平台的C/C++项目
第二个实验:多文件(相关代码)
第三个实验:尝试模块化–库(相关代码)
. Introduction : Everything in a single target
. Setting up static libraries
. embedded
. separated
. Building individual targets
. C++ standards at different granularities
分库的好处:
- 模块化,便于代码复用
- 部分构建,改动项目代码后只需要重新构建所在的库
第四个实验: 目标依赖
- public, interface, private
控制库的属性传播。
不写代码,就做做思想实验。
第五个实验: CMakeLists分而治之
- include命令(with .cmake scripts)
include
是 CMake 的基本功能,从一开始就一直存在。
用于在当前 CMakeLists.txt 文件中包含另一个 CMake 文件的内容,可以是一个脚本或者包含了一组命令的文件。这使得可以在多个项目中共享相同的配置或功能。
相关代码 - add_subdirectory (with more CMakeLists)
add_subdirectory
在 CMake 2.4.6 版本中引入。
用于向当前项目添加一个子目录,并在这个子目录的 CMakeLists.txt 文件中构建其他目标。这有助于将大型项目分解为更小的、可管理的部分。
相关代码
第六个实验: 脚本语法测试
# Scripts can be run with the command below:
# cmake -P <script_name>.cmake
写一些测试就比较方便
第七个实验:GUI工具
- win: cmake-gui
- linux: ccmake
第八个实验:代码格式化clang-format
- Custom Targets, cmake modules: The clang-format use case
理解custom target,在这里,clang-format是一个相对独立的工具。当执行 executable target时,custom target不一定要执行。类比npm script,代码格式化往往用其他script来跑。
cmake --build ./build -t format-code
当然,如果想要每次执行executable target自动运行代码格式化,则只需要添加依赖。
add_dependencies(HelloAppBinary format-code)
第九个实验:依赖管理
- fetchContent
根据仓库坐标自动获取
如果vscode出现include detect error, 修改C/C++扩展插件的Configuration provider为ms-vscode.cpptools - CPM, CMake Package Manager
第十个实验:单元测试
- googletest
- catch2
if(WIN32)
# error: could not open 'kernel32.lib': no such file or directory
# Clang默认尝试在Windows上使用MSVC的标准库,因为Clang自己的标准库还不能在Windows上工作。
# 在 Windows 上使用 MinGW 标准库
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
message("Clang默认尝试在Windows上使用MSVC的标准库,因为Clang自己的标准库还不能在Windows上工作")
endif()
再来一个实验:构建文档
- Doxygen through Doxyfile
doxygen -g
doxygen .\docs\Doxyfile
CMakeLists中设置custom_target - 比custome_target更加优雅的方式: (doxygen_add_doc)