|
1 | 1 | --- |
2 | 2 | name: mcpp-usage |
3 | | -description: Use when helping users install, configure, or use mcpp — the C++23 modular build tool. Covers installation, project creation, building, dependency management, toolchain management, workspace setup, packaging, diagnostics, and troubleshooting. |
| 3 | +description: Use when helping users install, configure, or use mcpp - the C++23 modular build tool. The usage lives in docs/mcpp-skills and in the binary. |
4 | 4 | --- |
5 | 5 |
|
6 | | -# mcpp 基础用法 |
| 6 | +# mcpp usage |
7 | 7 |
|
8 | | -## Overview |
| 8 | +How to use mcpp is stated once, in the sheets under |
| 9 | +[`docs/mcpp-skills/`](../../../docs/mcpp-skills/mcpp/SKILL.md), which the build renders into |
| 10 | +the binary: |
9 | 11 |
|
10 | | -mcpp 是一个现代 C++ 模块化构建工具,纯 C++23 模块编写,已实现自举。一条命令安装,`import std` 开箱即用。 |
11 | | - |
12 | | -- 仓库:https://github.com/mcpp-community/mcpp |
13 | | -- 文档:https://github.com/mcpp-community/mcpp/tree/main/docs |
14 | | -- 包索引:https://github.com/mcpp-community/mcpp-index |
15 | | -- 模块化库:https://github.com/mcpplibs |
16 | | - |
17 | | -## Quick Reference |
18 | | - |
19 | | -| 命令 | 用途 | |
20 | | -|---|---| |
21 | | -| `mcpp new <name>` | 创建项目 | |
22 | | -| `mcpp build` | 构建 | |
23 | | -| `mcpp run [-- args]` | 构建并运行 | |
24 | | -| `mcpp test [pattern] [-- args]` | 发现并运行 `tests/**/*.cpp` 测试 | |
25 | | -| `mcpp add <pkg>[@ver]` | 添加依赖 | |
26 | | -| `mcpp remove <pkg>` | 移除依赖 | |
27 | | -| `mcpp update [pkg]` | 更新依赖 | |
28 | | -| `mcpp search <keyword>` | 搜索包 | |
29 | | -| `mcpp toolchain list` | 查看工具链 | |
30 | | -| `mcpp toolchain install gcc 16` | 安装工具链 | |
31 | | -| `mcpp pack` | 打包 | |
32 | | -| `mcpp why [toolchain|runtime|deps]` | 解释解析出的构建决策 | |
33 | | -| `mcpp --offline` | 只使用已有本地状态 | |
34 | | -| `mcpp self doctor` | 环境诊断 | |
35 | | -| `mcpp explain <CODE>` | 错误码解释 | |
36 | | - |
37 | | -## 安装 |
38 | | - |
39 | | -```bash |
40 | | -# 推荐;Windows 请在 PowerShell 中运行 |
41 | | -xlings install mcpp -y |
42 | | - |
43 | | -# 或 Unix release 一键脚本(仅 Linux x86_64/aarch64 与 macOS ARM64) |
44 | | -curl -fsSL https://github.com/mcpp-community/mcpp/releases/latest/download/install.sh | bash |
45 | | -``` |
46 | | - |
47 | | -一键脚本不支持 Windows;Windows 使用 PowerShell 的 xlings 安装命令。安装到 |
48 | | -`~/.mcpp/` 后会自动加入 PATH。首次使用时,mcpp 按宿主选择默认工具链并 |
49 | | -安装到隔离沙盒:Linux 通常为 GCC,macOS 为 LLVM,Windows 在有可用 MSVC |
50 | | -时为 LLVM,否则为面向 `x86_64-windows-gnu` 的 MinGW-w64 GCC。 |
51 | | - |
52 | | -## 创建项目 |
53 | | - |
54 | | -```bash |
55 | | -mcpp new hello && cd hello |
56 | | -mcpp build |
57 | | -mcpp run |
58 | | -``` |
59 | | - |
60 | | -生成的项目包含最小 manifest 和可立即运行的 smoke test: |
61 | | - |
62 | | -```toml |
63 | | -[package] |
64 | | -name = "hello" |
65 | | -version = "0.1.0" |
66 | | -description = "A modular C++23 package" |
67 | | -license = "Apache-2.0" |
68 | | -``` |
69 | | - |
70 | | -`src/main.cpp` 会自动推断为 binary target,`tests/test_smoke.cpp` 会由 |
71 | | -`mcpp test` 自动发现;无需手写 `[targets.hello]`。 |
72 | | - |
73 | | -## mcpp.toml 配置 |
74 | | - |
75 | | -### 传统头文件库的模块化封装 |
76 | | - |
77 | | -`mcpplibs.mwrap.<库名>` 为传统 C/C++ 库提供模块入口;API namespace 保留上游,例如 |
78 | | -`import mcpplibs.mwrap.opencv.core;` 后仍使用 `cv::Mat`。包版本对齐上游,封装修订最多追加一个数字尾号。 |
79 | | -具体已发布的版本/feature 先查索引,不把文档中的候选库当已发布包。 |
80 | | - |
81 | | -每个新建或重构的 -m 库提供包自带快捷模板: |
82 | | - |
83 | | -```bash |
84 | | -mcpp new vision --template mcpplibs.mwrap.opencv@5.0.0.1 |
85 | | -mcpp new --list-templates mcpplibs.mwrap.opencv@5.0.0.1 |
86 | | -``` |
87 | | - |
88 | | -模板语法为 `[namespace.]name[@version][:template]`,只有一个默认模板时可以省略模板名。 |
89 | | -包模板中的 `{{self.name}}` 是完整包选择器,应用 `"{{self.name}}"` 作为单个依赖 key, |
90 | | -不要放到另一层 namespace 表下。模板必须实际创建、构建、运行,而不只做字符串检查。 |
91 | | - |
92 | | -feature 是加法并集;消费者可在依赖声明中使用 `default-features = false` 关闭该边的默认集合, |
93 | | -显式 features 仍会启用,其他依赖边的请求不会被取消。根项目没有 `--no-default-features` 命令行选项。 |
94 | | -可选大依赖由包作者显式门控。模块可导入不等于实际后端已启用,窗口/推理/设备能力需分别验证。 |
95 | | -宏不能经 import 传递;`.macros` 只提供可保持语义的常量/函数映射,原始宏仍需兼容头。 |
96 | | - |
97 | | -维护规范与包装流程见 [mcpp-index mwrap 规范](https://github.com/mcpplibs/mcpp-index/blob/main/docs/mwrap.md)。 |
98 | | -修改普通消费者项目时按现有包契约使用,不自动替用户迁移其他依赖。 |
99 | | - |
100 | | -```toml |
101 | | -[package] |
102 | | -name = "myapp" |
103 | | -version = "0.1.0" |
104 | | - |
105 | | -[targets.myapp] |
106 | | -kind = "bin" # bin / lib / shared; tests are discovered from tests/**/*.cpp |
107 | | -main = "src/main.cpp" |
108 | | - |
109 | | -[dev-dependencies] |
110 | | -gtest = "1.15.2" # 仅测试使用;SemVer: ^, ~, 范围, 精确 |
111 | | - |
112 | | -[toolchain] |
113 | | -default = "gcc@16.1.0" |
114 | | -``` |
115 | | - |
116 | | -更多配置:https://github.com/mcpp-community/mcpp/blob/main/docs/04-mcpp-toml.md |
117 | | - |
118 | | -## 工具链 |
119 | | - |
120 | | -```bash |
121 | | -mcpp toolchain list # 查看已装 |
122 | | -mcpp toolchain install gcc 16 # 装 GCC 16 |
123 | | -mcpp toolchain install llvm 20 # 装 LLVM 20 |
124 | | -mcpp toolchain default gcc@16.1.0 # 设默认 |
125 | | -mcpp build --target x86_64-linux-musl # 需要全静态 Linux 产物时显式选择 |
126 | | -``` |
127 | | - |
128 | | -## 测试结果与按用例运行 |
129 | | - |
130 | | -`mcpp test --message-format json` 每个测试一条 NDJSON 记录(通过的也带 `run_output`),最后是每个成员的 |
131 | | -`summary`。`status` 取 `pass`、`compile_fail`、`run_fail`(`timed_out` 为真即超时)、`not_run`(带 `reason`)、 |
132 | | -`built`(`--no-run`);退出码 0 为全部通过,1 为有测试失败,2 为有测试未能运行。 |
133 | | - |
134 | | -mcpp 的单位是测试程序;用例属于测试框架。按用例运行时,把框架的参数放在 `--` 之后: |
135 | | - |
136 | | -```bash |
137 | | -mcpp test unit/math -- --gtest_filter='Math.Adds:Math.Spans' # GoogleTest 只跑这两个用例 |
138 | | -mcpp test unit/math -- --gtest_output=json:"$PWD/math-report.json" # 每个用例的结果写成 JSON 报告 |
139 | | -mcpp test unit/math --print-launch -- --gtest_filter='Math.Adds' # 只构建,打印启动方式(argv、cwd、环境),供调试器使用 |
140 | | -``` |
141 | | - |
142 | | -读取用例结果时以框架报告为准(GoogleTest 的 `--gtest_output=json`、Catch2 的 `--reporter json`),程序输出中 |
143 | | -`[ RUN ] X` 与 `[ OK ] X` / `[ FAILED ] X` 之间是用例 X 的输出。 |
144 | | - |
145 | | -## 工作空间 |
146 | | - |
147 | | -```toml |
148 | | -[workspace] |
149 | | -members = ["libs/*", "apps/*"] |
150 | | - |
151 | | -[workspace.dependencies] |
152 | | -gtest = "1.15.2" |
153 | 12 | ``` |
154 | | - |
155 | | -```bash |
156 | | -mcpp build -p member-name # 构建指定成员 |
| 13 | +mcpp --agent-skill-doc # the root sheet and an index of the commands' sheets |
| 14 | +mcpp <command> --agent-skill-doc # one command's sheet |
157 | 15 | ``` |
158 | 16 |
|
159 | | -## 自定义索引 |
160 | | - |
161 | | -```toml |
162 | | -[indices] |
163 | | -my-index = "git@gitlab.example.com:team/index.git" |
164 | | -local = { path = "/path/to/index" } |
165 | | - |
166 | | -[dependencies.my-index] |
167 | | -internal-lib = "1.0.0" |
168 | | -``` |
169 | | - |
170 | | -## 常见问题 |
171 | | - |
172 | | -| 问题 | 解决 | |
173 | | -|---|---| |
174 | | -| 首次构建慢 | 正常,需下载工具链。后续使用缓存 | |
175 | | -| command not found | 重开终端。Unix release 安装脚本应确认 `~/.mcpp/bin` 在当前 shell 的 `PATH` 中;经 xlings 安装则确认 xlings 当前激活的 bin 目录。Windows 不要执行 `source`,重开 PowerShell 后用 `Get-Command mcpp.exe` 验证命令已激活。 | |
176 | | -| 编译错误 | `mcpp clean && mcpp build`,确认 `mcpp toolchain list` | |
177 | | -| 依赖找不到 | `mcpp index update`,确认 `mcpp search <name>` | |
178 | | -| 需要无网络构建 | 使用 `mcpp --offline` 或设置 `MCPP_OFFLINE=1`;缺失的工具链/依赖会直接报错 | |
179 | | - |
180 | | -## 问题反馈 |
181 | | - |
182 | | -1. **项目 Issue**:https://github.com/mcpp-community/mcpp/issues |
183 | | - - 描述复现步骤、期望/实际行为、mcpp 版本和 OS |
184 | | - - 可用 `gh issue create` 提交 |
185 | | - |
186 | | -2. **社区论坛**:https://forum.d2learn.org/category/20 |
187 | | - - 使用疑问和讨论 |
188 | | - |
189 | | -如果 AI Agent 无法直接创建 Issue,请提示用户手动创建并提供整理好的问题描述。 |
| 17 | +Install mcpp with `xlings install mcpp -y`, or on Linux and macOS with |
| 18 | +`curl -fsSL https://github.com/mcpp-community/mcpp/releases/latest/download/install.sh | bash`. |
| 19 | +The user documentation is [`docs/`](../../../docs/README.md). |
0 commit comments