ITADN
mcpp-community/mcpp · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

mcpp

一款现代 C++ 模块优先的构建工具 —— 使用纯 C++23 模块编写,完全自托管

English | 简体中文

Release C++23 Module License

文档 · 快速入门 · mcpp.toml 指南 · 示例 · 工具链
软件包索引 mcpp-index · 模块库 mcpplibs · 社区论坛 · 问题 · 发布版本
ci-linux ci-macos ci-windows

mcpp demo

亮点

  • 原生 C++23 模块支持import std 自动处理,文件级增量构建,自动模块依赖分析,零手动配置
  • 纯模块化自举 — mcpp 本身由 43+ 个 C++23 模块组成并构建自身;模块流水线经过实战检验
  • 开箱即用 — 单命令安装,捆绑的 GCC 16 / LLVM 20 工具链下载至隔离沙箱,绝不污染您的系统
  • 集成依赖管理 — SemVer 约束解析,锁文件,跨项目 BMI 缓存,自定义软件包索引
  • 多软件包工作区 — 为大型项目提供统一的锁文件和版本管理

为什么选择 mcpp

mcpp 专为 C++23 模块优先开发 而构建。如果您希望在项目中使用 import std、模块接口单元(.cppm)、模块分区以及其他现代 C++ 特性,mcpp 在 Linux、macOS ARM64 和 Windows x86_64 上为您提供流畅、友好的体验:

  • 默认模块化 — 由 mcpp new 创建的项目直接使用 C++23 模块;import std 开箱即用
  • 文件级增量构建 — 基于 P1689 dyndep 的三层优化(前端脏检查 + 逐文件扫描 + BMI 重新统计);仅重新编译实际发生变化的模块
  • 一键创建与构建mcpp new hello && cd hello && mcpp build;工具链自动安装,无需配置编译器或构建系统
  • 模块化生态系统mcpplibs 提供一套不断增长的、可直接 import 的 C++ 模块库,并支持自定义软件包索引

[!NOTE] 早期阶段项目 — mcpp 正处于积极开发中;接口和行为可能会在未来版本中发生变化。 欢迎对现代 C++ 模块优先构建工具感兴趣的开发者 contribute。 问题 / 反馈 / 建议 — 请在 issues 中留言。

快速入门

安装

通过 xlings 安装(推荐)

xlings install mcpp -y
还没有 xlings?点击获取安装命令

Linux / macOS

curl -fsSL https://d2learn.org/xlings-install.sh | bash

Windows — PowerShell

irm https://d2learn.org/xlings-install.ps1.txt | iex

关于 xlings 的更多信息 → xlings.d2learn.org

可选 — 简短命令(mpmbuildmrun、……)
xlings install mcpp-short-cmd -y

Registers 30 shims,因此 mcpp build 变为 mbuild。命名规则:除最后一个词外,每个词取首字母,最后一个词保留完整形式 — mcpp self doctormsdoctormp 是裸的 mcpp。它们别名指向 mcpp shim,而非固定的 二进制文件,因此 xlings use mcpp <ver> 也会切换它们。

ShortExpands toShortExpands to
mpmcppmexpkgmcpp emit xpkg
mnewmcpp newmxparsemcpp xpkg parse
mbuildmcpp buildmtinstallmcpp toolchain install
mrunmcpp runmtlistmcpp toolchain list
mtestmcpp testmtdefaultmcpp toolchain default
mcleanmcpp cleanmcdirmcpp cache dir
maddmcpp addmclistmcpp cache list
mremovemcpp removemcinfomcpp cache info
mupdatemcpp updatemcgcmcpp cache gc
msearchmcpp searchmilistmcpp index list
mpublishmcpp publishmiaddmcpp index add
mpackmcpp packmiremovemcpp index remove
msdoctormcpp self doctormiupdatemcpp index update
msenvmcpp self envmsconfigmcpp self config
msversionmcpp self versionmsexplainmcpp self explain

其他选项

选项 1 — 单行安装程序(Linux x86_64/aarch64, macOS ARM64)
curl -fsSL https://github.com/mcpp-community/mcpp/releases/latest/download/install.sh | bash

此安装程序不支持 Windows;请使用上述 PowerShell xlings 路径。 它安装到 ~/.mcpp/ 并将其添加到您的 shell PATH 中。删除 ~/.mcpp 即可干净地卸载。

选项 2 — Homebrew (macOS / Linux)
brew install mcpp-community/mcpp/mcpp-m

一条命令 — 它获取 mcpp-community/homebrew-mcpp 并安装相同的预构建发布二进制文件。macOS 需要 Apple 芯片 + macOS 14;每个用户的数据位于 ~/.mcpp/

Homebrew 的 mcpp 是一个无关的 C 预处理器,因此 mcpp-m 公式 名称 — 它安装的命令仍然是 mcpp

选项 3 — Arch Linux (AUR)
yay -S mcpp-bin      # prebuilt release binary
yay -S mcpp-m        # or build from source (bootstrapped with mcpp-bin)

在系统范围内安装 mcpp 命令;用户级数据仍位于 ~/.mcpp/。 在 Arch 上,名称 mcpp 是一个无关的 C 预处理器,因此软件包为 mcpp-bin / mcpp-m(参见 scripts/aur/)。

Option 4 — 让 AI 助手为您安装

将以下提示词复制给您的 AI 编程助手(Claude Code / Cursor / Copilot 等):

Read the README of https://github.com/mcpp-community/mcpp,
then install mcpp for me and create a C++23 module project, build and run it.
The repo's .agents/skills/mcpp-usage/SKILL.md has a detailed usage guide.

创建、构建并运行项目

mcpp new hello
cd hello
mcpp build
mcpp run

注意:首次构建会初始化环境并获取工具链,这可能需要一些时间。

项目布局

hello/
├── mcpp.toml             ← project manifest
├── src/
│   └── main.cpp          ← import std; works directly
└── tests/
    └── test_smoke.cpp    ← discovered by `mcpp test`
# mcpp.toml
[package]
name        = "hello"
version     = "0.1.0"
description = "A modular C++23 package"
license     = "Apache-2.0"

内置脚手架依赖约定:它不会写入 [targets.hello]src/main.cpp 推断二进制目标,并且 mcpp test 自动发现 tests/test_smoke.cpp

使用模块库

mcpp.toml 中添加两行依赖,以从 mcpplibs 引入社区模块库:

[dependencies]
cmdline = "0.0.2"

然后直接在代码中 import

import mcpplibs.cmdline;

有关更多依赖选项(版本约束、命名空间、Git 引用、本地路径等),请参阅 mcpp.toml 指南 — 依赖管理

功能概览

构建系统
  • 原生 C++20/23/26 模块支持(接口单元、实现单元、模块分区),以及 c++latest / c++fly 实验模式
  • import std / import std.compat 的完全自动预编译和缓存
  • 三层增量优化:前端脏检查 + 每文件 P1689 dyndep + BMI copy-if-different restat
  • 带指纹的 BMI 缓存:按编译器/标志/标准库进行哈希,跨项目共享
  • Ninja 后端:自动生成 build.ninja,并行编译
  • 自动生成 compile_commands.json(可直接用于 clangd / ccls)
  • 一等 C 语言支持:自动检测 .c 文件,混合 C/C++ 项目
  • 用户自定义 cflags / cxxflags / ldflags / c_standard
工具链管理
  • 捆绑 GCC 16.1.0 + LLVM/Clang 20.1.7,一条命令安装
  • 主机感知默认值:Linux x86_64 上使用原生 glibc GCC,其他 Linux 架构上使用 musl GCC,macOS 上使用 LLVM,在具有可用 MSVC 的 Windows 上使用 LLVM,在裸 Windows 上使用 MinGW-w64 GCC
  • 多版本并存:mcpp toolchain install gcc 16 / mcpp toolchain install llvm 20
  • 隔离沙箱:所有工具链均位于 ~/.mcpp/registry/,不触及系统
  • 按平台选择:linux = "gcc@16"macos = "llvm@20"
  • GCC 和 Clang 编译流水线对等(由 BmiTraits 抽象层驱动)
包与依赖管理
  • SemVer 约束解析:^~、范围、精确版本
  • 三阶段解析:约束合并 → 多版本混淆回退 → 精确匹配
  • 锁文件 mcpp.lock(v2 格式:索引快照 + 命名空间)
  • 命名空间系统:[dependencies.myteam] foo = "1.0"
  • 自定义包索引:[indices] acme = "git@..." / { path = "..." }
  • 项目级索引隔离(.mcpp/ 目录,无全局污染)
  • 依赖来源:索引 / Git / 本地路径
工作区
  • [workspace] members = ["libs/*", "apps/*"]
  • 统一锁文件 + 统一目标目录
  • 集中式版本管理:[workspace.dependencies] + .workspace = true
  • 选择性构建:mcpp build -p member-name
  • 配置继承:工具链、构建标志和索引从根目录级联到成员
打包与发布
  • mcpp pack:四种 Linux 发布模式 —— system / vendored(默认)/ self-contained / static;bundle-projectbundle-all 仍为兼容性别名
  • 完全静态的 musl 二进制文件:单文件分发,无 glibc 依赖(匹配 Linux x86_64 或 aarch64 目标)
  • mcpp publish:生成 xpkg.lua 并发布到软件包索引
  • 通过 patchelf 自动修复 RPATH(Linux)
开发者体验
  • mcpp new — 创建模块化项目;--template <pkg>[@ver][:<tmpl>] 使用库提供的模板(例如 --template imgui);--list-templates <pkg> 列出它们
  • mcpp run [-- args] — 构建并运行
  • mcpp test [pattern] [-- args] — 自动发现并运行测试(按名称过滤;--list--timeout <s>--message-format json
  • mcpp search — 搜索软件包索引
  • mcpp add / remove / update — 依赖管理
  • mcpp why [toolchain|runtime|deps] — 解释已解析的构建决策
  • mcpp --offline / MCPP_OFFLINE=1 — 仅使用已可用的本地状态
  • mcpp explain E0001 — 详细的错误代码解释
  • mcpp self doctor — 环境自诊断

平台支持

mcpp 的身份模型有两个正交维度:工具链family@version(family ∈ gcc | llvm | msvc),目标是一个三元组 arch-os[-env]。交叉编译仅仅是 mcpp build --target <triple> —— 正确的工具链负载会被自动解析并安装。 mcpp toolchain list 显示您机器上的实时状态。

主机(mcpp 本身运行的环境):Linux x86_64 / aarch64,macOS arm64,Windows x86_64。

目标--target 接受的对象;此表镜像了代码中的词汇表):

目标约定工具链状态
x86_64-linux-gnugcc (Linux 默认) 或 llvm
x86_64-linux-muslgcc 16,完全静态
aarch64-linux-muslgcc 16,完全静态 —— 从 x86_64 交叉编译(qemu 验证)或原生
x86_64-windows-gnugcc 16 MinGW-w64 —— Windows 上原生,从 Linux 交叉编译(wine 验证)(无 Visual Studio 时的 Windows 默认)
x86_64-windows-msvcmsvc@system(检测到的 VS/BuildTools)或 llvm ¹ (有 Visual Studio 时的 Windows 默认)
aarch64-macosllvm (macOS 默认)
riscv64-linux-musl🔄
aarch64-linux-gnu🔄
x86_64-macos🔄

✅ 已验证 —— CI 构建并执行了端到端的产物(包括 qemu/wine) | 🔄 计划中

Linux 发布二进制文件是 x86_64 和 aarch64 的完全静态 musl 构建 (x86_64-linux-muslaarch64-linux-musl)。 旧拼写 — x86_64-w64-mingw32gcc@16.1.0-muslmingw-cross@…musl-gcc@… — 将永久作为别名接受,并规范化为 上述标准形式。

¹ 在 Windows 上,llvm 针对 MSVC ABI,因此需要一个现有的 MSVC BuildTools 或 Visual Studio(UCRT、Windows SDK、MSVC STL)。你无 需自行配置:首次运行时 mcpp 会检查可用的 MSVC, 若未找到,则默认使用 x86_64-windows-gnu(winlibs MinGW-w64)—— 完全 自包含,无需 Visual Studio,包含 import std。无需安装 或配置;mcpp new && mcpp build 在标准 Windows 系统上即可正常工作。 mcpp.toml 中显式指定的 [toolchain] 始终按原样尊重 — mcpp 会修改其自身默认值,而不会修改你的设置。

文档

任何命令的完整选项均可通过 mcpp <cmd> --help 获取。

AI 辅助学习:将以下提示发送给 AI 编程助手,即可快速上手 mcpp:

Read .agents/skills/mcpp-usage/SKILL.md and the docs/ directory of the
https://github.com/mcpp-community/mcpp repository,
then tell me how to create a C++23 module project with dependencies using mcpp.

谁在使用 mcpp

使用 mcpp 构建的真实项目 — 可import的 C++23 模块及其所依赖的工具链:

项目描述
mcppmcpp 本身 — 43+ 个 C++23 模块,完全自托管
xlingsmcpp 所依赖的工具链与包管理基础
tinyhttps支持 SSE 流式传输的极简 C++23 HTTP/HTTPS 客户端
llmapi现代 C++ LLM API 客户端(兼容 OpenAI)
imgui-m作为 C++23 模块包提供的 Dear ImGui
cmdline命令行解析库/框架(mcpp 使用它)

更多模块化库 → mcpplibs · 包索引 → mcpp-index

贡献

欢迎通过 issue 和 PR 进行贡献。该项目接受由 AI 代理开发的贡献。

基本工作流

  1. 打开一个 issue — 对于 bug 修复、新功能或改进,请先在 issues 中发起讨论
  2. 实现更改 — fork 仓库,创建分支,并根据范围进行验证(行为变更需mcpp build及相关测试;仅文档变更需提供示例和链接)
  3. 提交 PR — 使用 gh pr create 并确保 CI 通过
  4. CI 必须通过 — CI 未通过的 PR 不会被合并

提交信息约定feat: / fix: / test: / docs: / refactor: 前缀

AI 代理贡献:仓库中的 .agents/skills/mcpp-contributing/SKILL.md 提供了完整的代理贡献工作流和项目结构指南。只需将此提示发送给你的 AI 助手:

Read .agents/skills/mcpp-contributing/SKILL.md of the
https://github.com/mcpp-community/mcpp repository,
then follow the guide to help me submit a contribution to mcpp.

社区与生态系统

致谢

依赖项与灵感来源: