@chengzhao-dev/dsh-cpp-workflow 0.7.5 → 0.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +6 -10
- package/README.md +6 -10
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +2 -2
- package/skills/building-cpp/assets/config/.clang-format +27 -3
- package/skills/building-cpp/assets/config/.clang-tidy +0 -4
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +49 -54
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +5 -5
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +8 -9
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +3 -3
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +2 -2
- package/skills/building-cpp/references/01-build-script.md +1 -1
- package/skills/building-cpp/references/02-environment.md +4 -4
- package/skills/building-cpp/steps/01-build.md +1 -1
- package/skills/building-cpp/steps/02-tooling.md +3 -3
- package/skills/building-cpp/steps/03-check.md +2 -0
- package/skills/building-cpp/steps/04-vscode.md +2 -2
- package/skills/developing-cpp/SKILL.md +9 -5
- package/skills/developing-cpp/references/comments/01-comment-rules.md +15 -1
- package/skills/developing-cpp/references/comments/02-style.md +7 -6
- package/skills/developing-cpp/references/comments/examples/11-declaration-constant.md +26 -0
- package/skills/developing-cpp/references/design/02-raii.md +42 -0
- package/skills/developing-cpp/references/design/03-third-party.md +35 -0
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +36 -0
- package/skills/developing-cpp/references/design/session-layer.md +16 -13
- package/skills/developing-cpp/references/index.md +10 -6
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +3 -3
- package/skills/developing-cpp/references/language/value-types/04-enum.md +3 -3
- package/skills/developing-cpp/references/layout/01-source-layout.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +24 -38
- package/skills/developing-cpp/references/layout/03-declaration-order.md +11 -6
- package/skills/developing-cpp/references/naming/01-identifier-names.md +7 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +6 -6
- package/skills/developing-cpp/scripts/checking-comments.mjs +77 -6
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +214 -13
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +2 -2
- package/skills/engineering/SKILL.md +8 -8
- package/skills/engineering/references/cmake/assets/CompileSettings.cmake +152 -123
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +62 -43
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +38 -0
- package/skills/engineering/references/cmake/linking/01-libraries.md +1 -1
- package/skills/engineering/references/cmake/modules/01-functions.md +26 -16
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +2 -2
- package/skills/engineering/references/cmake/standards/c++11/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++14/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++17/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++20/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++23/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/examples/01-standard.md +1 -1
- package/skills/engineering/references/cmake/standards/index.md +5 -5
- package/skills/engineering/references/cmake/standards/versions.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +50 -36
- package/skills/engineering/references/cmake/targets/examples/01-rpath.md +1 -1
- package/skills/engineering/references/formatting/01-encoding.md +4 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +2 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -2
- package/skills/engineering/references/formatting/examples/01-clang-format.md +13 -4
- package/skills/engineering/references/index.md +5 -5
- package/skills/engineering/steps/01-targets.md +7 -7
- package/skills/engineering/steps/02-modules.md +8 -5
- package/skills/engineering/steps/03-check.md +2 -1
- package/templates/cpp-workflow-project/SKILL.md +3 -2
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake;预算:一次只开这一份(244 行),不自动串联。 -->
|
|
2
2
|
# CMake
|
|
3
3
|
|
|
4
4
|
目标:一个编译器、Ninja、target-based 规则、可用的编译数据库。下面是最小形状和常见边界;工程模板仍由 `building-cpp` 提供,在 `../../../../building-cpp/assets/templates/projects/`,可复用的 CMake 函数在本技能的 `assets/`:`CompileSettings.cmake` 收标准与警告,`LinkSettings.cmake` 收构建期 RPATH 与 GoogleTest 依赖。
|
|
5
5
|
|
|
6
6
|
## 排版
|
|
7
7
|
|
|
8
|
-
CMake
|
|
8
|
+
CMake 没有单独的官方风格指南文档,官方仓库的 [.editorconfig](https://raw.githubusercontent.com/Kitware/CMake/master/.editorconfig) 规定 `CMakeLists.txt`、`*.cmake`、`*.rst` 使用 `indent_size = 2`、`indent_style = space`;本机 `cmake 4.2` 的 359 个官方模块没有一个文件含 tab。本仓与 C++ 源码统一为 4 个空格缩进,行内对齐也用空格;官方文档块内部仍按 RST 的 2 空格。行宽上限取 120 列,中文字符按 2 列计,既有 Tab 按 4 列展开后计宽;命令行、列表和注释一视同仁,官方文档块的分隔行固定 79 列,不跟着加宽。
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
注释用官方文档块。CMake 官方模块在文件开头放一个 `#[=======================================================================[.rst:` 块:模块名下一行画标题线,命令用 `.. command::`,签名用 `.. code-block:: cmake`,参数在正文里用双反引号包住(Kitware 的 [documentation.rst](https://github.com/Kitware/CMake/blob/master/Help/dev/documentation.rst) 与 [GoogleTest.cmake](https://github.com/Kitware/CMake/blob/master/Modules/GoogleTest.cmake) 都是这个形)。本仓照这个形写,块内散文用中文:模块名、`Commands`、`.. command::`、`.. code-block::` 这些标记保留英文,功能、边界与参数说明写中文句子。签名仍留在 `function()` 那一行,块里只抄调用形式,不重复参数表;CMake 函数没有返回值,不写 `:return:`,也不把 C++ 的 `@param`、`@return` 搬进来。模块文件不再画 `#---` 函数分隔线,一个文件的多个命令在 `Commands` 段落里逐个列出。`CMakeLists.txt` 以文件头开头:标题一行、`#` 一行、再用 1–2 行写用途,空一行后接命令;命令之间空一行,按“项目、模块、产物、子目录”的顺序自然分组,不画框线、不写段落标题;小文件可只留一行标题;工程模板提供最小形状,从 `cmake_minimum_required` 开始,注释保持最少,使用者按本段补齐;`CMakeLists.txt` 在每条命令上方写一句说明,子目录文件同样;同一文件保持同一句式,句子的主语按命令的实际对象(工程、目标、产物)写,不要每条都以「本工程」「本项目」开头。中文注释用全角标点,用书面语,避免口语词与网络用语;中文与英文、数字之间留一个空格,句子写完整并说明职责或原因,不复述命令名与模块名。模块文件(`*.cmake`)把说明写进 RST 文档块,函数体内不写行注释;子目录 `CMakeLists.txt` 用普通 `#` 注释。官方 [GoogleTest.cmake](https://github.com/Kitware/CMake/blob/master/Modules/GoogleTest.cmake) 在一个文件里放多个测试命令,可作“文件按职责聚合”的参照;社区 [CompilerWarnings.cmake](https://github.com/cpp-best-practices/cmake_template/blob/main/cmake/CompilerWarnings.cmake) 在每个警告 flag 后写它抓什么问题;本仓把这类说明收进文档块,函数体内不留行注释。
|
|
11
11
|
|
|
12
|
-
Shell
|
|
12
|
+
Shell 脚本同样用 4 个空格缩进;Google Shell Style Guide 用 2 个空格,本仓为单仓统一而有意偏离。
|
|
13
13
|
|
|
14
14
|
## 最小可用形状
|
|
15
15
|
|
|
@@ -26,8 +26,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
|
26
26
|
include(CompileSettings)
|
|
27
27
|
|
|
28
28
|
add_executable(app src/main.cpp)
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
set_target_cxx_standard(app PRIVATE c++23)
|
|
30
|
+
set_target_warnings(app PRIVATE LEVEL standard)
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
### 库 + 程序
|
|
@@ -47,8 +47,8 @@ include(CompileSettings)
|
|
|
47
47
|
add_subdirectory(greeting)
|
|
48
48
|
|
|
49
49
|
add_executable(app src/main.cpp)
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
set_target_cxx_standard(app PRIVATE c++23)
|
|
51
|
+
set_target_warnings(app PRIVATE LEVEL standard)
|
|
52
52
|
target_link_libraries(app PRIVATE greeting)
|
|
53
53
|
```
|
|
54
54
|
|
|
@@ -56,29 +56,29 @@ target_link_libraries(app PRIVATE greeting)
|
|
|
56
56
|
# greeting/CMakeLists.txt
|
|
57
57
|
add_library(greeting STATIC src/greeting.cpp)
|
|
58
58
|
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
set_target_cxx_standard(greeting PUBLIC c++23)
|
|
60
|
+
set_target_warnings(greeting PRIVATE LEVEL standard)
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
- 库的 `PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include` 让程序与链接它的测试写 `#include "greeting.h"` 就能找到公共头,不需要各自再写 include 路径。
|
|
64
|
-
- 标准用 `
|
|
64
|
+
- 标准用 `set_target_cxx_standard`:库用 `PUBLIC` 传播“至少 C++NN”,程序用 `PRIVATE`;`CXX_STANDARD`/`CXX_EXTENSIONS` 是 target 属性,不是 INTERFACE 属性,不会跨 target 传播;每个会编译的 target 都要自己调用一次。
|
|
65
65
|
- `CXX_STANDARD_REQUIRED ON` 防止 CMake 在编译器不支持时静默降级;`CXX_EXTENSIONS OFF` 把 `-std=gnu++NN` 收成 `-std=c++NN`。configure 后检查 `compile_commands.json` 的最后一条 `-std=`。
|
|
66
|
-
- 程序 target
|
|
66
|
+
- 程序 target 用项目技能点名的名字(模板默认 `app`,本书 `terminal`);库 target 用组件名。
|
|
67
67
|
|
|
68
68
|
## 标准与复用
|
|
69
69
|
|
|
70
|
-
C++11–C++23 的两层声明、可见性、常见错误与验证见 `../standards/01-cxx-standard.md`。通用函数 `
|
|
70
|
+
C++11–C++23 的两层声明、可见性、常见错误与验证见 `../standards/01-cxx-standard.md`。通用函数 `set_target_cxx_standard(target visibility c++NN)` 在 `../assets/CompileSettings.cmake`:
|
|
71
71
|
|
|
72
72
|
```cmake
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
set_target_cxx_standard(app PRIVATE c++23)
|
|
74
|
+
set_target_cxx_standard(greeting PUBLIC c++20)
|
|
75
75
|
```
|
|
76
76
|
|
|
77
77
|
函数名不绑定单一标准;新增档位只改函数里的支持列表。模块何时抽、文件怎么放、怎么导入与测试见 `../modules/01-functions.md`。
|
|
78
78
|
|
|
79
79
|
## 警告等级
|
|
80
80
|
|
|
81
|
-
`references/cmake/assets/CompileSettings.cmake` 的 `
|
|
81
|
+
`references/cmake/assets/CompileSettings.cmake` 的 `set_target_warnings(<target> <visibility> LEVEL basic|standard|strict)` 把常用 flag 收成三档;`visibility` 用 `PRIVATE` 或 `INTERFACE`,`PRIVATE` 不向使用方传播。警告与标准写在同一份编译设置模块里。
|
|
82
82
|
|
|
83
83
|
- `basic`:`-Wall -Wextra`;适合刚开始接 CI 的旧代码。
|
|
84
84
|
- `standard`:`basic` 加 `-Werror`、`-Wshadow`、`-Wold-style-cast`、`-Wpedantic`、`-Wnon-virtual-dtor`、`-Woverloaded-virtual`;默认档。
|
|
@@ -90,15 +90,15 @@ MSVC 分支用 `/W4`、`/permissive-`、`/WX`;其他编译器直接报错,
|
|
|
90
90
|
|
|
91
91
|
- 每个 target 自己声明 include、标准、编译选项、链接库;不要用全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS` 给所有 target 加料。
|
|
92
92
|
- `target_include_directories(<target> PUBLIC|PRIVATE <dir>)`:公共头给 `PUBLIC`,私有实现目录给 `PRIVATE`。
|
|
93
|
-
- `target_compile_features(<target> PUBLIC|PRIVATE cxx_std_NN)`:只传播“至少 C++NN”的最低要求;精确 `-std` 由 `
|
|
94
|
-
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC
|
|
93
|
+
- `target_compile_features(<target> PUBLIC|PRIVATE cxx_std_NN)`:只传播“至少 C++NN”的最低要求;精确 `-std` 由 `set_target_cxx_standard` 在会编译的 target 上设置,不靠这一步锁死。
|
|
94
|
+
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC`。官方文档只保证第一个参数已创建;当前 CMake 实现能解析稍后定义的库,但这不是文档承诺,拼错库名还会退化成 `-lname`。工程约定先定义库、再定义程序,不依赖隐式排序。
|
|
95
95
|
- 不要 `file(GLOB ...)`;显式列源文件,新增文件时改 CMake,构建系统才知道。`CONFIGURE_DEPENDS` 依赖构建系统每次检查目录,跨生成器行为也不完全一致,不作为工程规则。
|
|
96
96
|
- 不把 `main()` 编进库;程序只做装配和 I/O。
|
|
97
97
|
- 头文件不参与编译时,不需要 `target_sources` 列出;需要 IDE 展示时再列。
|
|
98
98
|
- 组件多起来时,一个组件一个 `CMakeLists.txt`:库放 `<component>/include` 与 `<component>/src`,程序放 `app/include` 与 `app/src`;`app/include` 只给程序内部用,不要被库 `PUBLIC` 导出。
|
|
99
99
|
- 源文件只列在它所在目录的 target 里;子目录的程序或测试用 `target_link_libraries` 链接该 target,不写 `../other/src/*.cpp`;跨目录共用代码时,先让源目录提供可链接的 target。
|
|
100
100
|
- CMake 注释只写 target 的职责、可见性和使用条件,不写「源码留在本目录」「子目录不写 ../other/src/*.cpp」这类目录边界;源文件列表与链接关系已经表达边界。
|
|
101
|
-
- 一个 target 的配置连续成块:按 `add_*`、`target_include_directories`、`
|
|
101
|
+
- 一个 target 的配置连续成块:按 `add_*`、`target_include_directories`、`set_target_cxx_standard`、`set_target_warnings`、其它 `target_*` 编译选项、`target_link_libraries`、`set_target_properties` 的顺序写;模板尚未引入设置函数时,`target_compile_features` 与设置 `CXX_STANDARD` 的 `set_target_properties` 占标准的位置,警告的 `target_compile_options` 占警告的位置。标准与警告必须相邻,`target_link_libraries` 与 `set_target_properties` 也必须相邻,中间不插其它调用。
|
|
102
102
|
- 测试 target 等出现第二个消费者时再加,不要为单个 `main()` 提前引入测试框架;测试只链接库的公共接口。
|
|
103
103
|
|
|
104
104
|
## 工程名
|
|
@@ -109,7 +109,7 @@ MSVC 分支用 `/W4`、`/permissive-`、`/WX`;其他编译器直接报错,
|
|
|
109
109
|
project(TaskList VERSION 0.1.0 LANGUAGES CXX)
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
-
目录 `task-list/` 对应 `project(TaskList ...)`。派生规则是「按 `-` 切分、每段首字母大写、其余保留」,不要直接照抄目录名,也不要把 `{{PROJECT_NAME}}` 留在模板里。库 target 用组件名的小写形式(`task_list` 或 `tasks`),程序 target
|
|
112
|
+
目录 `task-list/` 对应 `project(TaskList ...)`。派生规则是「按 `-` 切分、每段首字母大写、其余保留」,不要直接照抄目录名,也不要把 `{{PROJECT_NAME}}` 留在模板里。库 target 用组件名的小写形式(`task_list` 或 `tasks`),程序 target 由项目技能点名,模板默认 `app`。
|
|
113
113
|
|
|
114
114
|
## 编译数据库
|
|
115
115
|
|
|
@@ -126,11 +126,14 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
126
126
|
|
|
127
127
|
```cmake
|
|
128
128
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
129
|
+
|
|
129
130
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
131
|
+
|
|
130
132
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
131
133
|
```
|
|
132
134
|
|
|
133
|
-
- 可执行文件统一到 `build/<compiler>/bin
|
|
135
|
+
- 可执行文件统一到 `build/<compiler>/bin`。
|
|
136
|
+
- 库统一到 `build/<compiler>/lib`。
|
|
134
137
|
- 不要输出到源码目录;`git status` 不应出现构建产物。
|
|
135
138
|
- 运行程序时从 `build/<compiler>/bin` 启动,或由脚本设置工作目录。
|
|
136
139
|
|
|
@@ -138,7 +141,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
|
138
141
|
|
|
139
142
|
```cmake
|
|
140
143
|
include(LinkSettings)
|
|
141
|
-
|
|
144
|
+
set_target_build_rpath(app)
|
|
142
145
|
```
|
|
143
146
|
|
|
144
147
|
- 函数按平台写 `BUILD_RPATH`:Linux 用 `$ORIGIN/../lib`,Apple 用 `@loader_path/../lib`。
|
|
@@ -156,18 +159,18 @@ LinkSettings_SetBuildRpath(app)
|
|
|
156
159
|
|
|
157
160
|
## 警告与静态检查
|
|
158
161
|
|
|
159
|
-
- 警告选项统一由 `
|
|
162
|
+
- 警告选项统一由 `set_target_warnings` 按 `basic` / `standard` / `strict` 施加,不要另写一套 `-Wall -Wextra -Werror` 列表;各档的具体 flag 见上文「警告等级」。
|
|
160
163
|
- 不要把警告选项加到全局 `CMAKE_CXX_FLAGS`;每个 target 显式加,或用一个函数统一施加。
|
|
161
164
|
- `clang-tidy` 在构建后单独跑,不挂进 CMake 的编译命令;这样编译数据库稳定,诊断可复现。
|
|
162
165
|
- 需要 `clang-tidy` 集成时用 `CMAKE_CXX_CLANG_TIDY`,但本项目默认不启用,避免构建与检查耦合。
|
|
163
166
|
|
|
164
167
|
## GoogleTest
|
|
165
168
|
|
|
166
|
-
需要测试框架时,用固定版本的 `
|
|
169
|
+
需要测试框架时,用固定版本的 `fetch_googletest`:
|
|
167
170
|
|
|
168
171
|
```cmake
|
|
169
172
|
include(LinkSettings)
|
|
170
|
-
|
|
173
|
+
fetch_googletest()
|
|
171
174
|
```
|
|
172
175
|
|
|
173
176
|
- 函数内部固定 GoogleTest v1.17.0 的 URL 与 SHA-256,避免供应链漂移;版本升级只改一处。
|
|
@@ -199,33 +202,44 @@ LinkSettings_FetchGoogleTest()
|
|
|
199
202
|
|
|
200
203
|
### CMake 函数注释
|
|
201
204
|
|
|
202
|
-
|
|
205
|
+
**好**:
|
|
203
206
|
|
|
204
207
|
```cmake
|
|
205
|
-
#[[.rst:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
208
|
+
#[=======================================================================[.rst:
|
|
209
|
+
CompileSettings
|
|
210
|
+
---------------
|
|
211
|
+
|
|
212
|
+
.. command:: set_target_cxx_standard
|
|
213
|
+
|
|
214
|
+
.. code-block:: cmake
|
|
215
|
+
|
|
216
|
+
set_target_cxx_standard(<target> <visibility> <standard>)
|
|
217
|
+
|
|
218
|
+
让已存在的 ``<target>`` 使用 ``<standard>`` 指定的语言标准。
|
|
219
|
+
本命令只改传入的目标,不设全局变量。
|
|
220
|
+
#]=======================================================================]
|
|
209
221
|
```
|
|
210
222
|
|
|
211
|
-
|
|
223
|
+
模块名、`Commands` 与 `.. code-block::` 这些标记保留英文,块内散文用中文;签名留在 `function()` 那一行,块里只抄调用形式。
|
|
212
224
|
|
|
213
|
-
|
|
225
|
+
**差**:
|
|
214
226
|
|
|
215
227
|
```cmake
|
|
216
228
|
# 给一个目标加上编译警告。
|
|
217
229
|
#
|
|
218
|
-
# 只改传入的目标。PRIVATE 不向使用方传播。
|
|
219
|
-
#
|
|
220
230
|
# :param target: 已经存在的 CMake 目标。
|
|
221
231
|
# :param visibility: 取 PRIVATE 或 INTERFACE。
|
|
222
|
-
# :keyword LEVEL: 可省略。取 basic、standard 或 strict
|
|
223
|
-
function(
|
|
232
|
+
# :keyword LEVEL: 可省略。取 basic、standard 或 strict。
|
|
233
|
+
function(set_target_warnings target visibility)
|
|
224
234
|
```
|
|
225
235
|
|
|
236
|
+
`:param`、`:keyword` 是 CMinx 的 Sphinx 字段,官方模块不用;`@param`、`@return` 是 C++ 的 Doxygen 标签,CMake 里也不用;CMake 函数没有返回值,不写 `:return:`。
|
|
237
|
+
|
|
226
238
|
## 依据
|
|
227
239
|
|
|
228
240
|
- [CMake: `add_library`](https://cmake.org/cmake/help/latest/command/add_library.html):库 target 的类型与作用域。
|
|
229
241
|
- [CMake: `target_include_directories`](https://cmake.org/cmake/help/latest/command/target_include_directories.html):`PUBLIC` / `PRIVATE` / `INTERFACE` 的传递。
|
|
230
242
|
- [CMake: `CMAKE_EXPORT_COMPILE_COMMANDS`](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html):生成 `compile_commands.json`。
|
|
231
243
|
- [CMake: `CMAKE_CXX_STANDARD`](https://cmake.org/cmake/help/latest/variable/CMAKE_CXX_STANDARD.html):标准版本与 `CXX_STANDARD_REQUIRED`。
|
|
244
|
+
- [CMake `documentation.rst`](https://github.com/Kitware/CMake/blob/master/Help/dev/documentation.rst):官方文档块、`.. command::` 与参数段的写法。
|
|
245
|
+
- [CMake `.editorconfig`](https://raw.githubusercontent.com/Kitware/CMake/master/.editorconfig):官方取 2 个空格、不用 tab;本仓改为 4 个空格,仍不用 tab。
|
|
@@ -19,7 +19,7 @@ set_target_properties(greeting PROPERTIES
|
|
|
19
19
|
|
|
20
20
|
add_executable(app src/main.cpp)
|
|
21
21
|
target_link_libraries(app PRIVATE greeting)
|
|
22
|
-
|
|
22
|
+
set_target_build_rpath(app)
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
从 `build/<compiler>/bin/app` 直接启动即可找到同级 `../lib` 下的共享库;安装场景另配 RPATH。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:排版或工具配置时;读完能回答:编码与行尾;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:编码与行尾;预算:一次只开这一份(52 行),不自动串联。 -->
|
|
2
2
|
# 编码与行尾
|
|
3
3
|
|
|
4
4
|
默认组合是 UTF-8 无 BOM、LF。机器规则以仓库根 `.gitattributes` 与 `.editorconfig` 为准,本文件只记决定、MSVC 例外与检查方法。
|
|
@@ -26,6 +26,8 @@ MSVC 默认按 BOM 判断源码编码。源码没有 BOM 时,它按当前用
|
|
|
26
26
|
|
|
27
27
|
- `.gitattributes` 决定 Git 入库和检出;它覆盖贡献者自己的 `core.autocrlf`。
|
|
28
28
|
- `.editorconfig` 决定编辑器保存;`charset` 与 `end_of_line` 必须和 `.gitattributes` 同一档。
|
|
29
|
+
- `.editorconfig` 的边界:`[*]` 给 2 空格默认,C++、CMake、shell 覆盖成 4 个空格;`max_line_length`
|
|
30
|
+
不是核心键,VS Code 不读,C++ 的 120 由 `.clang-format` 的 `ColumnLimit` 强制,CMake 与 shell 只是参考线。
|
|
29
31
|
- `.clang-format` 写 `LineEnding: LF`,避免格式化把行尾改回 CRLF。
|
|
30
32
|
|
|
31
33
|
## 检查
|
|
@@ -39,6 +41,7 @@ node "<skill-dir>/scripts/checking-encoding.mjs" --profile portable
|
|
|
39
41
|
- `portable`(默认)要求所有文本文件 UTF-8 无 BOM、LF、末尾换行。
|
|
40
42
|
- `msvc` 只放宽 MSVC 编译的 C/C++ 源文件:允许 UTF-8 BOM 与 CRLF;Markdown、配置与脚本仍按 `portable` 检查。
|
|
41
43
|
- 退出码:0 无问题,1 有问题,2 参数或 Git 错误。加 `--json` 输出机器可读结果。
|
|
44
|
+
- 脚本只写 stdout,不生成报告文件;不要把输出重定向成仓库根下的日志。要留证据写 `temp/`,否则只引用终端输出。
|
|
42
45
|
|
|
43
46
|
再跑 `git diff --check`,确认没有空白错误。不要用 `git add --renormalize` 把行尾改动混进功能改动;确需规范化时单独提交并说明。
|
|
44
47
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:排版或工具配置时;读完能回答:clang-format;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:clang-format;预算:一次只开这一份(23 行),不自动串联。 -->
|
|
2
2
|
# clang-format
|
|
3
3
|
|
|
4
4
|
三件语言工具各管一段:`clang-format` 排版、`clang-tidy` 诊断、`clangd` 提供补全/跳转/诊断。它们都读同一份编译数据库,但职责不重叠。编辑器侧只保留 CMake Tools 作为配置、构建、运行和调试入口;C/C++ 扩展只做 `CMake: Debug` 的调试后端,不参与语言服务。
|
|
@@ -11,7 +11,7 @@ clang-format --dry-run --Werror src/*.cpp include/*.h app/*.cpp tests/*.cpp
|
|
|
11
11
|
- 工程必须有根 `.clang-format`;缺失时先复制配置,不要回退到内置风格,回退会掩盖配置缺失。
|
|
12
12
|
- 只扫描项目源码:`src/`、`include/`、`app/`、`tests/` 等实际存在的目录;不扫描 `build/`、第三方、生成文件。
|
|
13
13
|
- 配置文件在工程根 `.clang-format`;不要依赖用户主目录或编辑器内置配置。
|
|
14
|
-
- `BasedOnStyle: Google`、`Standard: Latest
|
|
14
|
+
- `BasedOnStyle: Google`、`Standard: Latest`;clang-format 21/22 不认 `c++23`,直接写会报 `error: unknown enumerated scalar`;23 起支持,但统一用 `Latest` 兼容旧版,由工具取它支持的最高标准。列宽、缩进与包含分块以工程根 `.clang-format` 为准;模板资产固定 `UseTab: Never`、`TabWidth: 4`、`IndentWidth: 4`、`ContinuationIndentWidth: 8`、`AccessModifierOffset: -4`、`ColumnLimit: 120` 与 `IncludeBlocks: Preserve`,不覆盖 `IncludeCategories`;工程显式写 `ColumnLimit` 时按工程值执行。
|
|
15
15
|
- 构造函数初始化列表写 `PackConstructorInitializers: BinPack`:放得下时并入一行,超出列宽再按成员换行。Google 预设逐项换行,成员一多就占五六行,与「列宽内保持一行」冲突;这条只在配置里定,不在条文里规定手写位置。
|
|
16
16
|
- 格式化失败就是失败,不要用 `// clang-format off` 绕过;确需关闭时只包住无法排版的宏或表格,并写一句理由。
|
|
17
17
|
|
|
@@ -20,3 +20,5 @@ clang-format --dry-run --Werror src/*.cpp include/*.h app/*.cpp tests/*.cpp
|
|
|
20
20
|
- [ClangFormat: Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html):`BasedOnStyle`、`Standard`、`ColumnLimit` 等选项。
|
|
21
21
|
- [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html):`--dry-run`、`--Werror` 与 `-i` 的用法。
|
|
22
22
|
- [Google C++ Style Guide: Formatting](https://google.github.io/styleguide/cppguide.html#Formatting):Google 基线的排版约定。
|
|
23
|
+
- [CLion: Code style settings for C and C++](https://www.jetbrains.com/help/clion/settings-code-style-c-cpp.html):用 tabs 缩进时推荐 tabs 缩进、spaces 对齐。
|
|
24
|
+
- [CLion: EditorConfig](https://www.jetbrains.com/help/clion/editorconfig.html):`.editorconfig` 覆盖项目代码风格。
|
|
@@ -12,11 +12,11 @@ clang-tidy -p build/clang \
|
|
|
12
12
|
|
|
13
13
|
- `-p` 指向当前编译器的构建目录,读取 `compile_commands.json`。
|
|
14
14
|
- `--header-filter` 控制哪些头文件的诊断被显示,不改变 clang-tidy 实际解析哪些头;模板实例化可能让一条根因产生多条警告,警告条数不等于缺陷条数。按根因修,不要按条数修。
|
|
15
|
-
- `--header-filter` 用项目头目录的白名单,不要用 `.*`
|
|
15
|
+
- `--header-filter` 用项目头目录的白名单,不要用 `.*` 把系统头和第三方头一并纳入。`--header-filter` 是对头文件名的正则;上面用 `$(pwd -P)` 前缀只是让正则匹配绝对路径的一种写法,不是文档强制。上面的目录是常见布局,工程用别的顶层目录时按实际替换。`--exclude-header-filter` 到 clang-tidy 19 才加入,16/17/18 都没有,不要依赖它。
|
|
16
16
|
- 系统头默认被排除,不要加 `--system-headers`。
|
|
17
17
|
- 只扫描项目源文件;不要把 `build/` 或第三方源码传给 clang-tidy。
|
|
18
18
|
- `--warnings-as-errors='*'` 把警告当失败;修代码,不要新增 `NOLINT`。确需抑制时写 `// NOLINT(检查名): 理由`,理由必须具体。
|
|
19
|
-
- 配置在工程根 `.clang-tidy`;默认集合是 `google
|
|
19
|
+
- 配置在工程根 `.clang-tidy`;默认集合是 `google-*` 与 `readability-identifier-naming`,不启用 `portability-avoid-pragma-once`(工程统一用 `#pragma once`)。关闭某条时逐条写理由,不整族关闭;命名规则只配项目确实统一的项,不设 `FunctionCase`。
|
|
20
20
|
- 没有编译数据库时先回到 `building-cpp` 的定构建生成;不要用 `--` 手工拼编译参数绕过。
|
|
21
21
|
- `clang-tidy` 的诊断可能指向头文件;修头文件里的声明或实现,不要只改调用点。
|
|
22
22
|
|
|
@@ -41,7 +41,7 @@ Diagnostics:
|
|
|
41
41
|
|
|
42
42
|
## 编辑器
|
|
43
43
|
|
|
44
|
-
`.vscode/extensions.json`
|
|
44
|
+
`.vscode/extensions.json` 推荐(`ms-vscode.cpptools` 只作调试后端,关闭 IntelliSense 与格式化;clangd 安装文档要求不装它,是针对 IntelliSense 冲突的默认建议,这里是明确的本地例外):
|
|
45
45
|
|
|
46
46
|
```json
|
|
47
47
|
{
|
|
@@ -64,7 +64,7 @@ Diagnostics:
|
|
|
64
64
|
],
|
|
65
65
|
"cmake.buildDirectory": "${workspaceFolder}/build/clang",
|
|
66
66
|
"cmake.generator": "Ninja",
|
|
67
|
-
"cmake.configureOnOpen":
|
|
67
|
+
"cmake.configureOnOpen": false,
|
|
68
68
|
"cmake.buildBeforeRun": true,
|
|
69
69
|
"cmake.exportCompileCommandsFile": true,
|
|
70
70
|
"cmake.debugConfig": {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<!-- 读取时机:排版或工具配置时;读完能回答:正反例:clang-format;预算:一次只开这一份(
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:正反例:clang-format;预算:一次只开这一份(47 行),不自动串联。 -->
|
|
2
2
|
# 正反例:clang-format
|
|
3
3
|
|
|
4
4
|
## 构建脚本里自动格式化
|
|
@@ -24,16 +24,25 @@ clang-format --dry-run --Werror "${sources[@]}" # 只检查
|
|
|
24
24
|
```yaml
|
|
25
25
|
# 本项目遵循 Google C++ Style Guide,详见 docs/...
|
|
26
26
|
# 所有检查必须通过,禁止 NOLINT,禁止 ...
|
|
27
|
-
|
|
27
|
+
BasedOnStyle: Google
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
**好**:
|
|
31
31
|
|
|
32
32
|
```yaml
|
|
33
|
-
#
|
|
33
|
+
# Google 基线;本工程用 4 个空格缩进、120 列。
|
|
34
34
|
Language: Cpp
|
|
35
35
|
BasedOnStyle: Google
|
|
36
36
|
Standard: Latest
|
|
37
|
+
IncludeBlocks: Preserve
|
|
38
|
+
ColumnLimit: 120
|
|
39
|
+
IndentWidth: 4
|
|
40
|
+
TabWidth: 4
|
|
41
|
+
UseTab: Never
|
|
42
|
+
ContinuationIndentWidth: 8
|
|
43
|
+
AccessModifierOffset: -4
|
|
44
|
+
PackConstructorInitializers: BinPack
|
|
45
|
+
LineEnding: LF
|
|
37
46
|
```
|
|
38
47
|
|
|
39
|
-
|
|
48
|
+
配置只选基线,再写有意偏离的少量选项,不重复策略;列宽、缩进、tab 与包含分块仍以工程根 `.clang-format` 为准。需要新规则时先改技能文档并说明理由,不要在手写配置里堆本地约定。两例的差别在把策略写进配置头,以及列宽、缩进、tab 与对齐是否成套,不是能否设 `ColumnLimit`。
|
|
@@ -11,11 +11,11 @@
|
|
|
11
11
|
| `cmake/linking/02-rpath.md` | 输出目录与 RPATH | 一次只开这一份(25 行),不自动串联。 |
|
|
12
12
|
| `cmake/linking/03-undefined-reference.md` | undefined reference 分诊 | 一次只开这一份(20 行),不自动串联。 |
|
|
13
13
|
| `cmake/linking/04-inspect.md` | 验收:file 与 ldd | 一次只开这一份(20 行),不自动串联。 |
|
|
14
|
-
| `cmake/modules/01-functions.md` | CMake 模块与函数 | 一次只开这一份(
|
|
14
|
+
| `cmake/modules/01-functions.md` | CMake 模块与函数 | 一次只开这一份(118 行),不自动串联。 |
|
|
15
15
|
| `cmake/standards/01-cxx-standard.md` | C++ 标准与 CMake | 一次只开这一份(68 行),不自动串联。 |
|
|
16
16
|
| `cmake/standards/versions.md` | C++11–C++23 各档差异 | 一次只开这一份(43 行),不自动串联。 |
|
|
17
17
|
| `cmake/standards/examples/01-standard.md` | 正反例:标准只写一半 | 一次只开这一份(19 行),不自动串联。 |
|
|
18
|
-
| `cmake/targets/01-target-rules.md` | CMake | 一次只开这一份(
|
|
18
|
+
| `cmake/targets/01-target-rules.md` | CMake | 一次只开这一份(244 行),不自动串联。 |
|
|
19
19
|
| `cmake/targets/examples/01-rpath.md` | 正反例:共享库运行时路径 | 一次只开这一份(24 行),不自动串联。 |
|
|
20
20
|
| `cmake/testing/01-ctest.md` | 测试与 CTest | 一次只开这一份(61 行),不自动串联。 |
|
|
21
21
|
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
|
|
24
24
|
| 文件 | 读完能回答 | 预算 |
|
|
25
25
|
| --- | --- | --- |
|
|
26
|
-
| `formatting/01-encoding.md` | 编码与行尾 | 一次只开这一份(
|
|
27
|
-
| `formatting/02-clang-format.md` | clang-format | 一次只开这一份(
|
|
26
|
+
| `formatting/01-encoding.md` | 编码与行尾 | 一次只开这一份(52 行),不自动串联。 |
|
|
27
|
+
| `formatting/02-clang-format.md` | clang-format | 一次只开这一份(23 行),不自动串联。 |
|
|
28
28
|
| `formatting/03-clang-tidy.md` | clang-tidy 与静态检查门禁 | 一次只开这一份(58 行),不自动串联。 |
|
|
29
29
|
| `formatting/04-clangd.md` | clangd 与编辑器 | 一次只开这一份(91 行),不自动串联。 |
|
|
30
|
-
| `formatting/examples/01-clang-format.md` | 正反例:clang-format | 一次只开这一份(
|
|
30
|
+
| `formatting/examples/01-clang-format.md` | 正反例:clang-format | 一次只开这一份(47 行),不自动串联。 |
|
|
31
31
|
| `formatting/examples/02-clang-tidy.md` | 正反例:clang-tidy | 一次只开这一份(43 行),不自动串联。 |
|
|
32
32
|
| `formatting/examples/03-clangd.md` | 正反例:clangd | 一次只开这一份(109 行),不自动串联。 |
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
|
|
19
19
|
target 规则:
|
|
20
20
|
|
|
21
|
-
- 程序 target
|
|
21
|
+
- 程序 target 用项目技能点名的名字(模板默认 `app`,本书 `terminal`);库 target 用组件名,程序链接库;不把 `main()` 编进库。
|
|
22
22
|
- 每个 target 自己声明 include、标准、编译选项和链接库;公共头用 `PUBLIC`,实现目录用 `PRIVATE`。
|
|
23
|
-
- 用 `target_include_directories`、`target_compile_features`、`target_link_libraries`;禁止全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS`。
|
|
23
|
+
- 用 `target_include_directories`、`target_compile_features`、`target_link_libraries`;禁止全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS`。target 内的命令按 `../references/cmake/targets/01-target-rules.md` 的块顺序写,标准与警告放在链接之前。
|
|
24
24
|
- 源文件显式列出;不写 `file(GLOB ...)`,新增文件时改 CMake。
|
|
25
25
|
- `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)`;可执行文件输出到 `build/<compiler>/bin`,库到 `build/<compiler>/lib`。
|
|
26
|
-
- 需要直接运行构建产物的共享库程序,调用 `
|
|
27
|
-
- 标准用工程根 `cmake/` 里的 `
|
|
28
|
-
- 警告用同一份 `CompileSettings.cmake` 里的 `
|
|
26
|
+
- 需要直接运行构建产物的共享库程序,调用 `set_target_build_rpath(app)`;函数在 Linux 写 `$ORIGIN/../lib`,Apple 写 `@loader_path/../lib`。
|
|
27
|
+
- 标准用工程根 `cmake/` 里的 `set_target_cxx_standard(target visibility c++NN)`;模板为了脱离插件可直接复制而内联三属性,复制进工程后应改回函数。没有这个函数时先从 `../references/cmake/assets/CompileSettings.cmake` 复制到工程根 `cmake/` 并导入,不要在每个 target 重复三属性。需要 `set_target_build_rpath` 或 `fetch_googletest` 时,把同目录的 `LinkSettings.cmake` 一并复制并 `include(LinkSettings)`。
|
|
28
|
+
- 警告用同一份 `CompileSettings.cmake` 里的 `set_target_warnings(target visibility LEVEL basic|standard|strict)`;`visibility` 用 `PRIVATE` 或 `INTERFACE`,不要在每个 target 重复 `-Wall` 列表。
|
|
29
29
|
|
|
30
30
|
## 命令形状
|
|
31
31
|
|
|
@@ -41,13 +41,13 @@ GCC 模式把目录和编译器换成 `build/gcc`、`g++`(C 驱动换成 `gcc`
|
|
|
41
41
|
|
|
42
42
|
## 情况
|
|
43
43
|
|
|
44
|
-
**新工程**:工程模板仍由 `building-cpp` 提供,从 `../../building-cpp/assets/templates/projects/` 选形态,只复制目标工程缺的文件;模板里的三属性与 `-Wall -Wextra -Werror` 只是可运行的最小形状,接管时按本节换成 `
|
|
44
|
+
**新工程**:工程模板仍由 `building-cpp` 提供,从 `../../building-cpp/assets/templates/projects/` 选形态,只复制目标工程缺的文件;模板里的三属性与 `-Wall -Wextra -Werror` 只是可运行的最小形状,接管时按本节换成 `set_target_cxx_standard`;需要警告时再加 `set_target_warnings`;模板里的 `{{PROJECT_NAME}}` 换成真实工程名,再按本节改 target 与标准。
|
|
45
45
|
|
|
46
46
|
**已有工程**:先读现有 `CMakeLists.txt`,找出全局设置、GLOB、输出目录和 target 命名;只改与当前任务相关的一处。大改前先让现有构建跑通,保留可回退点。
|
|
47
47
|
|
|
48
48
|
**库 + 程序**:库的公共 include 用 `PUBLIC` 传播;程序不直接包含库的 `src/`。静态库与动态库的 target 写法相同,动态库额外设运行时路径。
|
|
49
49
|
|
|
50
|
-
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 用 `
|
|
50
|
+
**测试**:确有第二个消费者时再加 `enable_testing()` 与 `add_subdirectory(tests)`;测试 target 用 `fetch_googletest()` 取固定版本框架,并用 `gtest_discover_tests(<target> DISCOVERY_TIMEOUT 30 PROPERTIES LABELS unit TIMEOUT 10)` 注册;不得用 `add_test(NAME all ...)` 包整个可执行文件。测试 target 只链接被测库的公共接口,没有测试时不加空测试。
|
|
51
51
|
|
|
52
52
|
**预设**:使用 preset 时名称固定 `clang` / `gcc`,与构建目录一致;没有 preset 时直接用上面的命令,不要两套并存。
|
|
53
53
|
|
|
@@ -16,8 +16,11 @@
|
|
|
16
16
|
|
|
17
17
|
- 一个文件放一组语义相近的函数,不为每个函数建单独文件;文件过大或出现无关职责时再拆。
|
|
18
18
|
- 文件名用 UpperCamelCase,按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`。
|
|
19
|
-
- 函数名用
|
|
20
|
-
|
|
19
|
+
- 函数名用 snake_case,按“动词 + 对象”直接描述动作,例如
|
|
20
|
+
`set_target_cxx_standard`、`set_target_warnings`、`set_output_directories`;
|
|
21
|
+
不要机械重复模块名。CMake 没有命名空间,函数全局可见;确有重名风险时
|
|
22
|
+
再加短项目前缀,而不是完整模块名。
|
|
23
|
+
- 工程自己新增、不准备回流的函数同样先取动作名;与外部模块可能冲突时再加短项目前缀,例如 `my_project_do_something`。
|
|
21
24
|
- 模块开头写 `include_guard(GLOBAL)`,避免重复导入时重定义函数。
|
|
22
25
|
|
|
23
26
|
## 导入
|
|
@@ -26,12 +29,12 @@
|
|
|
26
29
|
|
|
27
30
|
## 契约
|
|
28
31
|
|
|
29
|
-
-
|
|
32
|
+
- 公开函数上方写官方文档块:`#[=======================================================================[.rst:` 开头,模块名与 `---` 标题线,命令用 `.. command::`,签名用 `.. code-block:: cmake`,块内散文写中文。
|
|
30
33
|
- 完整写法见 `../references/cmake/targets/01-target-rules.md`;签名留在 `function()` 那一行。
|
|
31
34
|
- 签名、警告等级与错误消息以 `../references/cmake/assets/` 和 `../references/cmake/modules/01-functions.md` 为准;本步不复制第二份。
|
|
32
|
-
- 普通函数只改传入 target 的属性,不写全局变量、不读环境变量、不下载依赖;唯一例外是 `
|
|
35
|
+
- 普通函数只改传入 target 的属性,不写全局变量、不读环境变量、不下载依赖;唯一例外是 `fetch_googletest`,只在测试需要时调用。
|
|
33
36
|
- 先校验 target、可见性与标准,再调用 `target_*` 或 `set_target_properties`;失败用 `FATAL_ERROR`,消息带函数名与非法值。
|
|
34
37
|
|
|
35
38
|
## 退出
|
|
36
39
|
|
|
37
|
-
模块可被根文件导入且函数签名稳定后,进 `03-check.md` 跑 configure、构建与数据库核对。排版、
|
|
40
|
+
模块可被根文件导入且函数签名稳定后,进 `03-check.md` 跑 configure、构建与数据库核对。排版、4 空格缩进、120 列与文档块形态按 `../references/cmake/targets/01-target-rules.md`。
|
|
@@ -23,7 +23,8 @@ GCC 模式把目录和编译器换成 `build/gcc`、`g++`。每次检查都重
|
|
|
23
23
|
- **标准**:读 `build/clang/compile_commands.json` 中每个 target 的 `-std=`;每个 target 只能有一条,必须与 `environment.json` 的 `standard` 一致,不能是 `gnu++NN` 或更高默认标准,不同值即失败。
|
|
24
24
|
- **没有全局污染**:全仓搜索 `include_directories`、`add_definitions`、`CMAKE_CXX_FLAGS`、`file(GLOB`,命中就回第 1 步。
|
|
25
25
|
- **模块导入**:根 `CMakeLists.txt` 只导入一次;函数名与文件名可追溯;删除旧模块后没有残留 `include()`。
|
|
26
|
-
- **排版**:`CMakeLists.txt` 与 `*.cmake`
|
|
26
|
+
- **排版**:`CMakeLists.txt` 与 `*.cmake` 缩进用 4 个空格,行内对齐也用空格,UTF-8 无 BOM、LF;抽查 120 列(中文按 2 列计)以上的行是否有必要。
|
|
27
|
+
- **文档块**:每个模块文件开头有 `#[=` 文档块与 `.. command::`,块内散文写中文,签名留在 `function()` 那一行。
|
|
27
28
|
- **产物位置**:可执行文件在 `build/<compiler>/bin`,库在 `build/<compiler>/lib`;源码目录没有新增构建产物。
|
|
28
29
|
- **产物可加载**:对 `build/<compiler>/bin/app` 跑 `file` 与 `ldd`(macOS 用 `otool -L`),不得出现 `not found`;出现缺失按 RPATH 回第 1 步。启动检查由 `building-cpp` 的入口命令负责。见 `../references/cmake/linking/04-inspect.md`。
|
|
29
30
|
|
|
@@ -37,9 +37,9 @@ metadata:
|
|
|
37
37
|
- `.clang-format`、`.clang-tidy`、`.clangd`、`.vscode/` 随工程存放,不继承仓库根或用户主目录。
|
|
38
38
|
- 库与程序分开 target;程序链接库,终端输入输出留在程序。
|
|
39
39
|
- CTest 在工程内注册并运行;仓库根不构建、不注册测试。
|
|
40
|
-
-
|
|
40
|
+
- 头文件用 `#pragma once`,不写路径式包含守卫。
|
|
41
41
|
- 命名按 Google C++ 风格与工程白名单执行;谓词用 `Is/Has/Can/Completes` 等肯定式。
|
|
42
|
-
-
|
|
42
|
+
- 标准由 `cmake/CompileSettings.cmake` 的 `set_target_cxx_standard` 统一施加;工程需要警告时再调用 `set_target_warnings`,不默认给所有 target 加 `-Werror`。`environment.json` 的 `standard` 与 `.clang-format` 同步。
|
|
43
43
|
- 每个可独立构建、运行的程序一个工程;共享库等第二个真实使用方出现、语义稳定后再抽。
|
|
44
44
|
- 阶段表、代码目录与文档章节保持对齐;没有阶段表时只写用户点名的行为。
|
|
45
45
|
|
|
@@ -73,6 +73,7 @@ metadata:
|
|
|
73
73
|
- [ ] `clang-format --dry-run --Werror` 与 `clang-tidy` 无新告警。
|
|
74
74
|
- [ ] 阶段表、代码目录与文档章节的实际状态一致。
|
|
75
75
|
- [ ] 有文档检查时,项目声明的检查命令通过;跨域改动也跑同一条。
|
|
76
|
+
- [ ] `git status --porcelain` 只剩本轮有意改动的文件;检查命令只写终端,需要留证据时写已忽略的 `temp/`。
|
|
76
77
|
|
|
77
78
|
## 可选约定
|
|
78
79
|
|