@chengzhao-dev/dsh-cpp-workflow 0.7.5 → 0.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +6 -10
- package/README.md +6 -10
- package/package.json +1 -1
- package/skills/building-cpp/SKILL.md +2 -2
- package/skills/building-cpp/assets/config/.clang-format +27 -3
- package/skills/building-cpp/assets/config/.clang-tidy +0 -4
- package/skills/building-cpp/assets/templates/projects/common/running-build.sh +49 -54
- package/skills/building-cpp/assets/templates/projects/multi/CMakeLists.txt +5 -5
- package/skills/building-cpp/assets/templates/projects/multi/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/multi/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/multi/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/CMakeLists.txt +8 -9
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/shared-library/src/main.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/single/CMakeLists.txt +3 -3
- package/skills/building-cpp/assets/templates/projects/single/src/main.cpp +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/CMakeLists.txt +4 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt +8 -8
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h +1 -4
- package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp +2 -2
- package/skills/building-cpp/assets/templates/projects/static-library/src/main.cpp +2 -2
- package/skills/building-cpp/references/01-build-script.md +1 -1
- package/skills/building-cpp/references/02-environment.md +4 -4
- package/skills/building-cpp/steps/01-build.md +1 -1
- package/skills/building-cpp/steps/02-tooling.md +3 -3
- package/skills/building-cpp/steps/03-check.md +2 -0
- package/skills/building-cpp/steps/04-vscode.md +2 -2
- package/skills/developing-cpp/SKILL.md +9 -5
- package/skills/developing-cpp/references/comments/01-comment-rules.md +15 -1
- package/skills/developing-cpp/references/comments/02-style.md +7 -6
- package/skills/developing-cpp/references/comments/examples/11-declaration-constant.md +26 -0
- package/skills/developing-cpp/references/design/02-raii.md +42 -0
- package/skills/developing-cpp/references/design/03-third-party.md +35 -0
- package/skills/developing-cpp/references/design/examples/09-option-menu.md +36 -0
- package/skills/developing-cpp/references/design/session-layer.md +16 -13
- package/skills/developing-cpp/references/index.md +10 -6
- package/skills/developing-cpp/references/language/glossary/01-language-terms.md +1 -1
- package/skills/developing-cpp/references/language/language-basics/02-initialization.md +3 -3
- package/skills/developing-cpp/references/language/value-types/04-enum.md +3 -3
- package/skills/developing-cpp/references/layout/01-source-layout.md +1 -1
- package/skills/developing-cpp/references/layout/02-file-template.md +24 -38
- package/skills/developing-cpp/references/layout/03-declaration-order.md +11 -6
- package/skills/developing-cpp/references/naming/01-identifier-names.md +7 -1
- package/skills/developing-cpp/references/style/01-google-cpp-style.md +6 -6
- package/skills/developing-cpp/scripts/checking-comments.mjs +77 -6
- package/skills/developing-cpp/scripts/checking-declaration-order.mjs +214 -13
- package/skills/developing-cpp/steps/03-declaration.md +4 -4
- package/skills/developing-cpp/steps/04-implementation.md +2 -2
- package/skills/engineering/SKILL.md +8 -8
- package/skills/engineering/references/cmake/assets/CompileSettings.cmake +152 -123
- package/skills/engineering/references/cmake/assets/LinkSettings.cmake +62 -43
- package/skills/engineering/references/cmake/assets/OutputSettings.cmake +38 -0
- package/skills/engineering/references/cmake/linking/01-libraries.md +1 -1
- package/skills/engineering/references/cmake/modules/01-functions.md +26 -16
- package/skills/engineering/references/cmake/standards/01-cxx-standard.md +2 -2
- package/skills/engineering/references/cmake/standards/c++11/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++14/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++17/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++20/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/c++23/cmake-snippet.cmake +3 -3
- package/skills/engineering/references/cmake/standards/examples/01-standard.md +1 -1
- package/skills/engineering/references/cmake/standards/index.md +5 -5
- package/skills/engineering/references/cmake/standards/versions.md +1 -1
- package/skills/engineering/references/cmake/targets/01-target-rules.md +50 -36
- package/skills/engineering/references/cmake/targets/examples/01-rpath.md +1 -1
- package/skills/engineering/references/formatting/01-encoding.md +4 -1
- package/skills/engineering/references/formatting/02-clang-format.md +4 -2
- package/skills/engineering/references/formatting/03-clang-tidy.md +2 -2
- package/skills/engineering/references/formatting/04-clangd.md +2 -2
- package/skills/engineering/references/formatting/examples/01-clang-format.md +13 -4
- package/skills/engineering/references/index.md +5 -5
- package/skills/engineering/steps/01-targets.md +7 -7
- package/skills/engineering/steps/02-modules.md +8 -5
- package/skills/engineering/steps/03-check.md +2 -1
- package/templates/cpp-workflow-project/SKILL.md +3 -2
package/README.en.md
CHANGED
|
@@ -19,19 +19,15 @@ The exception material follows the C++ Core Guidelines' Error handling section a
|
|
|
19
19
|
Fixed-version npm install:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.
|
|
22
|
+
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.6
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
This page follows each release; run `npm view @chengzhao-dev/dsh-cpp-workflow version` if you need to confirm the current version on the registry.
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.5
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
GitHub tag installs require the matching tag; once `v0.7.5` is created, the version in the address matches the tag:
|
|
27
|
+
GitHub tag installs use the tag with the same version number:
|
|
32
28
|
|
|
33
29
|
```bash
|
|
34
|
-
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.
|
|
30
|
+
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.6
|
|
35
31
|
```
|
|
36
32
|
|
|
37
33
|
Replace `--profile` with the profile you actually run. The desktop app always runs the `desktop` profile, and that profile is owned by the app; if the command-line `add` is rejected, add the plugin in the app settings and use the same pinned npm version. A Git tag install uses the prebuilt `lib/` committed in the repository and runs no build on the target machine; the package has no install script that needs approval.
|
|
@@ -70,9 +66,9 @@ Keep one `environment.json` per repository. Its first three environment axes are
|
|
|
70
66
|
| `compiler` | `clang`, `gcc` | One compiler per configuration |
|
|
71
67
|
| `standard` | `c++11`, `c++14`, `c++17`, `c++20`, `c++23` | New projects default to `c++23`; one standard per project |
|
|
72
68
|
|
|
73
|
-
The standard lives in `environment.json` as `standard` and must match the CMake target and compile database; on the CMake side, prefer `engineering`'s `
|
|
69
|
+
The standard lives in `environment.json` as `standard` and must match the CMake target and compile database; on the CMake side, prefer `engineering`'s `set_target_cxx_standard` function. New projects default to C++23; for C++11–C++20, lower `standard`, `cxx_std_*`, `CXX_STANDARD`, and the `.clang-format` parsing level together, not one at a time. Per-standard facilities, tooling, and downgrade tables are in the [standard directory](skills/engineering/references/cmake/standards/index.md); the CMake two-layer declaration is in [01-cxx-standard.md](skills/engineering/references/cmake/standards/01-cxx-standard.md).
|
|
74
70
|
|
|
75
|
-
The generator is always Ninja. Build directories
|
|
71
|
+
The generator is always Ninja. Build directories default to `build/clang/` and `build/gcc/` and are never shared; a project skill may declare a single build directory (this book uses `games/tictactoe/build/`). Analysis tools are always `clang-format`, `clang-tidy`, and `clangd`; GCC mode does not switch to another analyzer.
|
|
76
72
|
|
|
77
73
|
Run the declaration check first, then the live check on the build host. `<skill-dir>` is the installed `building-cpp` directory, not a directory in the target project:
|
|
78
74
|
|
package/README.md
CHANGED
|
@@ -19,19 +19,15 @@
|
|
|
19
19
|
npm 安装固定版本:
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.
|
|
22
|
+
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.6
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
本页版本号随发布更新;安装前可用 `npm view @chengzhao-dev/dsh-cpp-workflow version` 确认 registry 上的最新版本。
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
dsh plugin --profile <profile> add npm:@chengzhao-dev/dsh-cpp-workflow@0.7.5
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
GitHub 标签安装要等 `v0.7.5` 创建后再用;标签创建后,地址中的版本号与标签同号:
|
|
27
|
+
GitHub 标签安装使用同一版本号的标签,地址中的版本与标签同号:
|
|
32
28
|
|
|
33
29
|
```bash
|
|
34
|
-
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.
|
|
30
|
+
dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.6
|
|
35
31
|
```
|
|
36
32
|
|
|
37
33
|
`--profile` 换成你实际运行的那个 profile。桌面应用固定跑 `desktop` profile,且该 profile 由应用独占管理;命令行 `add` 被拒绝时,在应用设置里添加插件,地址填同一个 npm 版本。Git 标签安装使用仓库内已构建的 `lib/`,不在目标机器上重新构建;包内也不含需要授权执行的安装脚本。
|
|
@@ -70,9 +66,9 @@ dsh plugin --profile <profile> add github:chengzhao-dev/dsh-cpp-workflow#v0.7.5
|
|
|
70
66
|
| `compiler` | `clang`、`gcc` | 一次配置一个编译器 |
|
|
71
67
|
| `standard` | `c++11`、`c++14`、`c++17`、`c++20`、`c++23` | 新工程默认 `c++23`;一个工程一个标准 |
|
|
72
68
|
|
|
73
|
-
标准写在 `environment.json` 的 `standard`,并同步到 CMake target 和编译数据库;CMake 侧优先调用 `engineering` 提供的 `
|
|
69
|
+
标准写在 `environment.json` 的 `standard`,并同步到 CMake target 和编译数据库;CMake 侧优先调用 `engineering` 提供的 `set_target_cxx_standard` 函数。新工程默认 C++23;C++11–C++20 工程把 `standard`、`cxx_std_*`、`CXX_STANDARD` 和 `.clang-format` 的解析档位一起降级,不要只改一处。每档的设施、工具和降档表见 [标准目录](skills/engineering/references/cmake/standards/index.md),CMake 两层声明见 [01-cxx-standard.md](skills/engineering/references/cmake/standards/01-cxx-standard.md)。
|
|
74
70
|
|
|
75
|
-
生成器固定 Ninja
|
|
71
|
+
生成器固定 Ninja,默认构建目录是 `build/clang/` 与 `build/gcc/`,不共用同一目录;项目技能声明单一构建目录时(本书 `games/tictactoe/build/`)以项目为准。诊断工具固定 `clang-format`、`clang-tidy`、`clangd`;GCC 模式也不换成别的分析器。
|
|
76
72
|
|
|
77
73
|
构建前先跑声明校验,再在构建宿主上跑活动校验。`<skill-dir>` 是插件安装后的 `building-cpp` 目录,不是目标工程里的目录:
|
|
78
74
|
|
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@ metadata:
|
|
|
16
16
|
- **交接**:一次改动同时碰到源码和构建文件时,先开 `developing-cpp` 定行为、边界与实现;需要新增或移动源文件、改库与程序的边界、补测试 target 时,转 `engineering` 把文件写进 CMake;最后回到本技能定工具配置并通过检查。只改函数体、命名、异常或注释且入口命令可运行时,定实现后只开本技能的通过检查。
|
|
17
17
|
- **前置**:存在项目技能 `cpp-workflow-project` 时先读它的 `SKILL.md`、`environment.json` 和 `stages/stage-table.md`。三个环境轴为空、`standard` 非法或组合非法时停在定构建,不猜 WSL、Clang 或构建目录;`standard` 缺失或为空时按默认 `c++23` 继续并提示声明升级;`windows` + `native` 直接拒绝,提示「本版本不能在 Windows 原生壳里构建」,不给补 PowerShell 的路线。
|
|
18
18
|
- **分因**:编译失败时先看诊断指向哪一层。函数体问题回 `developing-cpp` 的定实现;找不到头、链接失败、target 不存在或标准不对转 `engineering` 的对应步骤;没有编译数据库、format/tidy 不在 PATH 或配置不生效留在本技能的定工具配置。
|
|
19
|
-
- **编码**:源码与构建文件用 UTF-8 无 BOM、检出 LF,`.bat` / `.cmd` 用 CRLF,`.clang-format` 写 `LineEnding: LF`;MSVC
|
|
19
|
+
- **编码**:源码与构建文件用 UTF-8 无 BOM、检出 LF,`.bat` / `.cmd` 用 CRLF,`.clang-format` 写 `LineEnding: LF`;MSVC 不是受支持的环境轴;`/utf-8` 配方只留在资产注释里,当前构建仍用 clang/gcc。完整规则见 `../engineering/references/formatting/01-encoding.md`,机器规则以仓库根 `.gitattributes` 与 `.editorconfig` 为准。
|
|
20
20
|
- **编辑器**:工程根已有 `.vscode/` 时按 `steps/04-vscode.md` 接线;从当前目录向上到 git 根都没有 `.vscode/` 时不打开、也不新建。配置、构建、运行和调试都从 CMake Tools 进入,构建仍以 `running-build.sh` 为准。
|
|
21
21
|
|
|
22
22
|
## 三种操作
|
|
@@ -78,7 +78,7 @@ metadata:
|
|
|
78
78
|
|
|
79
79
|
- [ ] 项目 `environment.json` 的三个环境轴齐全且是合法组合,`standard` 在 `c++11`–`c++23` 内;新工程默认 `c++23`,没有靠猜。
|
|
80
80
|
- [ ] 配置命令固定 Ninja 和单一编译器,`build/<compiler>/compile_commands.json` 存在,根 `build/compile_commands.json` 指向当前编译器。
|
|
81
|
-
- [ ] target 契约已经成立:程序 `app
|
|
81
|
+
- [ ] target 契约已经成立:程序 target(模板默认 `app`,本书 `terminal`)、库 target、公共 include、标准与输出目录都已由 `engineering` 写好;本技能没有偷偷改 CMake 来绕过失败。
|
|
82
82
|
- [ ] `clang-format --dry-run --Werror` 与 `clang-tidy` 在项目源码上零告警;只扫描项目源码和项目头。
|
|
83
83
|
- [ ] CTest 全绿,程序从该编译器的构建目录启动;有测试时注册用 `gtest_discover_tests` 或项目既有机制,运行带 `--output-on-failure --no-tests=error`,失败时报告的是具体角色和原因。
|
|
84
84
|
- [ ] 没有改源码语义,没有覆盖已有工程文件,也没有安装或下载任何工具。
|
|
@@ -1,7 +1,31 @@
|
|
|
1
|
-
#
|
|
2
|
-
#
|
|
1
|
+
# C++ 代码排版配置。
|
|
2
|
+
#
|
|
3
|
+
# Google 基线,缩进用 4 个空格,对齐用空格,列宽 120。
|
|
4
|
+
|
|
5
|
+
# 只处理 C++ 源文件。
|
|
3
6
|
Language: Cpp
|
|
7
|
+
# 只覆盖本文件写出的项,其余沿用预设。
|
|
4
8
|
BasedOnStyle: Google
|
|
9
|
+
# 工具不接受 c++23 取值,改用 Latest 跟随其支持的最高标准。
|
|
5
10
|
Standard: Latest
|
|
6
|
-
|
|
11
|
+
|
|
12
|
+
# include 保持现有空行分组,组内按 Google 预设排序。
|
|
13
|
+
IncludeBlocks: Preserve
|
|
14
|
+
|
|
15
|
+
# 每行最多 120 列。
|
|
16
|
+
ColumnLimit: 120
|
|
17
|
+
# 一级缩进 4 个空格。
|
|
18
|
+
IndentWidth: 4
|
|
19
|
+
# Tab 按 4 列显示,只用于阅读既有文件。
|
|
20
|
+
TabWidth: 4
|
|
21
|
+
# 缩进与对齐都用空格,不写 Tab。
|
|
22
|
+
UseTab: Never
|
|
23
|
+
# 续行多缩进 8 个空格。
|
|
24
|
+
ContinuationIndentWidth: 8
|
|
25
|
+
# 访问修饰符不缩进,与 class/struct 左对齐。
|
|
26
|
+
AccessModifierOffset: -4
|
|
27
|
+
# 构造函数初始化列表尽量排在一行。
|
|
28
|
+
PackConstructorInitializers: BinPack
|
|
29
|
+
|
|
30
|
+
# 行尾统一为 LF。
|
|
7
31
|
LineEnding: LF
|
|
@@ -5,7 +5,6 @@ InheritParentConfig: false
|
|
|
5
5
|
Checks: >
|
|
6
6
|
-*,
|
|
7
7
|
google-*,
|
|
8
|
-
portability-avoid-pragma-once,
|
|
9
8
|
readability-identifier-naming
|
|
10
9
|
|
|
11
10
|
WarningsAsErrors: '*'
|
|
@@ -59,6 +58,3 @@ CheckOptions:
|
|
|
59
58
|
value: lower_case
|
|
60
59
|
- key: readability-identifier-naming.MacroDefinitionCase
|
|
61
60
|
value: UPPER_CASE
|
|
62
|
-
# 包含守卫按 Google 规则以 _H_ 结尾,UPPER_CASE 会把它判成多余下划线。
|
|
63
|
-
- key: readability-identifier-naming.MacroDefinitionIgnoredRegexp
|
|
64
|
-
value: '^[A-Z][A-Z0-9_]*_H_$'
|
|
@@ -1,86 +1,81 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
# 配置、构建、测试并运行当前 C++11–C++23 工程(默认 C++23)。
|
|
4
4
|
#
|
|
5
5
|
# 用法:./running-build.sh [clang|gcc];省略时用 clang。
|
|
6
|
-
#
|
|
7
|
-
# 编译数据库:build/<compiler>/compile_commands.json
|
|
8
|
-
# 适用于 Linux、macOS 和 Windows + WSL
|
|
9
|
-
# 本版本不支持 Windows 原生壳。
|
|
10
|
-
#===------------------------------------------------------------------------===#
|
|
6
|
+
# 程序名与构建目录可用 APP_NAME、BUILD_DIR 覆盖;默认 app 与 build/<compiler>。
|
|
7
|
+
# 编译数据库:build/<compiler>/compile_commands.json。
|
|
8
|
+
# 适用于 Linux、macOS 和 Windows + WSL,不支持 Windows 原生壳。
|
|
11
9
|
|
|
12
10
|
set -euo pipefail
|
|
13
11
|
cd "$(dirname "$0")"
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
APP_NAME="${APP_NAME:-app}"
|
|
14
|
+
BUILD_ROOT="${BUILD_ROOT:-build}"
|
|
17
15
|
|
|
16
|
+
# 解析编译器参数,只接受 clang 与 gcc。
|
|
18
17
|
COMPILER="${1:-clang}"
|
|
19
18
|
case "${COMPILER}" in
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
clang)
|
|
20
|
+
readonly CXX="clang++"
|
|
21
|
+
;;
|
|
22
|
+
gcc)
|
|
23
|
+
readonly CXX="g++"
|
|
24
|
+
;;
|
|
25
|
+
*)
|
|
26
|
+
printf '用法: %s [clang|gcc]\n' "$0" >&2
|
|
27
|
+
exit 2
|
|
28
|
+
;;
|
|
30
29
|
esac
|
|
31
30
|
readonly COMPILER CXX
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
#===------------------------------------------------------------------------===#
|
|
35
|
-
# 输出辅助
|
|
36
|
-
#===------------------------------------------------------------------------===#
|
|
31
|
+
BUILD_DIR="${BUILD_DIR:-${BUILD_ROOT}/${COMPILER}}"
|
|
37
32
|
|
|
33
|
+
# 打印阶段标题。
|
|
38
34
|
header() {
|
|
39
|
-
|
|
35
|
+
printf '\n== %s ==\n' "$1"
|
|
40
36
|
}
|
|
41
37
|
|
|
38
|
+
# 打印错误。
|
|
42
39
|
err() {
|
|
43
|
-
|
|
40
|
+
printf '错误: %s\n' "$1" >&2
|
|
44
41
|
}
|
|
45
42
|
|
|
43
|
+
# 命令失败时打印退出码与命令。
|
|
46
44
|
on_error() {
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
local status=$?
|
|
46
|
+
printf '失败(退出码 %s):%s\n' "${status}" "${BASH_COMMAND}" >&2
|
|
49
47
|
}
|
|
50
48
|
trap on_error ERR
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
# 主流程
|
|
54
|
-
#===------------------------------------------------------------------------===#
|
|
55
|
-
|
|
50
|
+
# 入口:配置、构建、导出编译数据库、测试并运行。
|
|
56
51
|
main() {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
header "配置 (${COMPILER})"
|
|
53
|
+
cmake -S . -B "${BUILD_DIR}" -G Ninja \
|
|
54
|
+
-DCMAKE_BUILD_TYPE=Debug \
|
|
55
|
+
-DCMAKE_C_COMPILER="${COMPILER}" \
|
|
56
|
+
-DCMAKE_CXX_COMPILER="${CXX}" \
|
|
57
|
+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
|
|
63
58
|
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
header "构建"
|
|
60
|
+
cmake --build "${BUILD_DIR}"
|
|
66
61
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
header "编译数据库"
|
|
63
|
+
rm -f "${BUILD_ROOT}/compile_commands.json"
|
|
64
|
+
ln -s "${COMPILER}/compile_commands.json" \
|
|
65
|
+
"${BUILD_ROOT}/compile_commands.json"
|
|
71
66
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
67
|
+
header "测试"
|
|
68
|
+
if [ -d tests ]; then
|
|
69
|
+
ctest --test-dir "${BUILD_DIR}" --output-on-failure --no-tests=error
|
|
70
|
+
else
|
|
71
|
+
ctest --test-dir "${BUILD_DIR}" --output-on-failure
|
|
72
|
+
fi
|
|
78
73
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
74
|
+
header "运行 ${APP_NAME}"
|
|
75
|
+
(
|
|
76
|
+
cd "${BUILD_DIR}/bin"
|
|
77
|
+
"./${APP_NAME}"
|
|
78
|
+
)
|
|
84
79
|
}
|
|
85
80
|
|
|
86
81
|
main "$@"
|
|
@@ -7,15 +7,15 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
|
|
|
7
7
|
enable_testing()
|
|
8
8
|
|
|
9
9
|
add_executable(app
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
src/main.cpp
|
|
11
|
+
src/greeting.cpp
|
|
12
12
|
)
|
|
13
13
|
target_include_directories(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
14
14
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
15
15
|
set_target_properties(app PROPERTIES
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
CXX_STANDARD 23
|
|
17
|
+
CXX_STANDARD_REQUIRED ON
|
|
18
|
+
CXX_EXTENSIONS OFF
|
|
19
19
|
)
|
|
20
20
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
21
21
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define CPP_WORKFLOW_TEMPLATE_MULTI_INCLUDE_GREETING_H_
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <string>
|
|
5
4
|
|
|
@@ -8,5 +7,3 @@ namespace greeting {
|
|
|
8
7
|
std::string MakeGreeting(const std::string& name);
|
|
9
8
|
|
|
10
9
|
} // namespace greeting
|
|
11
|
-
|
|
12
|
-
#endif // CPP_WORKFLOW_TEMPLATE_MULTI_INCLUDE_GREETING_H_
|
|
@@ -9,9 +9,9 @@ namespace greeting {
|
|
|
9
9
|
|
|
10
10
|
std::string MakeGreeting(const std::string& name) {
|
|
11
11
|
#if __cplusplus >= 202002L
|
|
12
|
-
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
13
|
#else
|
|
14
|
-
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
15
|
#endif
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -12,17 +12,16 @@ add_subdirectory(greeting)
|
|
|
12
12
|
add_executable(app src/main.cpp)
|
|
13
13
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
14
14
|
set_target_properties(app PROPERTIES
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
CXX_STANDARD 23
|
|
16
|
+
CXX_STANDARD_REQUIRED ON
|
|
17
|
+
CXX_EXTENSIONS OFF
|
|
18
18
|
)
|
|
19
|
-
target_link_libraries(app PRIVATE greeting)
|
|
20
19
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
21
|
-
|
|
22
|
-
add_test(NAME app_smoke COMMAND app)
|
|
23
|
-
|
|
20
|
+
target_link_libraries(app PRIVATE greeting)
|
|
24
21
|
if(APPLE)
|
|
25
|
-
|
|
22
|
+
set_target_properties(app PROPERTIES BUILD_RPATH "@loader_path/../lib")
|
|
26
23
|
else()
|
|
27
|
-
|
|
24
|
+
set_target_properties(app PROPERTIES BUILD_RPATH "$ORIGIN/../lib")
|
|
28
25
|
endif()
|
|
26
|
+
|
|
27
|
+
add_test(NAME app_smoke COMMAND app)
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/CMakeLists.txt
CHANGED
|
@@ -2,14 +2,14 @@ add_library(greeting SHARED src/greeting.cpp)
|
|
|
2
2
|
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
3
3
|
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
4
|
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
8
8
|
)
|
|
9
9
|
target_compile_options(greeting PRIVATE
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
-Wall
|
|
11
|
+
-Wextra
|
|
12
|
+
-Werror
|
|
13
|
+
-Wold-style-cast
|
|
14
|
+
-Wshadow
|
|
15
15
|
)
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/include/greeting.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <string>
|
|
5
4
|
|
|
@@ -8,5 +7,3 @@ namespace greeting {
|
|
|
8
7
|
std::string MakeGreeting(const std::string& name);
|
|
9
8
|
|
|
10
9
|
} // namespace greeting
|
|
11
|
-
|
|
12
|
-
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
package/skills/building-cpp/assets/templates/projects/shared-library/greeting/src/greeting.cpp
CHANGED
|
@@ -9,9 +9,9 @@ namespace greeting {
|
|
|
9
9
|
|
|
10
10
|
std::string MakeGreeting(const std::string& name) {
|
|
11
11
|
#if __cplusplus >= 202002L
|
|
12
|
-
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
13
|
#else
|
|
14
|
-
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
15
|
#endif
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -9,9 +9,9 @@ enable_testing()
|
|
|
9
9
|
add_executable(app src/main.cpp)
|
|
10
10
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
11
11
|
set_target_properties(app PROPERTIES
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
CXX_STANDARD 23
|
|
13
|
+
CXX_STANDARD_REQUIRED ON
|
|
14
|
+
CXX_EXTENSIONS OFF
|
|
15
15
|
)
|
|
16
16
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
17
17
|
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
#endif
|
|
6
6
|
|
|
7
7
|
int main() {
|
|
8
|
-
|
|
8
|
+
const std::string name = "C++";
|
|
9
9
|
#if __cplusplus >= 202002L
|
|
10
|
-
|
|
10
|
+
std::cout << std::format("Hello, {}!\n", name);
|
|
11
11
|
#else
|
|
12
|
-
|
|
12
|
+
std::cout << "Hello, " << name << "!\n";
|
|
13
13
|
#endif
|
|
14
|
-
|
|
14
|
+
return 0;
|
|
15
15
|
}
|
|
@@ -12,11 +12,11 @@ add_subdirectory(greeting)
|
|
|
12
12
|
add_executable(app src/main.cpp)
|
|
13
13
|
target_compile_features(app PRIVATE cxx_std_23)
|
|
14
14
|
set_target_properties(app PROPERTIES
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
CXX_STANDARD 23
|
|
16
|
+
CXX_STANDARD_REQUIRED ON
|
|
17
|
+
CXX_EXTENSIONS OFF
|
|
18
18
|
)
|
|
19
|
-
target_link_libraries(app PRIVATE greeting)
|
|
20
19
|
target_compile_options(app PRIVATE -Wall -Wextra -Werror)
|
|
20
|
+
target_link_libraries(app PRIVATE greeting)
|
|
21
21
|
|
|
22
22
|
add_test(NAME app_smoke COMMAND app)
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/CMakeLists.txt
CHANGED
|
@@ -2,14 +2,14 @@ add_library(greeting STATIC src/greeting.cpp)
|
|
|
2
2
|
target_include_directories(greeting PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
3
3
|
target_compile_features(greeting PUBLIC cxx_std_23)
|
|
4
4
|
set_target_properties(greeting PROPERTIES
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
CXX_STANDARD 23
|
|
6
|
+
CXX_STANDARD_REQUIRED ON
|
|
7
|
+
CXX_EXTENSIONS OFF
|
|
8
8
|
)
|
|
9
9
|
target_compile_options(greeting PRIVATE
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
-Wall
|
|
11
|
+
-Wextra
|
|
12
|
+
-Werror
|
|
13
|
+
-Wold-style-cast
|
|
14
|
+
-Wshadow
|
|
15
15
|
)
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/include/greeting.h
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
#define CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
|
1
|
+
#pragma once
|
|
3
2
|
|
|
4
3
|
#include <string>
|
|
5
4
|
|
|
@@ -8,5 +7,3 @@ namespace greeting {
|
|
|
8
7
|
std::string MakeGreeting(const std::string& name);
|
|
9
8
|
|
|
10
9
|
} // namespace greeting
|
|
11
|
-
|
|
12
|
-
#endif // CPP_WORKFLOW_TEMPLATE_GREETING_INCLUDE_GREETING_H_
|
package/skills/building-cpp/assets/templates/projects/static-library/greeting/src/greeting.cpp
CHANGED
|
@@ -9,9 +9,9 @@ namespace greeting {
|
|
|
9
9
|
|
|
10
10
|
std::string MakeGreeting(const std::string& name) {
|
|
11
11
|
#if __cplusplus >= 202002L
|
|
12
|
-
|
|
12
|
+
return std::format("Hello, {}!", name);
|
|
13
13
|
#else
|
|
14
|
-
|
|
14
|
+
return "Hello, " + name + "!";
|
|
15
15
|
#endif
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -29,7 +29,7 @@ clang-format --dry-run --Werror src/*.cpp include/*.h
|
|
|
29
29
|
|
|
30
30
|
## 结构
|
|
31
31
|
|
|
32
|
-
1. **shebang +
|
|
32
|
+
1. **shebang + 文件头**:标题一行、`#` 一行,再用「用法」「依赖」「构建目录」等行说明参数与支持的 build host;段落用单行注释,不画 `#===` 横幅。
|
|
33
33
|
2. **严格模式与目录**:`set -euo pipefail`;`cd "$(dirname "$0")"`;`readonly BUILD_DIR`、`readonly APP_NAME="app"`。
|
|
34
34
|
3. **输出辅助**:`header()` 打印阶段名,`err()` 打印到 stderr;`trap ERR` 打印失败命令。
|
|
35
35
|
4. **configure**:每次运行都重新 configure,不复用缓存;传 `-G Ninja`、`-DCMAKE_BUILD_TYPE=Debug`、`-DCMAKE_C_COMPILER`、`-DCMAKE_CXX_COMPILER`、`-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`。C 与 C++ 使用同一套编译器;没有 C 语言的工程会忽略 C 编译器设置。
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
| --- | --- | --- |
|
|
10
10
|
| `host` | `windows` / `linux` / `macos` | 编辑器与代理壳所在的系统 |
|
|
11
11
|
| `build_host` | `native` / `wsl` | 编译在哪执行;`wsl` 只对 `host=windows` 合法 |
|
|
12
|
-
| `compiler` | `clang` / `gcc` | 一次只配一个;不支持 MSVC、clang-cl,也不把 C
|
|
12
|
+
| `compiler` | `clang` / `gcc` | 一次只配一个;不支持 MSVC、clang-cl,也不把 C 编译器单独作为一个轴;资产里的 MSVC 分支只供阅读,不进入声明 |
|
|
13
13
|
| `standard` | `c++11` / `c++14` / `c++17` / `c++20` / `c++23` | 一个工程一个标准;新工程默认 `c++23`,旧声明缺字段时也按 `c++23` 处理 |
|
|
14
14
|
|
|
15
15
|
```json
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
- `host=windows` + `build_host=native`:本版本不支持,静态校验直接返回失败。
|
|
41
41
|
- `compiler=msvc`、`compiler=clang-cl`、`compiler=c`。
|
|
42
42
|
|
|
43
|
-
`windows` + `native`
|
|
43
|
+
`windows` + `native` 不再作为「合法但不能构建」的声明保留;CMake 资产里的 MSVC 分支不是受支持的环境,不能据此写 `compiler=msvc`。声明校验直接返回 `ok: false`,`issues` 只有一句「本版本不能在 Windows 原生壳里构建」;不要建议改 WSL,也不要临时写 PowerShell 脚本伪装成已支持。
|
|
44
44
|
|
|
45
45
|
## 工具角色
|
|
46
46
|
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
| 语言服务 | `clangd` | `clangd` |
|
|
53
53
|
| 构建 | `cmake` + `ninja` | `cmake` + `ninja` |
|
|
54
54
|
| 测试 | `ctest` | `ctest` |
|
|
55
|
-
| 程序 |
|
|
55
|
+
| 程序 | 项目技能点名的 target | 同一名字 |
|
|
56
56
|
|
|
57
57
|
工具角色与编译器解耦:GCC 模式下仍用 Clang 的格式化、静态检查和语言服务。不要因为编译器是 GCC 就换成 `gcc-format` 之类不存在的工具。工程确有 C 源时,C 编译器用同一套的 `clang` 或 `gcc`,不能与 C++ 编译器分属两套;`--live` 的 compiler 角色核对的是 C++ 驱动,C 驱动缺失时由 configure 报错。
|
|
58
58
|
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
| gcc | `build/gcc/` | `build/gcc/compile_commands.json` |
|
|
74
74
|
| 当前 | `build/` | `build/compile_commands.json` → 上面之一的符号链接 |
|
|
75
75
|
|
|
76
|
-
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER`
|
|
76
|
+
一次 configure 只用一个编译器;切换编译器时用另一个目录,不要在同一目录里改 `CMAKE_CXX_COMPILER` 后复用缓存。项目技能声明单一构建目录时以项目为准(本书 `games/tictactoe/build/`),此时不建 `build/clang` 与 `build/gcc`,编译数据库仍在 `<build-dir>/compile_commands.json`。
|
|
77
77
|
|
|
78
78
|
## 活动校验
|
|
79
79
|
|
|
@@ -30,7 +30,7 @@ build/
|
|
|
30
30
|
|
|
31
31
|
## 规则
|
|
32
32
|
|
|
33
|
-
- 生成器固定单配置 Ninja,不用 Ninja Multi-Config
|
|
33
|
+
- 生成器固定单配置 Ninja,不用 Ninja Multi-Config:同一源文件会写多条命令,读数据库时容易误选;构建目录固定 `build/clang` 或 `build/gcc`。
|
|
34
34
|
- 一个 configure 只用一个编译器:`-DCMAKE_CXX_COMPILER=clang++` 或 `g++`;确有 C 源时 C 编译器与 C++ 编译器同属一套。
|
|
35
35
|
- `set(CMAKE_EXPORT_COMPILE_COMMANDS ON)` 由 `engineering` 写在顶层;Ninja 下产出 `compile_commands.json`。
|
|
36
36
|
- 输出目录由 `engineering` 统一设到 `build/<compiler>/bin` 与 `build/<compiler>/lib`;本步只核对,不重复设。
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
- `.clangd` 的 `CompilationDatabase` 指向 `build/<compiler>`;切换编译器时同步改,不能留旧目录。**不要写 `-std`**:它会覆盖 `compile_commands.json`,把 C++23 工程降成旧标准;标准只由构建声明和编译数据库决定。
|
|
14
14
|
- `--query-driver` 是 clangd 的命令行参数,写在 `.vscode/settings.json` 的 `clangd.arguments` 里,不是 `.clangd` 的 YAML 键。它告诉 clangd 允许执行哪个编译器驱动来取系统头路径;路径写绝对路径,并与 `environment.json` 的编译器一致。
|
|
15
15
|
- 首次复制时按轴替换三处:`compiler=clang` 用 `CompilationDatabase: build/clang`、`--query-driver=/usr/bin/clang++` 和 `cmake.debugConfig` 的 `MIMode: lldb` / `miDebuggerPath: /usr/bin/lldb`;`compiler=gcc` 用 `build/gcc`、`/usr/bin/g++` 和 `MIMode: gdb` / `/usr/bin/gdb`。只复制模板而不改这三处,GCC 模式会指向错误的数据库、驱动或调试器。原工程已有的 `.clangd` 不覆盖;要改先征得同意。
|
|
16
|
-
- `.clang-format` 保留 `Language: Cpp`、`BasedOnStyle: Google`、`Standard` 和 `LineEnding: LF`:模板资产写 `Latest`;工程按 `environment.json` 声明 C++11–C++20 时,把 `Standard` 改成 `c++NN`(映射见 `../../engineering/references/cmake/standards/index.md
|
|
17
|
-
- `.clang-tidy` 用 `Checks: '-*, google-*,
|
|
16
|
+
- `.clang-format` 保留 `Language: Cpp`、`BasedOnStyle: Google`、`Standard` 和 `LineEnding: LF`:模板资产写 `Latest`;工程按 `environment.json` 声明 C++11–C++20 时,把 `Standard` 改成 `c++NN`(映射见 `../../engineering/references/cmake/standards/index.md`)。`Standard` 不接受 `c++23` 的旧工具会报 `unknown enumerated scalar`;C++23 工程统一写 `Latest`,由工具取它支持的最高标准。缩进与列宽默认 4 个空格、120 列,模板资产显式写 `UseTab: Never`、`TabWidth: 4`、`IndentWidth: 4`、`ContinuationIndentWidth: 8`、`AccessModifierOffset: -4` 与 `ColumnLimit: 120`;工程有自己配置时按工程值执行。包含分块保持 include 现有空行分组,组内顺序交给 Google 预设。需要新规则时先改 `../../developing-cpp/references/style/01-google-cpp-style.md` 并说明理由。
|
|
17
|
+
- `.clang-tidy` 用 `Checks: '-*, google-*, readability-identifier-naming'`,保留 `WarningsAsErrors: '*'` 和 `FormatStyle: file`。`google-*` 覆盖 Google 风格能静态检查的部分;`readability-identifier-naming` 只配项目确实统一的命名项:类型/枚举/别名/模板参数 PascalCase,变量/参数/成员 snake_case,类成员尾随 `_`,结构体成员不尾随,常量 `k` + PascalCase。不要设 `FunctionCase`,Google 允许 `count()` / `set_count()` 这类访问器;不要打开 `portability-avoid-pragma-once`,工程统一用 `#pragma once`,打开会把每个头文件报成问题;不要重新打开 `cppcoreguidelines-*` 整包。
|
|
18
18
|
- `.clang-tidy` 关闭项和例外必须写理由;不要借 `-*` 之后再打开 `cppcoreguidelines-*` 这类非 Google 整族检查。
|
|
19
19
|
- `.vscode/extensions.json` 推荐 clangd、CMake Tools 与 C/C++ 扩展;`.vscode/settings.json` 固定 clangd 参数、CMake 构建目录和 `cmake.debugConfig`。配置、构建、运行和调试只从 CMake Tools 进入,不写 `tasks.json` 或 `launch.json`;C/C++ 扩展只作为 CMake 快速调试的后端,IntelliSense 关闭,避免和 clangd 并行。配置注释用 JSONC 的 `//`,贴在被解释的键上方。
|
|
20
|
-
-
|
|
20
|
+
- 配置文件以标题一行、`#` 一行、一行用途开头;每个键上方一句说明,写这一项控制什么;取值有取舍或偏离预设时再补一句为什么。不写策略说明段,也不指向文档路径。
|
|
21
21
|
- 格式化与静态检查只扫描项目源码:`src/`、`include/`、`app/`、`tests/` 等实际存在的源码根;不扫描 `build/`、第三方目录、生成文件。
|
|
22
22
|
- `clang-tidy` 用 `-p build/<compiler>` 读取编译数据库,`--header-filter` 只列项目根下的顶层目录,避免把第三方头纳入;不要加 `--system-headers`,也不要用 clang-tidy 19 才加入、16 没有的 `--exclude-header-filter`。
|
|
23
23
|
- 格式化用 `--dry-run --Werror` 做检查;需要自动修复时单独运行 `-i`,不要让构建脚本悄悄改源码。`clang-format` 只排版,不会把行尾注释搬到上一行;数据成员注释位置按 `../../developing-cpp/references/comments/01-comment-rules.md` 检查。
|