@chengzhao-dev/dsh-cpp-workflow 0.7.1 → 0.7.4
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 +16 -10
- package/README.md +16 -10
- package/cordis.patch.yml +6 -6
- package/lib/client.js +3 -3
- package/lib/comment-policy.js +1 -1
- package/lib/index.js +45 -37
- package/lib/workspace.js +10 -17
- package/package.json +9 -3
- package/skills/building-cpp/SKILL.md +17 -17
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +0 -0
- package/skills/building-cpp/examples/environment.md +4 -3
- package/skills/building-cpp/references/{build-script.md → 01-build-script.md} +4 -3
- package/skills/building-cpp/references/{environment.md → 02-environment.md} +3 -2
- package/skills/building-cpp/scripts/checking-environment.mjs +11 -13
- package/skills/building-cpp/steps/01-build.md +4 -3
- package/skills/building-cpp/steps/02-tooling.md +12 -12
- package/skills/building-cpp/steps/03-check.md +5 -5
- package/skills/building-cpp/{project/vscode/02-settings.md → steps/04-vscode.md} +24 -6
- package/skills/developing-cpp/SKILL.md +21 -18
- package/skills/developing-cpp/references/comments/{comment-rules.md → 01-comment-rules.md} +81 -64
- package/skills/developing-cpp/references/comments/{comment-style.md → 02-style.md} +16 -18
- package/skills/developing-cpp/references/comments/examples/{declaration-class.md → 01-declaration-class.md} +16 -16
- package/skills/developing-cpp/references/comments/examples/{declaration-constructor.md → 02-declaration-constructor.md} +3 -2
- package/skills/developing-cpp/references/comments/examples/03-declaration-enum.md +48 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-file.md → 04-declaration-file.md} +6 -5
- package/skills/developing-cpp/references/comments/examples/05-declaration-function.md +116 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-member.md → 06-declaration-member.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/07-declaration-predicate.md +15 -0
- package/skills/developing-cpp/references/comments/examples/{declaration-struct.md → 08-declaration-struct.md} +4 -3
- package/skills/developing-cpp/references/comments/examples/09-implementation-body.md +95 -0
- package/skills/developing-cpp/references/comments/examples/10-implementation-todo.md +17 -0
- package/skills/developing-cpp/references/comments/glossary/01-comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/{project-design.md → 01-project-design.md} +21 -17
- package/skills/developing-cpp/references/design/examples/{behavior-sentence.md → 01-behavior-sentence.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{coordinate-table.md → 02-coordinate-table.md} +4 -3
- package/skills/developing-cpp/references/design/examples/{header-place.md → 03-header-place.md} +2 -1
- package/skills/developing-cpp/references/design/examples/{library-boundary.md → 04-library-boundary.md} +1 -0
- package/skills/developing-cpp/references/design/examples/{library-split.md → 05-library-split.md} +1 -0
- package/skills/developing-cpp/references/design/examples/06-move-result.md +42 -0
- package/skills/developing-cpp/references/design/examples/07-preview-read.md +37 -0
- package/skills/developing-cpp/references/design/session-layer.md +89 -0
- package/skills/developing-cpp/references/exceptions/{throw-rules.md → 01-exception-rules.md} +56 -61
- package/skills/developing-cpp/references/exceptions/examples/{throw-boundary.md → 01-throw-boundary.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-build-mode.md → 02-throw-build-mode.md} +5 -4
- package/skills/developing-cpp/references/exceptions/examples/{throw-catch.md → 03-throw-catch.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-destructor.md → 04-throw-destructor.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-format.md → 05-throw-format.md} +1 -0
- package/skills/developing-cpp/references/exceptions/examples/{throw-message.md → 06-throw-message.md} +10 -1
- package/skills/developing-cpp/references/exceptions/examples/{throw-when.md → 07-throw-when.md} +1 -0
- package/skills/developing-cpp/references/index.md +132 -0
- package/skills/developing-cpp/references/language/glossary/{language-terms.md → 01-language-terms.md} +3 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +2 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +2 -1
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +5 -4
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +13 -4
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +4 -1
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +1 -0
- package/skills/developing-cpp/references/language/language-basics/{10-checklist.md → 10-language-checklist.md} +2 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +1 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +1 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +1 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +1 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +1 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +1 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +2 -1
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +17 -6
- package/skills/developing-cpp/references/language/standard-library/07-time.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +2 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +1 -0
- package/skills/developing-cpp/references/language/standard-library/{10-checklist.md → 10-library-checklist.md} +1 -0
- package/skills/developing-cpp/references/language/value-types/01-width.md +1 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +1 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +1 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +3 -2
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +1 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +1 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +2 -1
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +2 -1
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +2 -1
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +1 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +1 -0
- package/skills/developing-cpp/references/layout/01-source-layout.md +3 -3
- package/skills/developing-cpp/references/layout/02-file-template.md +10 -9
- package/skills/developing-cpp/references/layout/03-declaration-order.md +25 -12
- package/skills/developing-cpp/references/naming/01-identifier-names.md +128 -0
- package/skills/developing-cpp/references/naming/examples/01-naming-accessor.md +29 -0
- package/skills/developing-cpp/references/naming/examples/02-naming-basic.md +4 -0
- package/skills/developing-cpp/references/naming/examples/{naming-current-prefix.md → 03-naming-current-prefix.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/04-naming-local.md +46 -0
- package/skills/developing-cpp/references/naming/examples/{naming-lookup.md → 05-naming-lookup.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/06-naming-lower-layer.md +46 -0
- package/skills/developing-cpp/references/naming/examples/{naming-multiple-return.md → 07-naming-multiple-return.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/08-naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/09-naming-predicate.md +19 -0
- package/skills/developing-cpp/references/naming/examples/{naming-rename.md → 10-naming-rename.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-responsibility.md → 11-naming-responsibility.md} +1 -0
- package/skills/developing-cpp/references/naming/examples/{naming-role.md → 12-naming-role.md} +2 -1
- package/skills/developing-cpp/references/naming/examples/13-naming-state-change.md +70 -0
- package/skills/developing-cpp/references/naming/glossary/01-naming-terms.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/02-position.md +24 -0
- package/skills/developing-cpp/references/naming/glossary/03-selection.md +23 -0
- package/skills/developing-cpp/references/naming/glossary/04-square.md +48 -0
- package/skills/developing-cpp/references/naming/glossary/05-scoring.md +27 -0
- package/skills/developing-cpp/references/naming/glossary/06-difficulty.md +26 -0
- package/skills/developing-cpp/references/style/{google-cpp-style.md → 01-google-cpp-style.md} +16 -9
- package/skills/developing-cpp/scripts/checking-comments.mjs +116 -9
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +27 -1
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +6 -6
- package/skills/developing-cpp/steps/03-declaration.md +7 -7
- package/skills/developing-cpp/steps/04-implementation.md +16 -16
- package/skills/engineering/SKILL.md +19 -19
- package/skills/engineering/references/cmake/linking/01-libraries.md +7 -4
- package/skills/engineering/references/cmake/linking/02-rpath.md +1 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +3 -2
- package/skills/engineering/references/cmake/linking/04-inspect.md +1 -1
- package/skills/engineering/references/cmake/modules/{functions.md → 01-functions.md} +4 -3
- package/skills/engineering/references/cmake/{standard/cxx-standard.md → standards/01-cxx-standard.md} +2 -1
- package/skills/engineering/references/cmake/{standard/examples/standard.md → standards/examples/01-standard.md} +2 -1
- package/skills/engineering/references/cmake/{standard/standards/README.md → standards/index.md} +22 -21
- package/skills/engineering/references/cmake/standards/versions.md +44 -0
- package/skills/engineering/references/cmake/targets/{rules.md → 01-target-rules.md} +34 -5
- package/skills/engineering/references/cmake/targets/examples/{rpath.md → 01-rpath.md} +1 -0
- package/skills/engineering/references/cmake/testing/{ctest.md → 01-ctest.md} +4 -3
- package/skills/engineering/references/formatting/01-encoding.md +2 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -3
- package/skills/engineering/references/formatting/03-clang-tidy.md +3 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -0
- package/skills/engineering/references/formatting/examples/{clang-format.md → 01-clang-format.md} +2 -1
- package/skills/engineering/references/formatting/examples/{clang-tidy.md → 02-clang-tidy.md} +1 -0
- package/skills/engineering/references/formatting/examples/{clangd.md → 03-clangd.md} +1 -0
- package/skills/engineering/references/index.md +32 -0
- package/skills/engineering/{references/formatting/scripts → scripts}/checking-encoding.mjs +6 -7
- package/skills/engineering/steps/01-targets.md +5 -4
- package/skills/engineering/steps/02-modules.md +3 -3
- package/skills/engineering/steps/03-check.md +3 -2
- package/skills/shared/budget.md +20 -0
- package/skills/{developing-cpp/references/editing/01-edit-operations.md → shared/operations.md} +8 -4
- package/skills/{developing-cpp/references/editing/02-rule-priority.md → shared/rule-priority.md} +2 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +0 -28
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +0 -48
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +0 -127
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +0 -14
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +0 -64
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +0 -16
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +0 -93
- package/skills/developing-cpp/references/design/examples/move-result.md +0 -39
- package/skills/developing-cpp/references/design/examples/preview-read.md +0 -40
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +0 -17
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +0 -3
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +0 -39
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +0 -16
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +0 -18
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +0 -57
- package/skills/developing-cpp/references/naming/identifier-names.md +0 -104
- package/skills/engineering/references/cmake/standard/standards/c++11/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++14/README.md +0 -48
- package/skills/engineering/references/cmake/standard/standards/c++17/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++20/README.md +0 -50
- package/skills/engineering/references/cmake/standard/standards/c++23/README.md +0 -50
- package/skills/engineering/references/cmake/targets/examples/glob.md +0 -22
- package/skills/engineering/references/cmake/targets/examples/target.md +0 -156
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++11/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++14/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++17/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++20/cmake-snippet.cmake +0 -0
- /package/skills/engineering/references/cmake/{standard/standards → standards}/c++23/cmake-snippet.cmake +0 -0
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: engineering
|
|
3
|
-
description: 编写或重构 C++11–C++23 工程的 CMake 与排版工具配置时使用:CMakeLists.txt、cmake 模块、target 属性、依赖与输出目录、语言标准,以及 clang-format、clang-tidy、clangd
|
|
3
|
+
description: 编写或重构 C++11–C++23 工程的 CMake 与排版工具配置时使用:CMakeLists.txt、cmake 模块、target 属性、依赖与输出目录、语言标准,以及 clang-format、clang-tidy、clangd;找不到头文件、链接失败、target 不存在或 -std 不对时也从这里进。新工程默认 C++23。不用于 Quarto、Python 或非 C++ 文档。
|
|
4
4
|
metadata:
|
|
5
5
|
short-description: CMake 与排版工具
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# engineering
|
|
9
9
|
|
|
10
|
-
本技能管两件事:CMake 文件与 target 契约,以及编码与排版工具配置。函数体、类型、命名、异常与源码注释只开 `developing-cpp`;构建目录、编译器、入口脚本与运行检查开 `building-cpp
|
|
10
|
+
本技能管两件事:CMake 文件与 target 契约,以及编码与排版工具配置。函数体、类型、命名、异常与源码注释只开 `developing-cpp`;构建目录、编译器、入口脚本与运行检查开 `building-cpp`。加载顺序与层表见 [budget.md](../shared/budget.md)。
|
|
11
11
|
|
|
12
12
|
## 适用场景
|
|
13
13
|
|
|
14
|
-
- **负责**:`CMakeLists.txt`、`references/cmake/` 模块、`target_*` 命令、目标属性、include 与链接边界、输出目录、共享库运行时路径、`FetchContent`、preset 与安装导出;UTF-8/LF、clang-format、clang-tidy、clangd
|
|
14
|
+
- **负责**:`CMakeLists.txt`、`references/cmake/` 模块、`target_*` 命令、目标属性、include 与链接边界、输出目录、共享库运行时路径、`FetchContent`、preset 与安装导出;UTF-8/LF、clang-format、clang-tidy、clangd 的配置条文。只写规则条文与选型,不把文件复制进项目、不跑工具检查;落地与检查归 `building-cpp`。
|
|
15
15
|
- **不负责**:源码语义与注释开 `developing-cpp`;`environment.json`、`running-build.sh`、构建目录、编译器与 CTest 运行开 `building-cpp`。
|
|
16
16
|
- **交接**:一次改动同时碰到源码和 CMake 时先开 `developing-cpp` 定行为与边界,再用本技能把源文件、库和程序写进 target,最后到 `building-cpp` 跑检查。只改一个 target 的 include、链接或标准时走单点修正;新工程、新组件、换语言标准或抽公共函数时走整段重构。
|
|
17
|
-
- **前置**:存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md`、`environment.json` 和 `stages/stage-table.md`。`environment.json` 的 `standard` 是 target
|
|
17
|
+
- **前置**:存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md`、`environment.json` 和 `stages/stage-table.md`。`environment.json` 的 `standard` 是 target 标准的唯一来源;缺失或为空时按默认 `c++23` 继续并提示声明升级,取值非法或与现有 target 不一致时停在定 target,不猜标准。
|
|
18
18
|
- **边界**:target 图与 CMake 写法以本技能为权威,`building-cpp` 只读不修改;构建目录、编译器和工具版本由 `building-cpp` 的定构建负责,本技能只写 target 契约与工具配置。
|
|
19
19
|
|
|
20
20
|
## 三种操作
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
一次只选一种操作,范围(单点、整段)是任务属性;判据、边界与交接见共用入口 [operations.md](../shared/operations.md)。本技能从 [01-targets.md](steps/01-targets.md) 进;四类改动是域,不是操作。
|
|
23
23
|
|
|
24
24
|
| 操作 | 什么时候用 | 从哪步进 |
|
|
25
25
|
| --- | --- | --- |
|
|
@@ -46,28 +46,28 @@ metadata:
|
|
|
46
46
|
|
|
47
47
|
| 要做的事 | 打开 |
|
|
48
48
|
| --- | --- |
|
|
49
|
-
|
|
|
50
|
-
|
|
|
51
|
-
|
|
|
49
|
+
| 不知道开哪一份条文 | `references/index.md` |
|
|
50
|
+
| 定 target、include、链接、输出目录与警告 | `references/cmake/targets/01-target-rules.md`;案例在 `references/cmake/targets/examples/` |
|
|
51
|
+
| 定语言标准与档位 | `references/cmake/standards/01-cxx-standard.md`;五档细节在 `references/cmake/standards/versions.md` |
|
|
52
|
+
| 抽 CMake 函数与模块 | `references/cmake/modules/01-functions.md` |
|
|
52
53
|
| 链接方式、rpath、undefined reference、产物核对 | 按问题只开一份:`references/cmake/linking/01-libraries.md`、`02-rpath.md`、`03-undefined-reference.md`、`04-inspect.md` |
|
|
53
|
-
| CTest 注册、断言与用例拆分 | `references/cmake/testing/ctest.md` |
|
|
54
|
+
| CTest 注册、断言与用例拆分 | `references/cmake/testing/01-ctest.md` |
|
|
54
55
|
| 编码、BOM 与行尾 | `references/formatting/01-encoding.md` |
|
|
55
|
-
| clang-format | `references/formatting/02-clang-format.md`;案例 `references/formatting/examples/clang-format.md` |
|
|
56
|
-
| clang-tidy 与检查分级 | `references/formatting/03-clang-tidy.md`;案例 `references/formatting/examples/clang-tidy.md` |
|
|
57
|
-
| clangd 与编辑器补全 | `references/formatting/04-clangd.md`;案例 `references/formatting/examples/clangd.md` |
|
|
56
|
+
| clang-format | `references/formatting/02-clang-format.md`;案例 `references/formatting/examples/01-clang-format.md` |
|
|
57
|
+
| clang-tidy 与检查分级 | `references/formatting/03-clang-tidy.md`;案例 `references/formatting/examples/02-clang-tidy.md` |
|
|
58
|
+
| clangd 与编辑器补全 | `references/formatting/04-clangd.md`;案例 `references/formatting/examples/03-clangd.md` |
|
|
58
59
|
| 可复制的 CMake 函数 | `references/cmake/assets/CompileSettings.cmake`、`references/cmake/assets/LinkSettings.cmake` |
|
|
60
|
+
| 操作判不准、规则冲突 | `../shared/operations.md`、`../shared/rule-priority.md` |
|
|
59
61
|
|
|
60
62
|
## 加载预算
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
- 条文只开该步点名的一份;同号案例留到下一次激活;排版工具一次只开入口表里的那一份。
|
|
64
|
-
- 标准档只打开项目技能声明的那一档,没有声明时用本技能已有的新工程默认,不把其余标准档读进来。
|
|
64
|
+
加载预算见 [budget.md](../shared/budget.md);engineering 的步骤固定 3 份。
|
|
65
65
|
|
|
66
|
-
项目技能列出了检查命令时只跑那一条,没有时按 `steps/03-check.md` 的 configure 与构建命令跑;退出码为 0 后停在这一步,不再继续走没有失败的步骤。五档标准的语言设施、工具映射与降档由 `references/cmake/
|
|
66
|
+
项目技能列出了检查命令时只跑那一条,没有时按 `steps/03-check.md` 的 configure 与构建命令跑;退出码为 0 后停在这一步,不再继续走没有失败的步骤。五档标准的语言设施、工具映射与降档由 `references/cmake/standards/` 维护;本技能只负责这些标准在 CMake 里怎么写,以及三份工具配置各自的最低版本。
|
|
67
67
|
|
|
68
68
|
## 四类改动
|
|
69
69
|
|
|
70
|
-
四类改动是操作内部的域:操作定入口,域定层次。四域的定义、边界与次序见
|
|
70
|
+
四类改动是操作内部的域:操作定入口,域定层次。四域的定义、边界与次序见 [operations.md](../shared/operations.md);engineering 的一次改动混了两类时,按「内容 → 结构 → 版式 → 措辞」拆开做,前一类落定并经检查再进下一类。
|
|
71
71
|
|
|
72
72
|
| 类型 | 典型请求 | 入口步骤 |
|
|
73
73
|
| --- | --- | --- |
|
|
@@ -79,9 +79,9 @@ metadata:
|
|
|
79
79
|
## 完成判据
|
|
80
80
|
|
|
81
81
|
- [ ] target 图显式:程序 target 叫 `app`,库 target 用组件名,程序链接库,库的公共 include 用 `PUBLIC`;没有全局 include、全局编译选项或 GLOB。
|
|
82
|
-
- [ ] 每个 target 的标准来自 `environment.json`;`target_compile_features` 与 `CXX_STANDARD`、`CXX_STANDARD_REQUIRED ON`、`CXX_EXTENSIONS OFF` 同时存在,live 的 `compile_commands.json`
|
|
82
|
+
- [ ] 每个 target 的标准来自 `environment.json`;`target_compile_features` 与 `CXX_STANDARD`、`CXX_STANDARD_REQUIRED ON`、`CXX_EXTENSIONS OFF` 同时存在,live 的 `compile_commands.json` 中每个 target 的 `-std=` 都与声明一致,且每个 target 只有一条、不同值即失败,不是 `gnu++NN`。
|
|
83
83
|
- [ ] 复用的 CMake 函数放在工程根的 `cmake/` 模块里(模板源在 `references/cmake/assets/`),文件名用 UpperCamelCase,函数名用 `<ModuleName>_` 加 PascalCase,有目标与参数校验,根 `CMakeLists.txt` 通过 `CMAKE_MODULE_PATH` 导入;没有重复片段和未使用的旧模块。
|
|
84
|
-
- [ ] 公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` / `:keyword` 标参数;不写 `#[[.rst:` 与 `.. command::`,完整写法见 `references/cmake/targets/rules.md`;函数组之间用 `#---`,不画 `#===...===#` 方框。
|
|
84
|
+
- [ ] 公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` / `:keyword` 标参数;不写 `#[[.rst:` 与 `.. command::`,完整写法见 `references/cmake/targets/01-target-rules.md`;函数组之间用 `#---`,不画 `#===...===#` 方框。
|
|
85
85
|
- [ ] `CMakeLists.txt` 与 `*.cmake` 用 UTF-8 无 BOM、LF、2 空格缩进、不用 tab;行宽按团队约定,默认 80 列。
|
|
86
86
|
- [ ] 编码与排版工具各管一段:`.clang-format` 管空白与包含顺序,`.clang-tidy` 管检查分级,`.clangd` 管编辑器补全;三份都不把标准写死成与 `environment.json` 冲突的值,也不把 clang-format 塞进构建脚本。
|
|
87
87
|
- [ ] configure 与构建通过,构建产物不落在源码目录;没有改源码语义,也没有覆盖已有工程文件。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:链接与库边界;预算:一次只开这一份(62 行),不自动串联。 -->
|
|
1
2
|
# 链接与库边界
|
|
2
3
|
|
|
3
4
|
## 四个阶段
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
- 普通函数的定义放 `.cpp`;在头文件里写非 `inline` 函数定义,多个翻译单元包含时会重复定义。
|
|
22
23
|
- 类定义放头文件;成员函数在类外定义时写 `ClassName::`,定义放 `.cpp`。
|
|
23
24
|
- 只用到声明时不要包含完整定义;前置声明可以减少编译依赖,但不能替代需要完整类型的场景(继承、成员对象、按值返回)。
|
|
24
|
-
- 每个头文件自包含:单独编译一个只包含该头文件的 `.cpp`
|
|
25
|
+
- 每个头文件自包含:单独编译一个只包含该头文件的 `.cpp` 应通过。这是编译单元边界的要求,不是链接要求。用路径式 `#define` 守卫防止重复包含,不用 `#pragma once`。
|
|
25
26
|
|
|
26
27
|
## 静态库与共享库
|
|
27
28
|
|
|
@@ -35,13 +36,15 @@
|
|
|
35
36
|
- 共享库的 ABI 包括类布局、虚表、标准库类型和编译器选项;不同编译器或 `-stdlib` 混用可能链接成功但运行崩溃。
|
|
36
37
|
- 一个项目里不要同时链接两套标准库(libstdc++ 与 libc++);编译器、clangd、库和可执行文件必须一致。
|
|
37
38
|
|
|
38
|
-
##
|
|
39
|
+
## 构建脚本里的建议顺序
|
|
40
|
+
|
|
41
|
+
这是构建入口的推荐顺序,不是链接器的硬性要求:
|
|
39
42
|
|
|
40
43
|
1. 配置:CMake 生成构建系统,导出编译数据库。
|
|
41
44
|
2. 编译 + 链接:构建所有 target。
|
|
42
45
|
3. 产物检查:确认 `bin/app`、`lib/`、编译数据库存在。
|
|
43
|
-
4.
|
|
44
|
-
5.
|
|
46
|
+
4. 测试:`ctest` 跑注册的测试。
|
|
47
|
+
5. 运行:用项目入口命令启动,检查退出码和输出。
|
|
45
48
|
|
|
46
49
|
不要用“链接成功”代替“运行成功”;也不要用“测试通过”代替“产物存在”。
|
|
47
50
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:undefined reference 分诊;预算:一次只开这一份(20 行),不自动串联。 -->
|
|
1
2
|
# undefined reference 分诊
|
|
2
3
|
|
|
3
4
|
## `undefined reference` 分诊
|
|
@@ -6,8 +7,8 @@
|
|
|
6
7
|
| --- | --- | --- |
|
|
7
8
|
| 链接器说某个成员函数未定义 | 声明了但没写定义,或 `.cpp` 没加入 target | 补定义,或把源文件加入 `target_sources` |
|
|
8
9
|
| 只有某个平台失败 | 条件编译把定义排除了 | 检查宏和平台分支,保证每个平台都有定义 |
|
|
9
|
-
| 第三方库符号缺失 |
|
|
10
|
-
| C++ 符号被当成 C 符号 | 头文件缺少 `extern
|
|
10
|
+
| 第三方库符号缺失 | 没链接库,或静态库相互依赖顺序不对 | `target_link_libraries` 加库;CMake 传播 target 依赖,静态库相互依赖时仍要检查顺序 |
|
|
11
|
+
| C++ 符号被当成 C 符号 | 头文件缺少 `extern "C"` | C 接口头加 `extern "C"`,C++ 调用方也可见 |
|
|
11
12
|
| 模板成员未定义 | 模板定义在 `.cpp`,实例化点看不到 | 模板定义移到头文件,或显式实例化 |
|
|
12
13
|
| 静态库中的目标文件没被拉入 | 链接器只取被引用符号所在的目标文件 | 确认符号确实被引用,或改用对象库/共享库 |
|
|
13
14
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:验收:file 与 ldd;预算:一次只开这一份(20 行),不自动串联。 -->
|
|
1
2
|
# 验收:file 与 ldd
|
|
2
3
|
|
|
3
4
|
## 验收:`file` 与 `ldd`
|
|
@@ -18,4 +19,3 @@ ldd build/clang/bin/app
|
|
|
18
19
|
|
|
19
20
|
- [`file(1)`](https://man7.org/linux/man-pages/man1/file.1.html):识别文件格式、架构与位数。
|
|
20
21
|
- [`ldd(1)`](https://man7.org/linux/man-pages/man1/ldd.1.html):动态库依赖解析与 `not found` 的含义。
|
|
21
|
-
- [CMake: `add_custom_command`](https://cmake.org/cmake/help/latest/command/add_custom_command.html):把产物检查接进构建流程。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake 模块与函数;预算:一次只开这一份(108 行),不自动串联。 -->
|
|
1
2
|
# CMake 模块与函数
|
|
2
3
|
|
|
3
4
|
目标:同一段构建逻辑只有一份权威,函数可导入、可校验、可测试,目标目录只表达差异。
|
|
@@ -22,7 +23,7 @@ project/
|
|
|
22
23
|
LinkSettings.cmake
|
|
23
24
|
component/
|
|
24
25
|
CMakeLists.txt
|
|
25
|
-
|
|
26
|
+
app/
|
|
26
27
|
CMakeLists.txt
|
|
27
28
|
```
|
|
28
29
|
|
|
@@ -30,7 +31,7 @@ project/
|
|
|
30
31
|
- 文件名用 UpperCamelCase,与 CMake 官方 `Modules/` 一致;按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`。不用 kebab-case,也不用现在分词。
|
|
31
32
|
- 函数名用 `<ModuleName>_` 加 PascalCase:`CompileSettings_SetCxxStandard`、`LinkSettings_FetchGoogleTest`。前缀取模块文件名的原样大小写,复制到工程后不改名。
|
|
32
33
|
- 工程自己新增、不准备回流的函数才用项目前缀加 snake_case,例如 `my_project_do_something`。
|
|
33
|
-
-
|
|
34
|
+
- 只有项目真的需要时才建工程根的 `cmake/` 模块目录;一个函数都没有时不建空目录。
|
|
34
35
|
|
|
35
36
|
## 导入
|
|
36
37
|
|
|
@@ -75,7 +76,7 @@ endfunction()
|
|
|
75
76
|
|
|
76
77
|
## 契约注释
|
|
77
78
|
|
|
78
|
-
公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` 标位置参数、`:keyword` 标关键字参数;不写 `#[[.rst:` 与 `.. command::`,那是进 CMake 手册的模块才用的标记。同一文件的函数组之间用一条 `#---` 分隔,行注释只写原因,不画 `#===...===#` 方框。完整写法见 `../targets/rules.md`。
|
|
79
|
+
公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` 标位置参数、`:keyword` 标关键字参数;不写 `#[[.rst:` 与 `.. command::`,那是进 CMake 手册的模块才用的标记。同一文件的函数组之间用一条 `#---` 分隔,行注释只写原因,不画 `#===...===#` 方框。完整写法见 `../targets/01-target-rules.md`。
|
|
79
80
|
|
|
80
81
|
## 测试
|
|
81
82
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:C++ 标准与 CMake;预算:一次只开这一份(68 行),不自动串联。 -->
|
|
1
2
|
# C++ 标准与 CMake
|
|
2
3
|
|
|
3
4
|
目标:一个 target 只声明一个标准,声明在 target 上,编译数据库里的实际参数与声明一致。
|
|
@@ -53,7 +54,7 @@ CompileSettings_SetCxxStandard(greeting PUBLIC c++20)
|
|
|
53
54
|
|
|
54
55
|
## 验证
|
|
55
56
|
|
|
56
|
-
configure 后读 `build/<compiler>/compile_commands.json
|
|
57
|
+
configure 后读 `build/<compiler>/compile_commands.json`,逐条核对每个 target 的 `-std=`:
|
|
57
58
|
|
|
58
59
|
```bash
|
|
59
60
|
grep -o -- '-std=[^ ]*' build/clang/compile_commands.json | sort -u
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:正反例:标准只写一半;预算:一次只开这一份(19 行),不自动串联。 -->
|
|
1
2
|
# 正反例:标准只写一半
|
|
2
3
|
|
|
3
4
|
**差**:
|
|
@@ -8,7 +9,7 @@ target_compile_features(app PRIVATE cxx_std_20)
|
|
|
8
9
|
set_target_properties(app PROPERTIES CXX_STANDARD 20)
|
|
9
10
|
```
|
|
10
11
|
|
|
11
|
-
问题:meta-feature 只保证“至少”,`CXX_STANDARD`
|
|
12
|
+
问题:meta-feature 只保证“至少”,`CXX_STANDARD` 又允许静默降级,实际标准可能不是 20。
|
|
12
13
|
|
|
13
14
|
**好**:
|
|
14
15
|
|
package/skills/engineering/references/cmake/{standard/standards/README.md → standards/index.md}
RENAMED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:语言标准档案;预算:一次只开这一份(165 行),不自动串联。 -->
|
|
1
2
|
# 语言标准档案
|
|
2
3
|
|
|
3
4
|
本工作流默认 **C++23**,但一个工程只选一个 `standard`,不为同一份源码承诺“同时兼容 C++11–C++23”。标准是项目级事实,写在项目的 `environment.json`(本书在 `.agents/skills/cpp-workflow-project/environment.json`)。
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
各档的编译器下限、能用/不能假设、降档要改什么集中在 [versions.md](versions.md);`c++11/`–`c++23/` 只保留这一档的 `cmake-snippet.cmake`。模板仍然只有一套公共骨架,靠 `__cplusplus` 条件选择实现,不复制五份模板。
|
|
6
7
|
|
|
7
|
-
Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++23 是一处明确偏离;Google 的排版、命名、头文件和异常之外的规则不变。异常政策仍按 [
|
|
8
|
+
Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++23 是一处明确偏离;Google 的排版、命名、头文件和异常之外的规则不变。异常政策仍按 [01-exception-rules.md](../../../../developing-cpp/references/exceptions/01-exception-rules.md)。
|
|
8
9
|
|
|
9
10
|
## 选择标准
|
|
10
11
|
|
|
11
|
-
| 标准 |
|
|
12
|
+
| 标准 | 差异 | 一句话 |
|
|
12
13
|
| --- | --- | --- |
|
|
13
|
-
| C++11 | [
|
|
14
|
-
| C++14 | [
|
|
15
|
-
| C++17 | [
|
|
16
|
-
| C++20 | [
|
|
17
|
-
| C++23 | [
|
|
14
|
+
| C++11 | [versions.md#c11](versions.md#c11) | 老工具链、老库仍要维护时的最低档 |
|
|
15
|
+
| C++14 | [versions.md#c14](versions.md#c14) | 需要 `make_unique`、泛型 lambda,但还不想上 C++17 |
|
|
16
|
+
| C++17 | [versions.md#c17](versions.md#c17) | 需要 `string_view`、`optional`、`if constexpr` 的稳定基线 |
|
|
17
|
+
| C++20 | [versions.md#c20](versions.md#c20) | 需要 concepts、ranges、`std::format` 的现代基线 |
|
|
18
|
+
| C++23 | [versions.md#c23](versions.md#c23) | 本工作流默认;需要 `print`、`expected` 等设施时选它 |
|
|
18
19
|
|
|
19
|
-
新工程写 `c++23`;已有工程按现有 `CMakeLists.txt`
|
|
20
|
+
新工程写 `c++23`;已有工程按现有 `CMakeLists.txt` 和编译器能力选,不为了“统一”擅自升降。降档前先读 `versions.md` 对应一节的“不能假设”和“降档要改什么”。
|
|
20
21
|
|
|
21
22
|
## 三个规划粒度
|
|
22
23
|
|
|
@@ -24,14 +25,14 @@ Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++2
|
|
|
24
25
|
| --- | --- | --- |
|
|
25
26
|
| 项目 | 本页选择表、编译器/库门槛 | `environment.json` 的 `standard`,以及“为什么选这档” |
|
|
26
27
|
| target | 该档的 `cmake-snippet.cmake` | `cxx_std_*`、`CXX_STANDARD`、`CXX_STANDARD_REQUIRED`、`CXX_EXTENSIONS` |
|
|
27
|
-
| 代码/工具 |
|
|
28
|
+
| 代码/工具 | `versions.md` 的“能用/不能假设/降档” | 设施替换、`.clang-format` 档位、`.clangd` 不写 `-std` |
|
|
28
29
|
| 验证 | 本页验证矩阵、`scripts/checking-standard-matrix.sh` | configure/build/CTest 和 `compile_commands.json` 的精确 `-std` |
|
|
29
30
|
|
|
30
31
|
每个标准目录都按这四层写;不要只在项目层改 `standard`,却在 target 层留着 C++23 的 `cxx_std_23`。
|
|
31
32
|
|
|
32
33
|
## 共用矩阵
|
|
33
34
|
|
|
34
|
-
| `standard` | CMake meta-feature | `CXX_STANDARD` | `__cplusplus` | clang-format
|
|
35
|
+
| `standard` | CMake meta-feature | `CXX_STANDARD` | `__cplusplus` | clang-format | 模板实用下限(含标准库设施) |
|
|
35
36
|
| --- | --- | --- | --- | --- | --- |
|
|
36
37
|
| `c++11` | `cxx_std_11` | 11 | `201103L` | `c++11` | GCC 4.8 / Clang 3.4 |
|
|
37
38
|
| `c++14` | `cxx_std_14` | 14 | `201402L` | `c++14` | GCC 5 / Clang 3.4 |
|
|
@@ -69,22 +70,22 @@ set_target_properties(app PROPERTIES
|
|
|
69
70
|
```
|
|
70
71
|
|
|
71
72
|
- `cxx_std_23` 是“至少”,会向依赖它的 target 传播。
|
|
72
|
-
- `CXX_STANDARD` 是请求值;`CXX_STANDARD_REQUIRED ON`
|
|
73
|
+
- `CXX_STANDARD` 是请求值;`CXX_STANDARD_REQUIRED ON` 防止编译器不支持时静默降级。
|
|
73
74
|
- `CXX_EXTENSIONS OFF` 把 `-std=gnu++23` 收成 `-std=c++23`。
|
|
74
|
-
- configure 后读 `compile_commands.json`
|
|
75
|
+
- configure 后读 `compile_commands.json` 中每个 target 的 `-std=`;每个 target 只能有一条,必须是 `-std=c++NN`,不能是 `gnu++NN`,不同值即失败。
|
|
75
76
|
- CMake 最低 3.20(模板统一要求);只单独使用低档片段时,C++11/14 的 `cxx_std_*` meta-feature 最低 3.1,C++17 为 3.8,C++20 为 3.12。C++23 的 `CXX_STANDARD 23` 自 3.20 才有。
|
|
76
77
|
|
|
77
78
|
### clang-format
|
|
78
79
|
|
|
79
|
-
clang-format 21/22 的 `Standard` 只到 `c++20
|
|
80
|
+
clang-format 21/22 的 `Standard` 只到 `c++20`,23 起接受 `c++23`。为兼容旧工具,映射如下:
|
|
80
81
|
|
|
81
82
|
| 声明 | 配置 |
|
|
82
83
|
| --- | --- |
|
|
83
84
|
| `c++11`–`c++20` | 对应 `Standard: c++NN` |
|
|
84
|
-
| `c++23` | `Standard: Latest`(21/22 没有 `c++23`) |
|
|
85
|
+
| `c++23` | `Standard: Latest`(21/22 没有 `c++23`;23 起也可写 `c++23`) |
|
|
85
86
|
| `c++26`(未来) | 工具支持时写 `c++26`,否则 `Latest` |
|
|
86
87
|
|
|
87
|
-
|
|
88
|
+
在 21/22 上不要为了“看起来精确”写 `c++23`,会报 `unknown enumerated scalar` 并退出 1;23 起可以直接写。
|
|
88
89
|
|
|
89
90
|
### clangd
|
|
90
91
|
|
|
@@ -151,7 +152,7 @@ target_link_options(app PRIVATE -stdlib=libc++)
|
|
|
151
152
|
|
|
152
153
|
增加 C++26 或更高标准时,不复制模板,按这个顺序做:
|
|
153
154
|
|
|
154
|
-
1.
|
|
155
|
+
1. 在 `versions.md` 增加 C++26 一节,新建 `standards/c++26/cmake-snippet.cmake` 并改数字。
|
|
155
156
|
2. 更新本页矩阵和 `checking-environment.mjs` 的 `STANDARDS`。
|
|
156
157
|
3. 确认 CMake 的 `CXX_STANDARD 26`、clang-format 的 `Standard` 能力、编译器/标准库门槛。
|
|
157
158
|
4. 在四套模板 × 新档上跑 configure/build/CTest,并在 `compile_commands.json` 核对 `-std=c++26`。
|
|
@@ -159,7 +160,7 @@ target_link_options(app PRIVATE -stdlib=libc++)
|
|
|
159
160
|
|
|
160
161
|
## 相关文件
|
|
161
162
|
|
|
162
|
-
- [environment.md](
|
|
163
|
-
- [cxx-standard.md](
|
|
164
|
-
- [clang-tidy.md](
|
|
165
|
-
- [01-build.md](
|
|
163
|
+
- [02-environment.md](../../../../building-cpp/references/02-environment.md):四轴声明与校验。
|
|
164
|
+
- [01-cxx-standard.md](01-cxx-standard.md):C++11–C++23 在 CMake 里的两层声明与验证。
|
|
165
|
+
- [03-clang-tidy.md](../../formatting/03-clang-tidy.md):clang-format / clang-tidy / clangd 分工。
|
|
166
|
+
- [01-build.md](../../../../building-cpp/steps/01-build.md):定构建步骤。
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:C++11–C++23 各档差异;预算:一次只开这一份(43 行),不自动串联。 -->
|
|
2
|
+
# 各标准差异
|
|
3
|
+
|
|
4
|
+
本页只列每档相对前一档多出什么、不能假设什么、降到这一档要改什么。选择表、共用矩阵、CMake/clang-format/clangd 共用规则与验证矩阵在 [index.md](index.md);每档的 CMake 片段在 `c++NN/cmake-snippet.cmake`。
|
|
5
|
+
|
|
6
|
+
## C++11
|
|
7
|
+
|
|
8
|
+
- 能用:`auto`、范围 `for`、lambda、右值引用与移动语义、`std::unique_ptr`/`shared_ptr`、`std::thread`/`chrono`、`enum class`、`override`/`final`、变参模板、受限 `constexpr`、`std::array`、`unordered_map`/`unordered_set`。
|
|
9
|
+
- 不能假设:`std::make_unique`、泛型 lambda、变量模板、返回类型推导、`std::string_view`、`std::optional`、`std::variant`、`if constexpr`、结构化绑定、`[[nodiscard]]`、`std::filesystem`、`std::format`、`std::print`;不要用 C++14 的放宽 `constexpr` 写循环或多语句函数。
|
|
10
|
+
- 降档/升级:这是最低档;从 C++14 降到这里,先去掉 `std::make_unique`、泛型 lambda、返回类型推导和变量模板。`std::unique_ptr<T>(new T(...))` 只作过渡,不要在新代码里扩散。
|
|
11
|
+
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++11`,不能是 `gnu++11`;模板的 `__cplusplus` 分支走字符串拼接。
|
|
12
|
+
- 片段:[cmake-snippet.cmake](c++11/cmake-snippet.cmake)。
|
|
13
|
+
|
|
14
|
+
## C++14
|
|
15
|
+
|
|
16
|
+
- 能用:C++11 的全部内容;`std::make_unique`、泛型 lambda、变量模板、返回类型推导、二进制字面量、数字分隔符、放宽的 `constexpr`、`std::integer_sequence`。
|
|
17
|
+
- 不能假设:`std::string_view`、`std::optional`、`std::variant`、`if constexpr`、结构化绑定、`[[nodiscard]]`、`std::filesystem`、`std::format`、`std::print`;不要把 C++17 的写法靠编译器扩展通过。
|
|
18
|
+
- 降档/升级:从 C++17 降到这里,把 `string_view` 改成 `const std::string&`,把 `optional` 改成 `bool` + 输出参数或项目结果类型,把 `if constexpr` 改成标签分派或重载。升级到 C++17 后也不要立刻把公共接口全改成 `string_view`,先看调用方生命周期。
|
|
19
|
+
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++14`;模板在 C++14 下走字符串拼接。
|
|
20
|
+
- 片段:[cmake-snippet.cmake](c++14/cmake-snippet.cmake)。
|
|
21
|
+
|
|
22
|
+
## C++17
|
|
23
|
+
|
|
24
|
+
- 能用:C++14 的全部内容;`std::string_view`、`std::optional`、`std::variant`、`std::any`、`if constexpr`、结构化绑定、`[[nodiscard]]`、内联变量、嵌套命名空间、`std::filesystem`、并行算法(库实现和链接方式另看)。
|
|
25
|
+
- 不能假设:concepts、ranges、`std::format`、`std::span`、`std::bit_cast`、`std::print`、`std::expected`、`std::stacktrace`、`= default` 的比较运算符;`std::filesystem` 在 GCC 8 需要额外链接 `stdc++fs`,GCC 9 起才稳定。
|
|
26
|
+
- 降档/升级:从 C++20 降到这里,把 concepts 改成 `enable_if`/重载,把 ranges 改成迭代器算法,把 `std::format` 改成 `std::ostringstream`,把 `= default` 的比较运算符改回结构体外的手写自由函数。升级到 C++20 后先确认 `std::format` 的库门槛(libstdc++ 13+ / libc++ 14+),不要只看编译器版本。
|
|
27
|
+
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++17`;模板在 C++17 下仍走字符串拼接。
|
|
28
|
+
- 片段:[cmake-snippet.cmake](c++17/cmake-snippet.cmake)。
|
|
29
|
+
|
|
30
|
+
## C++20
|
|
31
|
+
|
|
32
|
+
- 能用:C++17 的全部内容;concepts、requires、ranges、`std::span`、`std::bit_cast`、`std::source_location`、`std::format`、协程、`std::jthread`、三路比较;调用方需要按成员逐项比较时写 `bool operator==(const T&) const = default`,C++23 沿用;模块语法可用,但工具链支持不完整,模板不使用。
|
|
33
|
+
- 不能假设:`std::print`、`std::expected`、`std::stacktrace`、`if consteval`、显式对象参数;`std::format` 不只看语言核心:libstdc++ 13+,libc++ 14+ 实现完成,但特性宏到 19 才稳定;模块、协程的构建支持因编译器/构建系统而异。
|
|
34
|
+
- 降档/升级:从 C++23 降到这里,把 `std::print` 改成 `std::format` + `std::cout`,把 `std::expected` 改成 `std::optional` 或项目结果类型,去掉 `if consteval`。升级到 C++23 后仍要逐项确认库实现,不要因为 `-std=c++23` 通过就假定 `print`/`stacktrace` 可用。
|
|
35
|
+
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++20`;模板在 C++20 下走 `std::format` 分支。
|
|
36
|
+
- 片段:[cmake-snippet.cmake](c++20/cmake-snippet.cmake)。
|
|
37
|
+
|
|
38
|
+
## C++23
|
|
39
|
+
|
|
40
|
+
- 能用:C++20 的全部内容;`std::print`/`std::println`、`std::expected`、`std::stacktrace`、`if consteval`、显式对象参数(deducing this)、`std::mdspan`、`std::flat_map`、ranges 的 C++23 增补(工具链实现为准)。
|
|
41
|
+
- 不能假设:C++23 在 GCC/Clang 上仍是 Partial,具体设施要查编译器状态页;`std::print` 的库门槛是 libstdc++ 14+ / libc++ 19+;`std::stacktrace` 还要链接对应库;`std::expected` 的 monadic 接口、`std::flat_map` 等设施在旧标准库上可能缺失;模板只用 `std::format`,不因为默认 C++23 就强依赖 `print`/`stacktrace`。
|
|
42
|
+
- 降档/升级:从 C++23 降到 C++20 见上一节;升级到 C++26 前先确认 CMake 3.25+、编译器/标准库支持、clang-format 的 `c++26` 枚举,并按 [index.md](index.md#新增标准) 的流程新增片段与矩阵行。
|
|
43
|
+
- 验证:`compile_commands.json` 的最后一个 `-std=` 必须是 `-std=c++23`;模板在 C++23 下走 `std::format` 分支;若使用 `print`/`stacktrace`,另做链接测试。
|
|
44
|
+
- 片段:[cmake-snippet.cmake](c++23/cmake-snippet.cmake)。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:CMake;预算:一次只开这一份(227 行),不自动串联。 -->
|
|
1
2
|
# CMake
|
|
2
3
|
|
|
3
4
|
目标:一个编译器、Ninja、target-based 规则、可用的编译数据库。下面是最小形状和常见边界;工程模板仍由 `building-cpp` 提供,在 `../../../../building-cpp/assets/templates/projects/`,可复用的 CMake 函数在本技能的 `assets/`:`CompileSettings.cmake` 收标准与警告,`LinkSettings.cmake` 收构建期 RPATH 与 GoogleTest 依赖。
|
|
@@ -61,19 +62,19 @@ CompileSettings_SetWarnings(greeting PRIVATE LEVEL standard)
|
|
|
61
62
|
|
|
62
63
|
- 库的 `PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include` 让程序与链接它的测试写 `#include "greeting.h"` 就能找到公共头,不需要各自再写 include 路径。
|
|
63
64
|
- 标准用 `CompileSettings_SetCxxStandard`:库用 `PUBLIC` 传播“至少 C++NN”,程序用 `PRIVATE`;`CXX_STANDARD`/`CXX_EXTENSIONS` 不传播,每个会编译的 target 都要自己调用一次。
|
|
64
|
-
- `CXX_STANDARD_REQUIRED ON` 防止 CMake
|
|
65
|
+
- `CXX_STANDARD_REQUIRED ON` 防止 CMake 在编译器不支持时静默降级;`CXX_EXTENSIONS OFF` 把 `-std=gnu++NN` 收成 `-std=c++NN`。configure 后检查 `compile_commands.json` 的最后一条 `-std=`。
|
|
65
66
|
- 程序 target 固定 `app`;库 target 用组件名。
|
|
66
67
|
|
|
67
68
|
## 标准与复用
|
|
68
69
|
|
|
69
|
-
C++11–C++23 的两层声明、可见性、常见错误与验证见 `../
|
|
70
|
+
C++11–C++23 的两层声明、可见性、常见错误与验证见 `../standards/01-cxx-standard.md`。通用函数 `CompileSettings_SetCxxStandard(target visibility c++NN)` 在 `../assets/CompileSettings.cmake`:
|
|
70
71
|
|
|
71
72
|
```cmake
|
|
72
73
|
CompileSettings_SetCxxStandard(app PRIVATE c++23)
|
|
73
74
|
CompileSettings_SetCxxStandard(greeting PUBLIC c++20)
|
|
74
75
|
```
|
|
75
76
|
|
|
76
|
-
函数名不绑定单一标准;新增档位只改函数里的支持列表。模块何时抽、文件怎么放、怎么导入与测试见 `../modules/functions.md`。
|
|
77
|
+
函数名不绑定单一标准;新增档位只改函数里的支持列表。模块何时抽、文件怎么放、怎么导入与测试见 `../modules/01-functions.md`。
|
|
77
78
|
|
|
78
79
|
## 警告等级
|
|
79
80
|
|
|
@@ -90,11 +91,11 @@ MSVC 分支用 `/W4`、`/permissive-`、`/WX`;其他编译器直接报错,
|
|
|
90
91
|
- 每个 target 自己声明 include、标准、编译选项、链接库;不要用全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS` 给所有 target 加料。
|
|
91
92
|
- `target_include_directories(<target> PUBLIC|PRIVATE <dir>)`:公共头给 `PUBLIC`,私有实现目录给 `PRIVATE`。
|
|
92
93
|
- `target_compile_features(<target> PUBLIC|PRIVATE cxx_std_NN)`:只传播“至少 C++NN”的最低要求;精确 `-std` 由 `CompileSettings_SetCxxStandard` 在会编译的 target 上设置,不靠这一步锁死。
|
|
93
|
-
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC`。
|
|
94
|
+
- `target_link_libraries(<target> PRIVATE|PUBLIC <lib>)`:实现依赖用 `PRIVATE`,接口依赖用 `PUBLIC`。CMake 只硬性要求第一个参数已创建;被链接的库可以稍后定义,CMake 会补 ordering dependency。工程约定仍是先定义后引用,避免靠隐式排序。
|
|
94
95
|
- 不要 `file(GLOB ...)`;显式列源文件,新增文件时改 CMake,构建系统才知道。`CONFIGURE_DEPENDS` 依赖构建系统每次检查目录,跨生成器行为也不完全一致,不作为工程规则。
|
|
95
96
|
- 不把 `main()` 编进库;程序只做装配和 I/O。
|
|
96
97
|
- 头文件不参与编译时,不需要 `target_sources` 列出;需要 IDE 展示时再列。
|
|
97
|
-
- 组件多起来时,一个组件一个 `CMakeLists.txt`:库放 `<component>/include` 与 `<component>/src`,程序放 `
|
|
98
|
+
- 组件多起来时,一个组件一个 `CMakeLists.txt`:库放 `<component>/include` 与 `<component>/src`,程序放 `app/include` 与 `app/src`;`app/include` 只给程序内部用,不要被库 `PUBLIC` 导出。
|
|
98
99
|
- 测试 target 等出现第二个消费者时再加,不要为单个 `main()` 提前引入测试框架;测试只链接库的公共接口。
|
|
99
100
|
|
|
100
101
|
## 工程名
|
|
@@ -191,6 +192,34 @@ LinkSettings_FetchGoogleTest()
|
|
|
191
192
|
- 在 `CMakeLists.txt` 里 `execute_process` 下载依赖或改源码。
|
|
192
193
|
- 用 `CMAKE_CXX_FLAGS` 加 `-std=c++23` 之类与声明不符的标准。
|
|
193
194
|
|
|
195
|
+
## 正反例
|
|
196
|
+
|
|
197
|
+
### CMake 函数注释
|
|
198
|
+
|
|
199
|
+
**差**:
|
|
200
|
+
|
|
201
|
+
```cmake
|
|
202
|
+
#[[.rst:
|
|
203
|
+
CompileSettings_SetWarnings
|
|
204
|
+
---------------------------
|
|
205
|
+
]]
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`#[[.rst:` 与 `.. command::` 只给要进 CMake 手册的模块用;`@param`、`@return` 是 C++ 的 Doxygen 标签,CMake 里不用;CMake 函数没有返回值,也不写 `:return:`。
|
|
209
|
+
|
|
210
|
+
**好**:
|
|
211
|
+
|
|
212
|
+
```cmake
|
|
213
|
+
# 给一个目标加上编译警告。
|
|
214
|
+
#
|
|
215
|
+
# 只改传入的目标。PRIVATE 不向使用方传播。
|
|
216
|
+
#
|
|
217
|
+
# :param target: 已经存在的 CMake 目标。
|
|
218
|
+
# :param visibility: 取 PRIVATE 或 INTERFACE。
|
|
219
|
+
# :keyword LEVEL: 可省略。取 basic、standard 或 strict;省略时按 standard。
|
|
220
|
+
function(CompileSettings_SetWarnings target visibility)
|
|
221
|
+
```
|
|
222
|
+
|
|
194
223
|
## 依据
|
|
195
224
|
|
|
196
225
|
- [CMake: `add_library`](https://cmake.org/cmake/help/latest/command/add_library.html):库 target 的类型与作用域。
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
<!-- 读取时机:配 CMake 或工具链时;读完能回答:测试与 CTest;预算:一次只开这一份(61 行),不自动串联。 -->
|
|
1
2
|
# 测试与 CTest
|
|
2
3
|
|
|
3
|
-
测试属于构建域:测试 target 由 `engineering` 写进 CMake
|
|
4
|
+
测试属于构建域:测试 target 由 `engineering` 写进 CMake;测试代码的写法由本文件说明,CTest 运行归 `building-cpp` 的通过检查。条文依据 GoogleTest Primer、CMake 的 `add_test` / `gtest_discover_tests` 文档与 CTest 手册,访问日期 2026-10-27。
|
|
4
5
|
|
|
5
6
|
## 先决定测什么
|
|
6
7
|
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
|
|
21
22
|
```cmake
|
|
22
23
|
include(GoogleTest)
|
|
23
|
-
gtest_discover_tests(
|
|
24
|
+
gtest_discover_tests(my_project_test
|
|
24
25
|
DISCOVERY_TIMEOUT 30
|
|
25
26
|
PROPERTIES LABELS unit TIMEOUT 10)
|
|
26
27
|
```
|
|
@@ -30,7 +31,7 @@ gtest_discover_tests(tictactoe_test
|
|
|
30
31
|
- 需要独占端口、固定文件或数据库的用例用 `RESOURCE_LOCK`;有准备/清理步骤时用 `FIXTURES_SETUP` / `FIXTURES_REQUIRED`。
|
|
31
32
|
- `--no-tests=error` 防止「0 个测试也算通过」;`--output-on-failure` 只在失败时打印输出,避免日志淹没。
|
|
32
33
|
|
|
33
|
-
##
|
|
34
|
+
## 运行顺序(building-cpp 执行)
|
|
34
35
|
|
|
35
36
|
1. 构建通过后跑 `ctest --test-dir build/<compiler> --output-on-failure --no-tests=error`。
|
|
36
37
|
2. 先跑失败用例的单条:`ctest -R <用例名> --output-on-failure`,确认失败可复现。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:编码与行尾;预算:一次只开这一份(49 行),不自动串联。 -->
|
|
1
2
|
# 编码与行尾
|
|
2
3
|
|
|
3
4
|
默认组合是 UTF-8 无 BOM、LF。机器规则以仓库根 `.gitattributes` 与 `.editorconfig` 为准,本文件只记决定、MSVC 例外与检查方法。
|
|
@@ -32,7 +33,7 @@ MSVC 默认按 BOM 判断源码编码。源码没有 BOM 时,它按当前用
|
|
|
32
33
|
在仓库根运行:
|
|
33
34
|
|
|
34
35
|
```bash
|
|
35
|
-
node "<skill-dir>/
|
|
36
|
+
node "<skill-dir>/scripts/checking-encoding.mjs" --profile portable
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
- `portable`(默认)要求所有文本文件 UTF-8 无 BOM、LF、末尾换行。
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:clang-format;预算:一次只开这一份(20 行),不自动串联。 -->
|
|
1
2
|
# clang-format
|
|
2
3
|
|
|
3
4
|
三件语言工具各管一段:`clang-format` 排版、`clang-tidy` 诊断、`clangd` 提供补全/跳转/诊断。它们都读同一份编译数据库,但职责不重叠。编辑器侧只保留 CMake Tools 作为配置、构建、运行和调试入口;C/C++ 扩展只做 `CMake: Debug` 的调试后端,不参与语言服务。
|
|
4
5
|
|
|
5
6
|
```bash
|
|
6
|
-
clang-format --dry-run --Werror src/*.cpp include/*.h tests/*.cpp
|
|
7
|
+
clang-format --dry-run --Werror src/*.cpp include/*.h app/*.cpp tests/*.cpp
|
|
7
8
|
```
|
|
8
9
|
|
|
9
10
|
- 检查用 `--dry-run --Werror`;需要自动修复时单独跑 `clang-format -i`,不要在构建脚本里悄悄改源码。
|
|
10
11
|
- 工程必须有根 `.clang-format`;缺失时先复制配置,不要回退到内置风格,回退会掩盖配置缺失。
|
|
11
|
-
- 只扫描项目源码:`src/`、`include/`、`
|
|
12
|
+
- 只扫描项目源码:`src/`、`include/`、`app/`、`tests/` 等实际存在的目录;不扫描 `build/`、第三方、生成文件。
|
|
12
13
|
- 配置文件在工程根 `.clang-format`;不要依赖用户主目录或编辑器内置配置。
|
|
13
|
-
- `BasedOnStyle: Google`、`Standard: Latest
|
|
14
|
+
- `BasedOnStyle: Google`、`Standard: Latest`;`c++23` 到 clang-format 23 才被接受,21 与 22 会报 `error: unknown enumerated scalar`,`Latest` 在旧版本上按该版本支持的最高标准解析。列宽、缩进与包含顺序以工程根 `.clang-format` 为准;工程显式写 `ColumnLimit` 时按工程值执行,模板资产不写 `ColumnLimit`。
|
|
14
15
|
- 格式化失败就是失败,不要用 `// clang-format off` 绕过;确需关闭时只包住无法排版的宏或表格,并写一句理由。
|
|
15
16
|
|
|
16
17
|
## 依据
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:clang-tidy 与静态检查门禁;预算:一次只开这一份(58 行),不自动串联。 -->
|
|
1
2
|
# clang-tidy 与静态检查门禁
|
|
2
3
|
|
|
3
4
|
## clang-tidy
|
|
4
5
|
|
|
5
6
|
```bash
|
|
6
7
|
clang-tidy -p build/clang \
|
|
7
|
-
--header-filter="^$(pwd -P)/(include|src|
|
|
8
|
+
--header-filter="^$(pwd -P)/(include|src|app|tests)/" \
|
|
8
9
|
--warnings-as-errors='*' \
|
|
9
|
-
src/*.cpp
|
|
10
|
+
src/*.cpp app/*.cpp tests/*.cpp
|
|
10
11
|
```
|
|
11
12
|
|
|
12
13
|
- `-p` 指向当前编译器的构建目录,读取 `compile_commands.json`。
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:clangd 与编辑器;预算:一次只开这一份(91 行),不自动串联。 -->
|
|
1
2
|
# clangd 与编辑器
|
|
2
3
|
|
|
3
4
|
## clangd
|
|
@@ -63,6 +64,7 @@ Diagnostics:
|
|
|
63
64
|
],
|
|
64
65
|
"cmake.buildDirectory": "${workspaceFolder}/build/clang",
|
|
65
66
|
"cmake.generator": "Ninja",
|
|
67
|
+
"cmake.configureOnOpen": true,
|
|
66
68
|
"cmake.buildBeforeRun": true,
|
|
67
69
|
"cmake.exportCompileCommandsFile": true,
|
|
68
70
|
"cmake.debugConfig": {
|
package/skills/engineering/references/formatting/examples/{clang-format.md → 01-clang-format.md}
RENAMED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
<!-- 读取时机:排版或工具配置时;读完能回答:正反例:clang-format;预算:一次只开这一份(38 行),不自动串联。 -->
|
|
1
2
|
# 正反例:clang-format
|
|
2
3
|
|
|
3
4
|
## 构建脚本里自动格式化
|
|
@@ -35,4 +36,4 @@ BasedOnStyle: Google
|
|
|
35
36
|
Standard: Latest
|
|
36
37
|
```
|
|
37
38
|
|
|
38
|
-
配置只选预设和语言版本,不重复策略;列宽、缩进、包含顺序都由 Google
|
|
39
|
+
配置只选预设和语言版本,不重复策略;列宽、缩进、包含顺序都由 Google 预设决定。需要新规则时先改技能文档并说明理由,不要在手写配置里堆本地约定。两例的差别在把策略写进配置头,不是 `ColumnLimit`;要改列宽时按工程值设置并改技能依据。
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<!-- 读取时机:入口表点名或判不准时;读完能回答:每份条文的读取时机、产出与预算;预算:一次只开这一份,按需再开单份。 -->
|
|
2
|
+
# 条文索引
|
|
3
|
+
|
|
4
|
+
先读本页,再按主题或语义开一份;不要一次打开多份。
|
|
5
|
+
|
|
6
|
+
## cmake
|
|
7
|
+
|
|
8
|
+
| 文件 | 读完能回答 | 预算 |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| `cmake/linking/01-libraries.md` | 链接与库边界 | 一次只开这一份(62 行),不自动串联。 |
|
|
11
|
+
| `cmake/linking/02-rpath.md` | 输出目录与 RPATH | 一次只开这一份(25 行),不自动串联。 |
|
|
12
|
+
| `cmake/linking/03-undefined-reference.md` | undefined reference 分诊 | 一次只开这一份(20 行),不自动串联。 |
|
|
13
|
+
| `cmake/linking/04-inspect.md` | 验收:file 与 ldd | 一次只开这一份(20 行),不自动串联。 |
|
|
14
|
+
| `cmake/modules/01-functions.md` | CMake 模块与函数 | 一次只开这一份(108 行),不自动串联。 |
|
|
15
|
+
| `cmake/standards/01-cxx-standard.md` | C++ 标准与 CMake | 一次只开这一份(68 行),不自动串联。 |
|
|
16
|
+
| `cmake/standards/versions.md` | C++11–C++23 各档差异 | 一次只开这一份(43 行),不自动串联。 |
|
|
17
|
+
| `cmake/standards/examples/01-standard.md` | 正反例:标准只写一半 | 一次只开这一份(19 行),不自动串联。 |
|
|
18
|
+
| `cmake/targets/01-target-rules.md` | CMake | 一次只开这一份(227 行),不自动串联。 |
|
|
19
|
+
| `cmake/targets/examples/01-rpath.md` | 正反例:共享库运行时路径 | 一次只开这一份(24 行),不自动串联。 |
|
|
20
|
+
| `cmake/testing/01-ctest.md` | 测试与 CTest | 一次只开这一份(61 行),不自动串联。 |
|
|
21
|
+
|
|
22
|
+
## formatting
|
|
23
|
+
|
|
24
|
+
| 文件 | 读完能回答 | 预算 |
|
|
25
|
+
| --- | --- | --- |
|
|
26
|
+
| `formatting/01-encoding.md` | 编码与行尾 | 一次只开这一份(49 行),不自动串联。 |
|
|
27
|
+
| `formatting/02-clang-format.md` | clang-format | 一次只开这一份(20 行),不自动串联。 |
|
|
28
|
+
| `formatting/03-clang-tidy.md` | clang-tidy 与静态检查门禁 | 一次只开这一份(58 行),不自动串联。 |
|
|
29
|
+
| `formatting/04-clangd.md` | clangd 与编辑器 | 一次只开这一份(91 行),不自动串联。 |
|
|
30
|
+
| `formatting/examples/01-clang-format.md` | 正反例:clang-format | 一次只开这一份(38 行),不自动串联。 |
|
|
31
|
+
| `formatting/examples/02-clang-tidy.md` | 正反例:clang-tidy | 一次只开这一份(43 行),不自动串联。 |
|
|
32
|
+
| `formatting/examples/03-clangd.md` | 正反例:clangd | 一次只开这一份(109 行),不自动串联。 |
|