@chengzhao-dev/dsh-cpp-workflow 0.6.19 → 0.7.1
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 +11 -26
- package/README.md +13 -28
- package/cordis.patch.yml +1 -1
- package/lib/client.js +33 -21
- package/lib/comment-policy.js +15 -6
- package/lib/index.js +1 -1
- package/lib/workspace.js +0 -15
- package/package.json +7 -7
- package/skills/building-cpp/SKILL.md +34 -21
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +5 -1
- package/skills/building-cpp/project/vscode/01-extensions.md +28 -0
- package/skills/building-cpp/project/vscode/02-settings.md +39 -0
- package/skills/building-cpp/references/build-script.md +14 -7
- package/skills/building-cpp/references/environment.md +8 -1
- package/skills/building-cpp/steps/01-build.md +8 -8
- package/skills/building-cpp/steps/02-tooling.md +17 -9
- package/skills/building-cpp/steps/03-check.md +16 -14
- package/skills/developing-cpp/SKILL.md +37 -25
- package/skills/developing-cpp/references/comments/comment-rules.md +181 -0
- package/skills/developing-cpp/references/comments/comment-style.md +80 -0
- package/skills/developing-cpp/references/comments/examples/declaration-class.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-constructor.md +22 -0
- package/skills/developing-cpp/references/comments/examples/declaration-enum.md +48 -0
- package/skills/developing-cpp/references/comments/examples/declaration-file.md +66 -0
- package/skills/developing-cpp/references/comments/examples/declaration-function.md +127 -0
- package/skills/developing-cpp/references/comments/examples/declaration-member.md +23 -0
- package/skills/developing-cpp/references/comments/examples/declaration-predicate.md +14 -0
- package/skills/developing-cpp/references/comments/examples/declaration-struct.md +18 -0
- package/skills/developing-cpp/references/comments/examples/implementation-body.md +64 -0
- package/skills/developing-cpp/references/comments/examples/implementation-todo.md +16 -0
- package/skills/developing-cpp/references/comments/glossary/comment-terms.md +93 -0
- package/skills/developing-cpp/references/design/examples/behavior-sentence.md +14 -0
- package/skills/developing-cpp/references/design/examples/coordinate-table.md +31 -0
- package/skills/developing-cpp/references/design/examples/header-place.md +26 -0
- package/skills/developing-cpp/references/design/examples/library-boundary.md +30 -0
- package/skills/developing-cpp/references/design/examples/library-split.md +7 -0
- package/skills/developing-cpp/references/design/examples/move-result.md +39 -0
- package/skills/developing-cpp/references/design/examples/preview-read.md +40 -0
- package/skills/developing-cpp/references/{project-design.md → design/project-design.md} +57 -5
- package/skills/developing-cpp/references/{change-model.md → editing/01-edit-operations.md} +5 -3
- package/skills/developing-cpp/references/{priority.md → editing/02-rule-priority.md} +2 -2
- package/skills/developing-cpp/references/exceptions/examples/throw-boundary.md +28 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-build-mode.md +38 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-catch.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-destructor.md +21 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-format.md +13 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-message.md +16 -0
- package/skills/developing-cpp/references/exceptions/examples/throw-when.md +27 -0
- package/skills/developing-cpp/references/{exceptions.md → exceptions/throw-rules.md} +78 -23
- package/skills/developing-cpp/references/{glossary.md → language/glossary/language-terms.md} +2 -2
- package/skills/developing-cpp/references/language/language-basics/01-objects.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/03-deduction.md +12 -0
- package/skills/developing-cpp/references/language/language-basics/04-const.md +25 -0
- package/skills/developing-cpp/references/language/language-basics/05-expressions.md +23 -0
- package/skills/developing-cpp/references/language/language-basics/06-functions.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/07-classes.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/08-raii.md +15 -0
- package/skills/developing-cpp/references/language/language-basics/09-templates.md +26 -0
- package/skills/developing-cpp/references/language/language-basics/10-checklist.md +10 -0
- package/skills/developing-cpp/references/language/performance/01-measure.md +22 -0
- package/skills/developing-cpp/references/language/performance/02-complexity.md +13 -0
- package/skills/developing-cpp/references/language/performance/03-memory.md +14 -0
- package/skills/developing-cpp/references/language/performance/04-undefined-behavior.md +28 -0
- package/skills/developing-cpp/references/language/performance/05-pitfalls.md +17 -0
- package/skills/developing-cpp/references/language/performance/06-false-optimizations.md +13 -0
- package/skills/developing-cpp/references/language/performance/07-checklist.md +8 -0
- package/skills/developing-cpp/references/language/standard-library/01-containers.md +29 -0
- package/skills/developing-cpp/references/language/standard-library/02-access.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/03-iteration.md +18 -0
- package/skills/developing-cpp/references/language/standard-library/04-algorithms.md +26 -0
- package/skills/developing-cpp/references/language/standard-library/05-string.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/06-format.md +106 -0
- package/skills/developing-cpp/references/language/standard-library/07-time.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/08-random.md +13 -0
- package/skills/developing-cpp/references/language/standard-library/09-files.md +14 -0
- package/skills/developing-cpp/references/language/standard-library/10-checklist.md +9 -0
- package/skills/developing-cpp/references/language/value-types/01-width.md +17 -0
- package/skills/developing-cpp/references/language/value-types/02-signed.md +26 -0
- package/skills/developing-cpp/references/language/value-types/03-integer-roles.md +38 -0
- package/skills/developing-cpp/references/language/value-types/04-enum.md +24 -0
- package/skills/developing-cpp/references/language/value-types/05-strong-type.md +17 -0
- package/skills/developing-cpp/references/language/value-types/06-const-method.md +22 -0
- package/skills/developing-cpp/references/language/value-types/07-nodiscard.md +15 -0
- package/skills/developing-cpp/references/language/value-types/08-comparison.md +10 -0
- package/skills/developing-cpp/references/language/value-types/09-subscript.md +15 -0
- package/skills/developing-cpp/references/language/value-types/10-float-bool.md +13 -0
- package/skills/developing-cpp/references/language/value-types/11-checklist.md +8 -0
- package/skills/developing-cpp/references/{layout.md → layout/01-source-layout.md} +10 -4
- package/skills/developing-cpp/references/{file-template.md → layout/02-file-template.md} +10 -4
- package/skills/developing-cpp/references/layout/03-declaration-order.md +138 -0
- package/skills/developing-cpp/references/naming/examples/naming-accessor.md +17 -0
- package/skills/developing-cpp/references/naming/examples/naming-basic.md +3 -0
- package/skills/developing-cpp/references/naming/examples/naming-current-prefix.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-lookup.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-lower-layer.md +39 -0
- package/skills/developing-cpp/references/naming/examples/naming-multiple-return.md +20 -0
- package/skills/developing-cpp/references/naming/examples/naming-player-value.md +16 -0
- package/skills/developing-cpp/references/naming/examples/naming-predicate.md +18 -0
- package/skills/developing-cpp/references/naming/examples/naming-rename.md +5 -0
- package/skills/developing-cpp/references/naming/examples/naming-responsibility.md +19 -0
- package/skills/developing-cpp/references/naming/examples/naming-role.md +21 -0
- package/skills/developing-cpp/references/naming/examples/naming-state-change.md +57 -0
- package/skills/developing-cpp/references/{naming.md → naming/identifier-names.md} +37 -9
- package/skills/developing-cpp/references/{google-cpp-style.md → style/google-cpp-style.md} +11 -10
- package/skills/developing-cpp/scripts/checking-comments.mjs +71 -14
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +357 -0
- package/skills/developing-cpp/steps/01-behavior.md +5 -5
- package/skills/developing-cpp/steps/02-boundary.md +10 -10
- package/skills/developing-cpp/steps/03-declaration.md +16 -14
- package/skills/developing-cpp/steps/04-implementation.md +21 -17
- package/skills/engineering/SKILL.md +87 -0
- package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/CompileSettings.cmake +2 -0
- package/skills/{building-cpp/references/linking.md → engineering/references/cmake/linking/01-libraries.md} +7 -48
- package/skills/engineering/references/cmake/linking/02-rpath.md +25 -0
- package/skills/engineering/references/cmake/linking/03-undefined-reference.md +20 -0
- package/skills/engineering/references/cmake/linking/04-inspect.md +21 -0
- package/skills/{configuring-cmake/references/modules.md → engineering/references/cmake/modules/functions.md} +8 -5
- package/skills/{configuring-cmake/references → engineering/references/cmake/standard}/cxx-standard.md +5 -4
- package/skills/engineering/references/cmake/standard/examples/standard.md +19 -0
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md +9 -9
- package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/README.md +1 -1
- package/skills/engineering/references/cmake/targets/examples/glob.md +22 -0
- package/skills/engineering/references/cmake/targets/examples/rpath.md +24 -0
- package/skills/{configuring-cmake/examples/cmake.md → engineering/references/cmake/targets/examples/target.md} +2 -70
- package/skills/{configuring-cmake/references/cmake.md → engineering/references/cmake/targets/rules.md} +18 -11
- package/skills/{building-cpp/references/testing.md → engineering/references/cmake/testing/ctest.md} +11 -4
- package/skills/{building-cpp/references/encoding.md → engineering/references/formatting/01-encoding.md} +1 -1
- package/skills/engineering/references/formatting/02-clang-format.md +20 -0
- package/skills/engineering/references/formatting/03-clang-tidy.md +58 -0
- package/skills/engineering/references/formatting/04-clangd.md +90 -0
- package/skills/engineering/references/formatting/examples/clang-format.md +38 -0
- package/skills/engineering/references/formatting/examples/clang-tidy.md +43 -0
- package/skills/{building-cpp/examples/tooling.md → engineering/references/formatting/examples/clangd.md} +1 -80
- package/skills/{configuring-cmake → engineering}/steps/01-targets.md +6 -6
- package/skills/{configuring-cmake → engineering}/steps/02-modules.md +4 -4
- package/skills/{configuring-cmake → engineering}/steps/03-check.md +3 -2
- package/templates/cpp-workflow-project/SKILL.md +1 -1
- package/skills/building-cpp/references/static-analysis.md +0 -148
- package/skills/configuring-cmake/SKILL.md +0 -68
- package/skills/developing-cpp/examples/comments-declaration.md +0 -313
- package/skills/developing-cpp/examples/comments-implementation.md +0 -66
- package/skills/developing-cpp/examples/exceptions.md +0 -173
- package/skills/developing-cpp/examples/naming.md +0 -168
- package/skills/developing-cpp/examples/structure.md +0 -194
- package/skills/developing-cpp/references/comment-terms.md +0 -75
- package/skills/developing-cpp/references/comments.md +0 -223
- package/skills/developing-cpp/references/declaration-order.md +0 -118
- package/skills/developing-cpp/references/language.md +0 -123
- package/skills/developing-cpp/references/performance.md +0 -83
- package/skills/developing-cpp/references/standard-library.md +0 -138
- package/skills/developing-cpp/references/types.md +0 -154
- /package/skills/{configuring-cmake/assets/cmake → engineering/references/cmake/assets}/LinkSettings.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++11/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++14/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++17/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++20/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/README.md +0 -0
- /package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/c++23/cmake-snippet.cmake +0 -0
- /package/skills/{building-cpp → engineering/references/formatting}/scripts/checking-encoding.mjs +0 -0
|
@@ -7,19 +7,22 @@
|
|
|
7
7
|
## 通用情况
|
|
8
8
|
|
|
9
9
|
- 函数保持短小,一个函数只做一件事。超过约 40 行时先找可以命名的中间步骤,不靠注释把长函数切成几段。
|
|
10
|
-
- 按「准备 → 操作 →
|
|
11
|
-
- `.cpp`
|
|
12
|
-
-
|
|
10
|
+
- 按「准备 → 操作 → 收尾」排列语句,用空行分组;组与组之间的空行表达阶段,同一组内的守卫与结果处理不插空行。
|
|
11
|
+
- `.cpp` 里的定义顺序跟配对头文件:先类型与常量,再自由函数,再成员函数;成员函数按类内声明顺序。头文件把构造函数、析构函数、拷贝/移动声明在成员组最前时,定义也放最前;如果头文件把它们声明在别的成员之后,仍按头文件顺序,不硬提。不为先编译重排,也不按调用频率重排。`.cpp` 里不需要外部引用的 `constexpr` 放具名命名空间,仅一个函数使用时才用函数内 `static constexpr`;函数与类型放唯一的匿名命名空间,公开函数定义放在其后。见 `references/layout/03-declaration-order.md`「`.cpp` 的定义顺序」与 `references/style/google-cpp-style.md`。
|
|
12
|
+
- 同一文件里的函数按同一套分段约定:需要分阶段的非单表达式函数按空行分段、每段上方一句;读入、单值转换、一行输出这类一条直路的短函数不加分段注释;不能结构相近的函数一半分段、一半完全不留注释。阶段怎么分见 `references/layout/03-declaration-order.md`「函数体内的空行」。
|
|
13
|
+
- `.cpp` 的第一个 include 是自己的配对头,然后直接包含本文件用到的标准库头,不依赖公共头转发声明。包含顺序见 `references/style/google-cpp-style.md`,文件槽位见 `references/layout/02-file-template.md`。
|
|
14
|
+
- 函数体注释用 `//`,不用 `///`:体内 `///` 会接到该函数的 Doxygen 详细说明后面。重要函数按空行分阶段,每个阶段上方写一句 `//`,写这一支的后果,可以点明处理了哪些枚举值,不逐词翻译下一行的条件;`//` 里的枚举值不加反引号。单表达式函数(如 `IsFull`)不配注释;关键局部表(查表、分派表)上方写一句,说明表的角色与分组。只写删掉会让下一次修改得到错误结果、而代码本身看不出的内容;变更史留给提交说明。`.cpp` 的自由函数在定义上一行用 `///` 写一句职责,与配对头文件的声明注释一致;类方法定义不重复声明处已有的事实。
|
|
13
15
|
- 什么时候在 `if` 上方加一句:多条独立拒绝(如 `CheckPlaceable`)每个 `if` 一句,不同条件的 `if` 不合并成一句;成功写入前一句;只有次序依赖的一对 `if` 在第一个上方说明;循环上方已有阶段说明时,循环里的 `if` 不再加;单表达式谓词不加。
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
+
- `switch` 覆盖枚举全部取值时不写 `default`,新增取值交给 `-Wswitch` 报错;编译器需要出口时在 `switch` 后写 `std::unreachable()`,不写 `throw` 兜底。见 `references/language/language-basics/05-expressions.md`。
|
|
17
|
+
- 失败是调用方正常结果时返回结果类型、`bool` 或 `std::optional`;函数无法完成自己的任务时抛异常,不拿异常做正常分支。Debug 与 Release 用同一套返回值与抛出条件,只有诊断详略不同。两种模式都要守的不变量用 `Check`(项目保留时),只在开发期做的自检用 `DebugCheck`;项目没有这两个宏时分别用 `assert` 与 `std::terminate`。`Check` 不是 `[[noreturn]]`,条件成立时正常返回;`DebugCheck` 的参数在 Release 不求值,不能放入有副作用的表达式。捕获时按 `const` 引用,只处理能处理的类型,其余继续抛。异常类型、消息格式与「不抛的边界」见 `references/exceptions/throw-rules.md`。
|
|
18
|
+
- 参数校验、调用方状态检查与正常结果返回的次序见 `references/exceptions/throw-rules.md` 的「检查次序」;本层的抛出检查先于 `kDenied` 之类的正常失败,下层检查不重复。改完 `throw` 再走同文件的「检查清单」:异常类型、消息标签、`std::format` 直接进 `throw`、不吞异常。
|
|
16
19
|
- 声明 `noexcept` 前核对函数体:出现 `at()` 或其他可能抛出的调用就去掉。越界用 `at()` 抛 `std::out_of_range`,这个函数就不能标 `noexcept`。普通查询和返回结果枚举的函数,不因为当前函数体不抛就补 `noexcept`;它留给移动、交换、析构,以及已经标上的纯谓词。
|
|
17
20
|
- 组合输出用 `std::format`,再写入 `std::cout` / `std::cerr`;库实现里不直接打印。C++23 且库支持时程序边界可用 `std::print`,C++20 及更早没有。
|
|
18
21
|
- 转换用具名转换 `static_cast` / `std::bit_cast`,不用 C 风格强制转换。
|
|
19
|
-
- 所有权用 `std::unique_ptr
|
|
22
|
+
- 所有权用 `std::unique_ptr`;禁止裸 `new` / `delete`,没有共享所有权的理由时不用 `std::shared_ptr`。
|
|
20
23
|
- 只改注释时,不改可观察行为;只改实现时,不改公共声明。两者同时需要时,先改声明,再改实现。
|
|
21
|
-
- 清空成员数组:元素类型提供收回空取值的函数时(如 `
|
|
22
|
-
- 默认构造只做「建立初值」时用成员初值(如 `
|
|
24
|
+
- 清空成员数组:元素类型提供收回空取值的函数时(如 `Square::Clear`),容器的 `Clear` 逐个调用它;没有这种函数时才用 `std::ranges::fill(squares_ | std::views::join, SquareValue::kEmpty)` 或 `for` 循环逐格赋空值。两种写法都不直接写 `squares_ = {}`,也不从外部把枚举赋成空值。撤销只回退受影响的那一处,不清空整个容器再重放全部历史,见 `references/design/project-design.md` 的「撤销与回退」。
|
|
25
|
+
- 默认构造只做「建立初值」时用成员初值(如 `squares_{}`),不在构造函数里再调一遍 `Clear()`;`Clear()` 只负责把已经写入的值清空为 `kEmpty`。
|
|
23
26
|
|
|
24
27
|
## 情况
|
|
25
28
|
|
|
@@ -29,18 +32,19 @@
|
|
|
29
32
|
|
|
30
33
|
**重构**:一次只动一种东西(改名、抽函数、换容器),每步都让测试保持绿色,行为句不变。
|
|
31
34
|
|
|
32
|
-
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format("错误: {}", error.what())`
|
|
35
|
+
**异常边界**:库内部让异常向上传播;程序入口捕获 `std::exception`(或项目自己的异常基类),用 `std::format("错误: {}", error.what())` 打印,返回项目约定的非零退出码。`catch` 子句按派生到基类、具体到通用排序,`catch (...)` 只能放最后,且不要静默返回。不吞掉异常后继续使用无效状态。
|
|
33
36
|
|
|
34
37
|
## 交回构建
|
|
35
38
|
|
|
36
|
-
|
|
39
|
+
实现改完,先对照配对头文件自检:每个声明都有定义,定义顺序与声明顺序一致;新增或改动的函数按同一套分段约定,需要分阶段的都有阶段注释与空行;只改过语言细节时再过对应目录的完成清单(`references/language/value-types/11-checklist.md`、`language-basics/10-checklist.md`、`standard-library/10-checklist.md`、`performance/07-checklist.md`);行宽与缩进以工程根 `.clang-format` 为准,注释版式见 `references/comments/comment-style.md`。再跑 `node <skill-dir>/scripts/checking-declaration-order.mjs --root .`(`<skill-dir>` 换成插件 `developing-cpp` 的实际目录),有乱序先修再交回下游:需要新增或移动源文件时,先由 `engineering` 的 `steps/01-targets.md` 把文件显式写进 target;target 齐了再由 `building-cpp` 的 `steps/01-build.md` configure、构建,最后走 `steps/03-check.md`。不要只改完函数体就宣称完成。
|
|
37
40
|
|
|
38
41
|
## 正反例指针
|
|
39
42
|
|
|
40
|
-
- 实现注释复述代码、分组混乱:`examples/
|
|
41
|
-
- 已经证明非负的坐标把下标收成 `std::size_t` 再 `[]`,不转成 `int`:`references/types.md
|
|
42
|
-
- 越界消息的标签取自调用点:`references/exceptions.md`「消息」的 `std::source_location` 用法。
|
|
43
|
-
-
|
|
44
|
-
-
|
|
45
|
-
-
|
|
46
|
-
-
|
|
43
|
+
- 实现注释复述代码、分组混乱:`references/comments/examples/implementation-body.md`。
|
|
44
|
+
- 已经证明非负的坐标把下标收成 `std::size_t` 再 `[]`,不转成 `int`:`references/language/value-types/09-subscript.md`。
|
|
45
|
+
- 越界消息的标签取自调用点:`references/exceptions/throw-rules.md`「消息」的 `std::source_location` 用法。
|
|
46
|
+
- 多个入口重复同一范围检查:抽私有函数,函数名显式传入、`source_location` 取调用点,见 `references/exceptions/throw-rules.md`「重复的入口检查」。
|
|
47
|
+
- 为了保住 `noexcept` 再包一层只转发下标的私有函数:`references/exceptions/throw-rules.md`「不抛的边界」。
|
|
48
|
+
- 用 `static_cast<void>` 丢弃 `[[nodiscard]]` 的落子结果,或丢掉调用方给的一手、在 `Game` 内重新拼装:`references/design/examples/move-result.md`。
|
|
49
|
+
- 异常类型与结果类型混用:`references/exceptions/examples/throw-when.md`。
|
|
50
|
+
- `std::format` 与流输出混用得当:`references/exceptions/examples/throw-message.md`。
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: engineering
|
|
3
|
+
description: 编写或重构 C++11–C++23 工程的 CMake 与排版工具配置时使用:CMakeLists.txt、cmake 模块、target 属性、依赖与输出目录、语言标准,以及 clang-format、clang-tidy、clangd;新工程默认 C++23。不用于 Quarto、Python 或非 C++ 文档。
|
|
4
|
+
metadata:
|
|
5
|
+
short-description: CMake 与排版工具
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# engineering
|
|
9
|
+
|
|
10
|
+
本技能管两件事:CMake 文件与 target 契约,以及编码与排版工具配置。函数体、类型、命名、异常与源码注释只开 `developing-cpp`;构建目录、编译器、入口脚本与运行检查开 `building-cpp`。
|
|
11
|
+
|
|
12
|
+
## 适用场景
|
|
13
|
+
|
|
14
|
+
- **负责**:`CMakeLists.txt`、`references/cmake/` 模块、`target_*` 命令、目标属性、include 与链接边界、输出目录、共享库运行时路径、`FetchContent`、preset 与安装导出;UTF-8/LF、clang-format、clang-tidy、clangd 的配置条文。
|
|
15
|
+
- **不负责**:源码语义与注释开 `developing-cpp`;`environment.json`、`running-build.sh`、构建目录、编译器与 CTest 运行开 `building-cpp`。
|
|
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 标准的唯一来源;缺失、非法或与现有 target 不一致时停在定 target,不猜标准。
|
|
18
|
+
- **边界**:target 图与 CMake 写法以本技能为权威,`building-cpp` 只读不修改;构建目录、编译器和工具版本由 `building-cpp` 的定构建负责,本技能只写 target 契约与工具配置。
|
|
19
|
+
|
|
20
|
+
## 三种操作
|
|
21
|
+
|
|
22
|
+
一次只选一种,范围(单点、整段)是任务属性;判据、边界与交接见 `../developing-cpp/references/editing/01-edit-operations.md`。四类改动是域,不是操作。
|
|
23
|
+
|
|
24
|
+
| 操作 | 什么时候用 | 从哪步进 |
|
|
25
|
+
| --- | --- | --- |
|
|
26
|
+
| 写作(起稿) | 新增 target、改公共 include 或链接边界、换语言标准 | 第 1 步;新工程走到第 3 步 |
|
|
27
|
+
| 改写 | target 图与标准不变,模块、函数或目录结构不对 | 从第 2 步重走,可到第 3 步 |
|
|
28
|
+
| 润色 | 只改函数名、注释措辞或错误消息 | 第 2 步 |
|
|
29
|
+
|
|
30
|
+
- 不发明项目没有声明的 target、标准、编译器或目录;不覆盖已有 `CMakeLists.txt`。
|
|
31
|
+
- 不用全局 `include_directories()`、`add_definitions()`、`CMAKE_CXX_FLAGS` 给所有 target 加料;不写 `file(GLOB ...)`;不把 `main()` 编进库。
|
|
32
|
+
- 库与程序不下载依赖;只有测试需要时调用 `LinkSettings_FetchGoogleTest`。排版工具配置只改本技能点名的三份文件,不顺手改项目其他文件。
|
|
33
|
+
- 后一步发现前一步没做完,退回那一步;拿不准先单点修正,target 图或标准要改则整段退回。
|
|
34
|
+
|
|
35
|
+
## 步骤
|
|
36
|
+
|
|
37
|
+
| 步 | 名称 | 文件 | 一句话 |
|
|
38
|
+
| --- | --- | --- | --- |
|
|
39
|
+
| 1 | 定 target 与标准 | `steps/01-targets.md` | target 图、PUBLIC/PRIVATE、标准、输出目录与编译数据库 |
|
|
40
|
+
| 2 | 定模块与函数 | `steps/02-modules.md` | 何时抽函数、文件与命名、导入、注释与 2 空格排版 |
|
|
41
|
+
| 3 | 通过检查 | `steps/03-check.md` | configure、构建、编译数据库与实际 `-std` 核对 |
|
|
42
|
+
|
|
43
|
+
每步只打开同号的一个步骤文件。步骤只覆盖 CMake;编码与 clang 工具没有步骤,按入口表直接打开那一份文件。
|
|
44
|
+
|
|
45
|
+
## 入口表
|
|
46
|
+
|
|
47
|
+
| 要做的事 | 打开 |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| 定 target、include、链接、输出目录与警告 | `references/cmake/targets/rules.md`;案例在 `references/cmake/targets/examples/` |
|
|
50
|
+
| 定语言标准与档位 | `references/cmake/standard/cxx-standard.md`;五档细节在 `references/cmake/standard/standards/` |
|
|
51
|
+
| 抽 CMake 函数与模块 | `references/cmake/modules/functions.md` |
|
|
52
|
+
| 链接方式、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
|
+
| 编码、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` |
|
|
58
|
+
| 可复制的 CMake 函数 | `references/cmake/assets/CompileSettings.cmake`、`references/cmake/assets/LinkSettings.cmake` |
|
|
59
|
+
|
|
60
|
+
## 加载预算
|
|
61
|
+
|
|
62
|
+
- 只开当前步对应的一个步骤文件;同一改动域内不同时展开两个技能的步骤。
|
|
63
|
+
- 条文只开该步点名的一份;同号案例留到下一次激活;排版工具一次只开入口表里的那一份。
|
|
64
|
+
- 标准档只打开项目技能声明的那一档,没有声明时用本技能已有的新工程默认,不把其余标准档读进来。
|
|
65
|
+
|
|
66
|
+
项目技能列出了检查命令时只跑那一条,没有时按 `steps/03-check.md` 的 configure 与构建命令跑;退出码为 0 后停在这一步,不再继续走没有失败的步骤。五档标准的语言设施、工具映射与降档由 `references/cmake/standard/standards/` 维护;本技能只负责这些标准在 CMake 里怎么写,以及三份工具配置各自的最低版本。
|
|
67
|
+
|
|
68
|
+
## 四类改动
|
|
69
|
+
|
|
70
|
+
四类改动是操作内部的域:操作定入口,域定层次。四域的定义、边界与次序见 `../developing-cpp/references/editing/01-edit-operations.md`;一次改动混了两类时,按「内容 → 结构 → 版式 → 措辞」拆开做,前一类落定并经检查再进下一类。
|
|
71
|
+
|
|
72
|
+
| 类型 | 典型请求 | 入口步骤 |
|
|
73
|
+
| --- | --- | --- |
|
|
74
|
+
| 内容 | 新增 target、改公共 include 或链接边界、换语言标准 | `steps/01-targets.md` |
|
|
75
|
+
| 结构 | 抽函数、拆模块、调导入位置 | `steps/02-modules.md` |
|
|
76
|
+
| 版式 | 只改 2 空格、行宽、注释形态或工具配置 | `steps/02-modules.md`;工具配置见 `references/formatting/02-clang-format.md` |
|
|
77
|
+
| 措辞 | 只改函数名、注释措辞或错误消息 | `steps/02-modules.md` |
|
|
78
|
+
|
|
79
|
+
## 完成判据
|
|
80
|
+
|
|
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` 最后一条 `-std=` 与声明一致,不是 `gnu++NN`。
|
|
83
|
+
- [ ] 复用的 CMake 函数放在工程根的 `cmake/` 模块里(模板源在 `references/cmake/assets/`),文件名用 UpperCamelCase,函数名用 `<ModuleName>_` 加 PascalCase,有目标与参数校验,根 `CMakeLists.txt` 通过 `CMAKE_MODULE_PATH` 导入;没有重复片段和未使用的旧模块。
|
|
84
|
+
- [ ] 公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` / `:keyword` 标参数;不写 `#[[.rst:` 与 `.. command::`,完整写法见 `references/cmake/targets/rules.md`;函数组之间用 `#---`,不画 `#===...===#` 方框。
|
|
85
|
+
- [ ] `CMakeLists.txt` 与 `*.cmake` 用 UTF-8 无 BOM、LF、2 空格缩进、不用 tab;行宽按团队约定,默认 80 列。
|
|
86
|
+
- [ ] 编码与排版工具各管一段:`.clang-format` 管空白与包含顺序,`.clang-tidy` 管检查分级,`.clangd` 管编辑器补全;三份都不把标准写死成与 `environment.json` 冲突的值,也不把 clang-format 塞进构建脚本。
|
|
87
|
+
- [ ] configure 与构建通过,构建产物不落在源码目录;没有改源码语义,也没有覆盖已有工程文件。
|
|
@@ -11,6 +11,8 @@ include_guard(GLOBAL)
|
|
|
11
11
|
#
|
|
12
12
|
# :param target: 已经存在的 CMake 目标。
|
|
13
13
|
# :param visibility: 取 PUBLIC、PRIVATE 或 INTERFACE。
|
|
14
|
+
# INTERFACE 只传播 compile feature(最低标准),不能锁死 CXX_STANDARD
|
|
15
|
+
# 与 CXX_EXTENSIONS;纯头文件 target 的消费者仍需自己调用一次。
|
|
14
16
|
# :param standard: 取 c++11、c++14、c++17、c++20 或 c++23。
|
|
15
17
|
function(CompileSettings_SetCxxStandard target visibility standard)
|
|
16
18
|
if(NOT TARGET "${target}")
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
链接错误和编译错误是两类问题。先判断失败发生在哪个阶段,再改对应的文件。
|
|
1
|
+
# 链接与库边界
|
|
4
2
|
|
|
5
3
|
## 四个阶段
|
|
6
4
|
|
|
@@ -25,19 +23,6 @@
|
|
|
25
23
|
- 只用到声明时不要包含完整定义;前置声明可以减少编译依赖,但不能替代需要完整类型的场景(继承、成员对象、按值返回)。
|
|
26
24
|
- 每个头文件自包含:单独编译一个只包含该头文件的 `.cpp` 应通过。用路径式 `#define` 守卫防止重复包含,不用 `#pragma once`。
|
|
27
25
|
|
|
28
|
-
## `undefined reference` 分诊
|
|
29
|
-
|
|
30
|
-
| 现象 | 常见原因 | 修法 |
|
|
31
|
-
| --- | --- | --- |
|
|
32
|
-
| 链接器说某个成员函数未定义 | 声明了但没写定义,或 `.cpp` 没加入 target | 补定义,或把源文件加入 `target_sources` |
|
|
33
|
-
| 只有某个平台失败 | 条件编译把定义排除了 | 检查宏和平台分支,保证每个平台都有定义 |
|
|
34
|
-
| 第三方库符号缺失 | 没链接库,或库顺序不对 | `target_link_libraries` 加库;CMake 会处理顺序 |
|
|
35
|
-
| C++ 符号被当成 C 符号 | 头文件缺少 `extern \"C\"` | C 接口头加 `extern \"C\"`,C++ 调用方也可见 |
|
|
36
|
-
| 模板成员未定义 | 模板定义在 `.cpp`,实例化点看不到 | 模板定义移到头文件,或显式实例化 |
|
|
37
|
-
| 静态库中的目标文件没被拉入 | 链接器只取被引用符号所在的目标文件 | 确认符号确实被引用,或改用对象库/共享库 |
|
|
38
|
-
|
|
39
|
-
先看链接器报的完整符号名和来源文件,不要只看第一行。
|
|
40
|
-
|
|
41
26
|
## 静态库与共享库
|
|
42
27
|
|
|
43
28
|
| 形式 | 产物 | 链接时机 | 运行期依赖 | 适用 |
|
|
@@ -50,38 +35,6 @@
|
|
|
50
35
|
- 共享库的 ABI 包括类布局、虚表、标准库类型和编译器选项;不同编译器或 `-stdlib` 混用可能链接成功但运行崩溃。
|
|
51
36
|
- 一个项目里不要同时链接两套标准库(libstdc++ 与 libc++);编译器、clangd、库和可执行文件必须一致。
|
|
52
37
|
|
|
53
|
-
## 输出目录与 RPATH
|
|
54
|
-
|
|
55
|
-
推荐布局:
|
|
56
|
-
|
|
57
|
-
```text
|
|
58
|
-
build/<compiler>/
|
|
59
|
-
bin/app
|
|
60
|
-
lib/libgame.so
|
|
61
|
-
compile_commands.json
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
- 可执行文件在 `bin/`,共享库在 `lib/`;不要把库和可执行文件混在同一层。
|
|
65
|
-
- 运行时动态加载器默认不搜索可执行文件旁边的 `lib/`;需要 RPATH:
|
|
66
|
-
- Linux:`-Wl,-rpath,'$ORIGIN/../lib'`
|
|
67
|
-
- macOS:`-Wl,-rpath,@loader_path/../lib`
|
|
68
|
-
- CMake 用 `BUILD_RPATH` / `INSTALL_RPATH` 设置,不要手工拼链接命令;见 [`LinkSettings.cmake`](../../configuring-cmake/assets/cmake/LinkSettings.cmake)。
|
|
69
|
-
- RPATH 里的 `$ORIGIN` 要防止被 shell 展开:CMake 字符串里写 `\$ORIGIN` 或使用 `BUILD_RPATH_USE_ORIGIN`。
|
|
70
|
-
|
|
71
|
-
## 验收:`file` 与 `ldd`
|
|
72
|
-
|
|
73
|
-
链接成功不等于能运行。构建后检查产物:
|
|
74
|
-
|
|
75
|
-
```bash
|
|
76
|
-
file build/clang/bin/app
|
|
77
|
-
ldd build/clang/bin/app
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
- `file` 确认架构、位数、是否动态链接。
|
|
81
|
-
- `ldd` 确认每个共享库都能解析到实际路径;出现 `not found` 就是加载期会失败。
|
|
82
|
-
- 运行 `app` 做一次最小启动;动态库缺失会在启动时暴露,而不是链接时。
|
|
83
|
-
- macOS 用 `otool -L` 代替 `ldd`;Windows 用 `dumpbin /dependents`。
|
|
84
|
-
|
|
85
38
|
## 构建脚本里的顺序
|
|
86
39
|
|
|
87
40
|
1. 配置:CMake 生成构建系统,导出编译数据库。
|
|
@@ -99,3 +52,9 @@ ldd build/clang/bin/app
|
|
|
99
52
|
- [ ] 静态库/共享库选择有理由,没有混用两套标准库。
|
|
100
53
|
- [ ] 产物在 `bin/` 与 `lib/`,RPATH 指向相对位置。
|
|
101
54
|
- [ ] `file` 与 `ldd` 检查过,程序能最小启动。
|
|
55
|
+
|
|
56
|
+
## 依据
|
|
57
|
+
|
|
58
|
+
- [CMake: `target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html):链接依赖的传递与顺序。
|
|
59
|
+
- [CMake: `BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html):`add_library` 未写类型时的默认库类型。
|
|
60
|
+
- [CMake: `POSITION_INDEPENDENT_CODE`](https://cmake.org/cmake/help/latest/prop_tgt/POSITION_INDEPENDENT_CODE.html):共享库与 PIE 的编译要求。
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# 输出目录与 RPATH
|
|
2
|
+
|
|
3
|
+
推荐布局:
|
|
4
|
+
|
|
5
|
+
```text
|
|
6
|
+
build/<compiler>/
|
|
7
|
+
bin/app
|
|
8
|
+
lib/libgame.so
|
|
9
|
+
compile_commands.json
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- 可执行文件在 `bin/`,共享库在 `lib/`;不要把库和可执行文件混在同一层。
|
|
13
|
+
- 运行时动态加载器默认不搜索可执行文件旁边的 `lib/`;需要 RPATH:
|
|
14
|
+
- Linux:`-Wl,-rpath,'$ORIGIN/../lib'`
|
|
15
|
+
- macOS:`-Wl,-rpath,@loader_path/../lib`
|
|
16
|
+
- CMake 用 `BUILD_RPATH` / `INSTALL_RPATH` 设置,不要手工拼链接命令;见 [`LinkSettings.cmake`](../assets/LinkSettings.cmake)。
|
|
17
|
+
- CMake 字符串里直接写 `"$ORIGIN/../lib"`:`$ORIGIN` 不是 CMake 变量(CMake 只展开 `${...}`),CMake 也不经过 shell。实测 `"$ORIGIN/../lib"` 与 `"\$ORIGIN/../lib"` 生成的 RUNPATH 都是 `$ORIGIN/../lib`;只有把路径交给 shell 命令时才需要 shell 引号。
|
|
18
|
+
- `BUILD_RPATH_USE_ORIGIN` 让 CMake 为构建树里的绝对路径自动生成 `$ORIGIN` 相对形式,不是转义开关。
|
|
19
|
+
|
|
20
|
+
## 依据
|
|
21
|
+
|
|
22
|
+
- [CMake: `BUILD_RPATH`](https://cmake.org/cmake/help/latest/prop_tgt/BUILD_RPATH.html):构建树中的运行时搜索路径。
|
|
23
|
+
- [CMake: `INSTALL_RPATH`](https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html):安装后的运行时搜索路径。
|
|
24
|
+
- [CMake: `BUILD_RPATH_USE_ORIGIN`](https://cmake.org/cmake/help/latest/prop_tgt/BUILD_RPATH_USE_ORIGIN.html):用 `$ORIGIN` 生成相对 RPATH。
|
|
25
|
+
- [Linux `ld.so(8)`](https://man7.org/linux/man-pages/man8/ld.so.8.html):`$ORIGIN` 与 RPATH 的展开规则。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# undefined reference 分诊
|
|
2
|
+
|
|
3
|
+
## `undefined reference` 分诊
|
|
4
|
+
|
|
5
|
+
| 现象 | 常见原因 | 修法 |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| 链接器说某个成员函数未定义 | 声明了但没写定义,或 `.cpp` 没加入 target | 补定义,或把源文件加入 `target_sources` |
|
|
8
|
+
| 只有某个平台失败 | 条件编译把定义排除了 | 检查宏和平台分支,保证每个平台都有定义 |
|
|
9
|
+
| 第三方库符号缺失 | 没链接库,或库顺序不对 | `target_link_libraries` 加库;CMake 会处理顺序 |
|
|
10
|
+
| C++ 符号被当成 C 符号 | 头文件缺少 `extern \"C\"` | C 接口头加 `extern \"C\"`,C++ 调用方也可见 |
|
|
11
|
+
| 模板成员未定义 | 模板定义在 `.cpp`,实例化点看不到 | 模板定义移到头文件,或显式实例化 |
|
|
12
|
+
| 静态库中的目标文件没被拉入 | 链接器只取被引用符号所在的目标文件 | 确认符号确实被引用,或改用对象库/共享库 |
|
|
13
|
+
|
|
14
|
+
先看链接器报的完整符号名和来源文件,不要只看第一行。
|
|
15
|
+
|
|
16
|
+
## 依据
|
|
17
|
+
|
|
18
|
+
- [CMake: `target_link_libraries`](https://cmake.org/cmake/help/latest/command/target_link_libraries.html):链接依赖与顺序。
|
|
19
|
+
- [CMake: `target_sources`](https://cmake.org/cmake/help/latest/command/target_sources.html):把实现文件加入 target。
|
|
20
|
+
- [GCC: Link Options](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html):链接器参数与库顺序。
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# 验收:file 与 ldd
|
|
2
|
+
|
|
3
|
+
## 验收:`file` 与 `ldd`
|
|
4
|
+
|
|
5
|
+
链接成功不等于能运行。构建后检查产物:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
file build/clang/bin/app
|
|
9
|
+
ldd build/clang/bin/app
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
- `file` 确认架构、位数、是否动态链接。
|
|
13
|
+
- `ldd` 确认每个共享库都能解析到实际路径;出现 `not found` 就是加载期会失败。
|
|
14
|
+
- 运行 `app` 做一次最小启动;动态库缺失会在启动时暴露,而不是链接时。
|
|
15
|
+
- macOS 用 `otool -L` 代替 `ldd`;Windows 用 `dumpbin /dependents`。
|
|
16
|
+
|
|
17
|
+
## 依据
|
|
18
|
+
|
|
19
|
+
- [`file(1)`](https://man7.org/linux/man-pages/man1/file.1.html):识别文件格式、架构与位数。
|
|
20
|
+
- [`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):把产物检查接进构建流程。
|
|
@@ -30,7 +30,7 @@ project/
|
|
|
30
30
|
- 文件名用 UpperCamelCase,与 CMake 官方 `Modules/` 一致;按职责命名,例如 `CompileSettings.cmake`、`LinkSettings.cmake`。不用 kebab-case,也不用现在分词。
|
|
31
31
|
- 函数名用 `<ModuleName>_` 加 PascalCase:`CompileSettings_SetCxxStandard`、`LinkSettings_FetchGoogleTest`。前缀取模块文件名的原样大小写,复制到工程后不改名。
|
|
32
32
|
- 工程自己新增、不准备回流的函数才用项目前缀加 snake_case,例如 `my_project_do_something`。
|
|
33
|
-
- 只有项目真的需要时才建 `cmake/`;一个函数都没有时不建空目录。
|
|
33
|
+
- 只有项目真的需要时才建 `references/cmake/`;一个函数都没有时不建空目录。
|
|
34
34
|
|
|
35
35
|
## 导入
|
|
36
36
|
|
|
@@ -59,8 +59,9 @@ function(CompileSettings_SetCxxStandard target visibility standard)
|
|
|
59
59
|
endfunction()
|
|
60
60
|
|
|
61
61
|
# 同一文件的第二组函数前再放一条 #--- 分隔线。
|
|
62
|
-
function(CompileSettings_SetWarnings target visibility
|
|
63
|
-
|
|
62
|
+
function(CompileSettings_SetWarnings target visibility)
|
|
63
|
+
cmake_parse_arguments(PARSE_ARGV 2 arg "" "LEVEL" "")
|
|
64
|
+
# 校验 LEVEL 与可见性,再调用 target_compile_options。
|
|
64
65
|
endfunction()
|
|
65
66
|
```
|
|
66
67
|
|
|
@@ -74,7 +75,7 @@ endfunction()
|
|
|
74
75
|
|
|
75
76
|
## 契约注释
|
|
76
77
|
|
|
77
|
-
公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` 标位置参数、`:keyword` 标关键字参数;不写 `#[[.rst:` 与 `.. command::`,那是进 CMake 手册的模块才用的标记。同一文件的函数组之间用一条 `#---` 分隔,行注释只写原因,不画 `#===...===#` 方框。完整写法见
|
|
78
|
+
公开函数上方写中文 `#` 契约:一句功能、空行、边界,再用 `:param` 标位置参数、`:keyword` 标关键字参数;不写 `#[[.rst:` 与 `.. command::`,那是进 CMake 手册的模块才用的标记。同一文件的函数组之间用一条 `#---` 分隔,行注释只写原因,不画 `#===...===#` 方框。完整写法见 `../targets/rules.md`。
|
|
78
79
|
|
|
79
80
|
## 测试
|
|
80
81
|
|
|
@@ -86,7 +87,7 @@ endfunction()
|
|
|
86
87
|
4. 重复 `include()` 不报函数重定义。
|
|
87
88
|
5. `compile_commands.json` 出现对应 `-std=`。
|
|
88
89
|
|
|
89
|
-
测试完删除临时工程;不把测试用 `CMakeLists.txt` 留在仓库。真实工程里的验证由
|
|
90
|
+
测试完删除临时工程;不把测试用 `CMakeLists.txt` 留在仓库。真实工程里的验证由 `../../../steps/03-check.md` 完成。
|
|
90
91
|
|
|
91
92
|
## 迁移旧模块
|
|
92
93
|
|
|
@@ -98,6 +99,8 @@ endfunction()
|
|
|
98
99
|
## 官方依据
|
|
99
100
|
|
|
100
101
|
- [`function()`](https://cmake.org/cmake/help/latest/command/function.html):函数在调用时执行,拥有自己的作用域。
|
|
102
|
+
- [`macro()`](https://cmake.org/cmake/help/latest/command/macro.html):宏是文本替换,没有函数作用域。
|
|
103
|
+
- [`include()`](https://cmake.org/cmake/help/latest/command/include.html):模块搜索路径与 `CMAKE_MODULE_PATH`。
|
|
101
104
|
- [`include_guard()`](https://cmake.org/cmake/help/latest/command/include_guard.html):按目录或全局防止重复导入。
|
|
102
105
|
- [`CMAKE_MODULE_PATH`](https://cmake.org/cmake/help/latest/variable/CMAKE_MODULE_PATH.html):`include()` 的模块搜索路径。
|
|
103
106
|
- [CMake Source Code Guide](https://github.com/Kitware/CMake/blob/master/Help/dev/source.rst):只规定 C++ 风格,没有 CMake 语言风格章节。
|
|
@@ -21,7 +21,7 @@ CMake 有两套相关机制,必须同时写:
|
|
|
21
21
|
|
|
22
22
|
## 通用函数
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
`../assets/CompileSettings.cmake` 提供:
|
|
25
25
|
|
|
26
26
|
```cmake
|
|
27
27
|
CompileSettings_SetCxxStandard(app PRIVATE c++23)
|
|
@@ -34,9 +34,10 @@ CompileSettings_SetCxxStandard(greeting PUBLIC c++20)
|
|
|
34
34
|
|
|
35
35
|
## 可见性
|
|
36
36
|
|
|
37
|
-
- 库的公共接口需要该标准时用 `PUBLIC
|
|
37
|
+
- 库的公共接口需要该标准时用 `PUBLIC`:`target_compile_features` 只把“至少 C++NN”传给链接者;`CXX_STANDARD`、`CXX_STANDARD_REQUIRED` 与 `CXX_EXTENSIONS` 不随 `PUBLIC` / `INTERFACE` 传播。
|
|
38
38
|
- 程序、测试、实现文件用 `PRIVATE`。
|
|
39
|
-
- `INTERFACE` 只用于纯头文件 target
|
|
39
|
+
- `INTERFACE` 只用于纯头文件 target;它没有编译单元,只能传播最低标准,不能锁死精确的 `-std=c++NN`。
|
|
40
|
+
- 因此每个会编译的 target 都要自己调用一次函数;只靠库的 `PUBLIC`,消费者可能得到 `-std=gnu++NN`,甚至没有 `-std=`。
|
|
40
41
|
- `target_compile_features` 重复调用会追加特性;同一目标不要同时写两个不同标准。
|
|
41
42
|
|
|
42
43
|
## 常见错误
|
|
@@ -58,7 +59,7 @@ configure 后读 `build/<compiler>/compile_commands.json`,取最后一条 `-st
|
|
|
58
59
|
grep -o -- '-std=[^ ]*' build/clang/compile_commands.json | sort -u
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
|
|
62
|
+
每个编译 target 的输出都应只有一条,且等于 `-std=c++NN`;出现 `gnu++NN`、两条不同值或没有 `-std=` 都回第 1 步。`CXX_STANDARD_REQUIRED ON` 与 `CXX_EXTENSIONS OFF` 同时存在时,该 target 会得到精确的 `-std=c++NN`;只链接了 `PUBLIC` / `INTERFACE` 库的消费者不继承这两项,要自己调用函数。仍以数据库为准,不靠猜。
|
|
62
63
|
|
|
63
64
|
## 官方依据
|
|
64
65
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 正反例:标准只写一半
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cmake
|
|
6
|
+
target_compile_features(app PRIVATE cxx_std_20)
|
|
7
|
+
# 没写 CXX_STANDARD_REQUIRED;编译器不支持时可能静默降级
|
|
8
|
+
set_target_properties(app PROPERTIES CXX_STANDARD 20)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
问题:meta-feature 只保证“至少”,`CXX_STANDARD` 又允许 decay,实际标准可能不是 20。
|
|
12
|
+
|
|
13
|
+
**好**:
|
|
14
|
+
|
|
15
|
+
```cmake
|
|
16
|
+
CompileSettings_SetCxxStandard(app PRIVATE c++20)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
两套声明同时写;`CXX_STANDARD_REQUIRED ON` 禁止降级,`CXX_EXTENSIONS OFF` 把 `gnu++20` 收成 `c++20`。
|
package/skills/{building-cpp/references → engineering/references/cmake/standard}/standards/README.md
RENAMED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# 语言标准档案
|
|
2
2
|
|
|
3
|
-
本工作流默认 **C++23**,但一个工程只选一个 `standard`,不为同一份源码承诺“同时兼容 C++11–C++23
|
|
3
|
+
本工作流默认 **C++23**,但一个工程只选一个 `standard`,不为同一份源码承诺“同时兼容 C++11–C++23”。标准是项目级事实,写在项目的 `environment.json`(本书在 `.agents/skills/cpp-workflow-project/environment.json`)。
|
|
4
4
|
|
|
5
5
|
每个标准单独一个目录:`c++11/`、`c++14/`、`c++17/`、`c++20/`、`c++23/`。目录里放这一档自己的编译器下限、语言/库设施、CMake 片段和工具映射;模板仍然只有一套公共骨架,靠 `__cplusplus` 条件选择实现,不复制五份模板。
|
|
6
6
|
|
|
7
|
-
Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++23 是一处明确偏离;Google 的排版、命名、头文件和异常之外的规则不变。异常政策仍按 [exceptions.md](
|
|
7
|
+
Google C++ Style Guide 上游仍写 “target C++20”,本工作流默认 C++23 是一处明确偏离;Google 的排版、命名、头文件和异常之外的规则不变。异常政策仍按 [exceptions.md](../../../../../developing-cpp/references/exceptions/throw-rules.md)。
|
|
8
8
|
|
|
9
9
|
## 选择标准
|
|
10
10
|
|
|
@@ -72,7 +72,7 @@ set_target_properties(app PROPERTIES
|
|
|
72
72
|
- `CXX_STANDARD` 是请求值;`CXX_STANDARD_REQUIRED ON` 防止编译器不支持时静默 decay。
|
|
73
73
|
- `CXX_EXTENSIONS OFF` 把 `-std=gnu++23` 收成 `-std=c++23`。
|
|
74
74
|
- configure 后读 `compile_commands.json` 的最后一个 `-std=`;必须是 `-std=c++NN`,不能是 `gnu++NN`。
|
|
75
|
-
- CMake 最低 3.20(模板统一要求);只单独使用低档片段时,C++11/14 的 `cxx_std_*` meta-feature 最低 3.8,C++20 为 3.12。C++23 的 `CXX_STANDARD 23` 自 3.20 才有。
|
|
75
|
+
- 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
76
|
|
|
77
77
|
### clang-format
|
|
78
78
|
|
|
@@ -104,7 +104,7 @@ CompileFlags:
|
|
|
104
104
|
`.clang-tidy` 不写标准,标准来自编译数据库。检查项保持 Google 基线:
|
|
105
105
|
|
|
106
106
|
```yaml
|
|
107
|
-
Checks: '-*, google-*, readability-identifier-naming'
|
|
107
|
+
Checks: '-*, google-*, portability-avoid-pragma-once, readability-identifier-naming'
|
|
108
108
|
WarningsAsErrors: '*'
|
|
109
109
|
FormatStyle: file
|
|
110
110
|
```
|
|
@@ -145,7 +145,7 @@ target_link_options(app PRIVATE -stdlib=libc++)
|
|
|
145
145
|
| `c++20` | 必测 | 必测 | 必测 | `c++20` | `c++20` | 读 DB |
|
|
146
146
|
| `c++23` | 必测 | 必测 | 必测 | `c++23` | `Latest` | 读 DB |
|
|
147
147
|
|
|
148
|
-
本仓库在 WSL(clang 21.1.8、CMake 4.2.3、Ninja 1.13.2)上跑过四套模板 × 五档共 20 个构建:single、multi、static-library、shared-library 全部 configure/build/CTest 通过,`compile_commands.json` 都得到精确的 `-std=c++11/14/17/20/23`。模板用 `__cplusplus` 条件选择 `std::format` 或字符串拼接,默认仍交付 C++23
|
|
148
|
+
本仓库在 WSL(clang 21.1.8、CMake 4.2.3、Ninja 1.13.2)上跑过四套模板 × 五档共 20 个构建:single、multi、static-library、shared-library 全部 configure/build/CTest 通过,`compile_commands.json` 都得到精确的 `-std=c++11/14/17/20/23`。模板用 `__cplusplus` 条件选择 `std::format` 或字符串拼接,默认仍交付 C++23。可复现脚本:`scripts/checking-standard-matrix.sh`(仓库开发脚本,不随 npm 包发布)。
|
|
149
149
|
|
|
150
150
|
## 新增标准
|
|
151
151
|
|
|
@@ -159,7 +159,7 @@ target_link_options(app PRIVATE -stdlib=libc++)
|
|
|
159
159
|
|
|
160
160
|
## 相关文件
|
|
161
161
|
|
|
162
|
-
- [environment.md](
|
|
163
|
-
- [
|
|
164
|
-
- [
|
|
165
|
-
- [01-build.md](
|
|
162
|
+
- [environment.md](../../../../../building-cpp/references/environment.md):四轴声明与校验。
|
|
163
|
+
- [cxx-standard.md](../cxx-standard.md):C++11–C++23 在 CMake 里的两层声明与验证。
|
|
164
|
+
- [clang-tidy.md](../../../formatting/03-clang-tidy.md):clang-format / clang-tidy / clangd 分工。
|
|
165
|
+
- [01-build.md](../../../../../building-cpp/steps/01-build.md):定构建步骤。
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
- C++17 的全部内容。
|
|
23
23
|
- concepts、requires、ranges、`std::span`、`std::bit_cast`、`std::source_location`。
|
|
24
24
|
- `std::format`、协程、`std::jthread`、三路比较。
|
|
25
|
-
-
|
|
25
|
+
- 调用方需要按成员逐项比较时,写成 `bool operator==(const T&) const = default`。C++23 沿用,不另写一遍。
|
|
26
26
|
- 模块语法(工具链支持不完整,模板不使用)。
|
|
27
27
|
|
|
28
28
|
## 本档不能假设
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# 正反例:GLOB
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```cmake
|
|
6
|
+
file(GLOB SOURCES "src/*.cpp")
|
|
7
|
+
add_executable(app ${SOURCES})
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
新增文件不触发重新配置;不同机器上的文件集合可能不同。
|
|
11
|
+
|
|
12
|
+
**好**:
|
|
13
|
+
|
|
14
|
+
```cmake
|
|
15
|
+
add_executable(app
|
|
16
|
+
src/main.cpp
|
|
17
|
+
src/task_list.cpp
|
|
18
|
+
src/task_parser.cpp
|
|
19
|
+
)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
显式列表,新增文件时改 CMake;diff 能看出变化。
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# 正反例:共享库运行时路径
|
|
2
|
+
|
|
3
|
+
**差**:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
LD_LIBRARY_PATH=build/clang/lib ./build/clang/bin/app # 每次运行都要包一层
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
**好**:
|
|
10
|
+
|
|
11
|
+
```cmake
|
|
12
|
+
# LinkSettings.cmake 提供 RPATH 与 GoogleTest 依赖函数。
|
|
13
|
+
include(LinkSettings)
|
|
14
|
+
|
|
15
|
+
add_library(greeting SHARED greeting/src/greeting.cpp)
|
|
16
|
+
set_target_properties(greeting PROPERTIES
|
|
17
|
+
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
18
|
+
|
|
19
|
+
add_executable(app src/main.cpp)
|
|
20
|
+
target_link_libraries(app PRIVATE greeting)
|
|
21
|
+
LinkSettings_SetBuildRpath(app)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
从 `build/<compiler>/bin/app` 直接启动即可找到同级 `../lib` 下的共享库;安装场景另配 RPATH。
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# 正反例:
|
|
1
|
+
# 正反例:target 与输出目录
|
|
2
2
|
|
|
3
3
|
## 最小可用形状
|
|
4
4
|
|
|
@@ -10,7 +10,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|
|
10
10
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
11
11
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
12
12
|
|
|
13
|
-
# 先把 assets/
|
|
13
|
+
# 先把 assets/ 里的 CompileSettings.cmake 复制到工程的 cmake/。
|
|
14
14
|
include(CompileSettings)
|
|
15
15
|
|
|
16
16
|
add_library(greeting STATIC greeting/src/greeting.cpp)
|
|
@@ -44,29 +44,6 @@ target_include_directories(greeting PUBLIC greeting/include)
|
|
|
44
44
|
target_link_libraries(app PRIVATE greeting)
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
## GLOB
|
|
48
|
-
|
|
49
|
-
**差**:
|
|
50
|
-
|
|
51
|
-
```cmake
|
|
52
|
-
file(GLOB SOURCES "src/*.cpp")
|
|
53
|
-
add_executable(app ${SOURCES})
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
新增文件不触发重新配置;不同机器上的文件集合可能不同。
|
|
57
|
-
|
|
58
|
-
**好**:
|
|
59
|
-
|
|
60
|
-
```cmake
|
|
61
|
-
add_executable(app
|
|
62
|
-
src/main.cpp
|
|
63
|
-
src/task_list.cpp
|
|
64
|
-
src/task_parser.cpp
|
|
65
|
-
)
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
显式列表,新增文件时改 CMake;diff 能看出变化。
|
|
69
|
-
|
|
70
47
|
## 把 `main()` 编进库
|
|
71
48
|
|
|
72
49
|
**差**:
|
|
@@ -86,26 +63,6 @@ target_link_libraries(app PRIVATE core)
|
|
|
86
63
|
|
|
87
64
|
库不依赖 `main()`;测试链接库时不会碰到入口。
|
|
88
65
|
|
|
89
|
-
## 标准只写一半
|
|
90
|
-
|
|
91
|
-
**差**:
|
|
92
|
-
|
|
93
|
-
```cmake
|
|
94
|
-
target_compile_features(app PRIVATE cxx_std_20)
|
|
95
|
-
# 没写 CXX_STANDARD_REQUIRED;编译器不支持时可能静默降级
|
|
96
|
-
set_target_properties(app PROPERTIES CXX_STANDARD 20)
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
问题:meta-feature 只保证“至少”,`CXX_STANDARD` 又允许 decay,实际标准可能不是 20。
|
|
100
|
-
|
|
101
|
-
**好**:
|
|
102
|
-
|
|
103
|
-
```cmake
|
|
104
|
-
CompileSettings_SetCxxStandard(app PRIVATE c++20)
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
两套声明同时写;`CXX_STANDARD_REQUIRED ON` 禁止降级,`CXX_EXTENSIONS OFF` 把 `gnu++20` 收成 `c++20`。
|
|
108
|
-
|
|
109
66
|
## 警告等级
|
|
110
67
|
|
|
111
68
|
**差**:
|
|
@@ -150,31 +107,6 @@ ln -s clang/compile_commands.json build/compile_commands.json
|
|
|
150
107
|
clang-tidy -p build/clang src/main.cpp
|
|
151
108
|
```
|
|
152
109
|
|
|
153
|
-
## 共享库运行时路径
|
|
154
|
-
|
|
155
|
-
**差**:
|
|
156
|
-
|
|
157
|
-
```bash
|
|
158
|
-
LD_LIBRARY_PATH=build/clang/lib ./build/clang/bin/app # 每次运行都要包一层
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
**好**:
|
|
162
|
-
|
|
163
|
-
```cmake
|
|
164
|
-
# LinkSettings.cmake 提供 RPATH 与 GoogleTest 依赖函数。
|
|
165
|
-
include(LinkSettings)
|
|
166
|
-
|
|
167
|
-
add_library(greeting SHARED greeting/src/greeting.cpp)
|
|
168
|
-
set_target_properties(greeting PROPERTIES
|
|
169
|
-
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
|
|
170
|
-
|
|
171
|
-
add_executable(app src/main.cpp)
|
|
172
|
-
target_link_libraries(app PRIVATE greeting)
|
|
173
|
-
LinkSettings_SetBuildRpath(app)
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
从 `build/<compiler>/bin/app` 直接启动即可找到同级 `../lib` 下的共享库;安装场景另配 RPATH。
|
|
177
|
-
|
|
178
110
|
## 输出目录
|
|
179
111
|
|
|
180
112
|
**差**:可执行文件生成在源码根,`git status` 出现 `app`、`*.o`。
|