mcpField
C
carbon-lang

carbon-lang

github.com/mohitmishra786开发269265 次安装18 周前更新
代码仓库

关于 carbon-lang

面向评估和试用 Carbon 的开发者的语言指南,涵盖基础语法、与 C++ 的双向互操作、工具链构建及实验性内存安全模型,并说明其尚未达到 1.0 的限制。

主要功能

  1. 01学习 Carbon 基础语法
  2. 02通过 Carbon.h 与 C++ 互操作
  3. 03从源码构建 carbon-toolchain
  4. 04了解 checked borrows
  5. 05评估预 1.0 阶段的限制

适用场景

  1. 01为 C++ 项目评估渐进迁移
  2. 02从 Carbon 调用 C++ 库
  3. 03试验 Carbon 工具链
  4. 04判断 Carbon 的生产就绪度

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill carbon-lang

安装后在 Claude Code 中输入/carbon-lang确认已加载。

详细介绍

Carbon

用途

引导 Agent 了解 Carbon 编程语言:语法基础、通过 Carbon.h 实现双向 C++ 互操作、使用 carbon-toolchain 从源代码构建、实验性的内存安全模型(受检借用)、当前 1.0 之前的限制,以及何时评估 Carbon、何时继续使用 C++。

适用场景

  • 为有大量 C++ 依赖的新项目评估 Carbon
  • 从 Carbon 调用 C++ 库,或将 Carbon 暴露给 C++
  • 从 GitHub 源代码试用 Carbon 工具链
  • 了解从 C++ 代码库迁移到 Carbon 的路径
  • 评估其是否已适合用于生产环境(目前仍处于实验阶段)
  • 比较 Carbon 与现代 C++(C++20/23)的使用体验

工作流

1. 了解项目状态
Carbon (2026 state)
├── Experimental — no 1.0 release
├── Focus: C++ interoperability and migration
├── No production stdlib equivalent to C++ yet
└── Toolchain under active development

将 Carbon 用于探索和迁移原型开发;除非团队接受其不稳定性,否则不要用于生产系统。

2. 构建 carbon-toolchain
git clone https://github.com/carbon-language/carbon-lang
cd carbon-lang

# Prerequisites: LLVM, Clang, CMake, Ninja
./scripts/run_bazelisk.py build \
  //toolchain:install \
  --symlink_prefix=carbon/

# Add to PATH
export PATH="$PWD/carbon/bin:$PATH"

carbon --version
# Online explorer (Compiler Explorer instance)
# http://carbon.compiler-explorer.com/
3. 基本语法
package Sample api;

fn Add(a: i32, b: i32) -> i32 {
    return a + b;
}

fn Main() -> i32 {
    var x: i32 = 3;
    var y: i32 = 4;
    Print(ToString(Add(x, y)));
    return 0;
}
CarbonC++ 等价写法
fn函数
var x: i32int32_t x
class / interfaceclass / 纯虚类
impl方法定义
package命名空间/模块
4. C++ 互操作——从 Carbon 调用 C++
// math.h (C++ header)
#pragma once
namespace Math {
    int Add(int a, int b);
}
// Carbon imports C++ via library directive
package MathInterop api;

import Math;

fn Main() -> i32 {
    var sum: i32 = Math.Add(3, 4);
    return sum;
}

互操作会使用 Clang 解析 C++ 头文件并生成 Carbon 绑定。

5. Carbon.h——将 Carbon 暴露给 C++
// Exported for C++ consumption
package MyLib api;

export fn PublicApi() -> i32 {
    return 42;
}
// C++ side includes generated Carbon headers
#include "carbon/generated/MyLib.h"

int main() {
    return MyLib::PublicApi();
}

构建系统通过 Carbon 工具链驱动程序将 Carbon 目标文件与 C++ 链接。

6. 内存安全——受检借用(实验性)
// Conceptual — API evolving
fn Process(data: i32*) -> i32 {
    // Checked borrow: compiler tracks lifetime
    var ref: i32* = data;
    return *ref;
}

Carbon 的目标是在不使用垃圾回收的情况下实现内存安全——采用经过检查的指针和所有权语义。功能成熟度各不相同;请查看最新的设计文档。

7. 当前限制
  • 没有与 C++ <vector>、<thread> 等相当的稳定标准库
  • 工具链 API 会随版本更新而变化
  • 与 C++/Rust 相比,IDE 支持有限
  • 交叉编译方案尚不成熟
  • 社区和软件包生态较小
8. Carbon 与 C++ 的选择流程
Choose Carbon exploration when
├── Large C++ codebase to gradually migrate
├── Team wants improved syntax with C++ interop
└── Can tolerate toolchain churn

Stay on C++ when
├── Production stability required now
├── Need full stdlib, Boost, mature tooling
├── Heavy template metaprogramming investment
└── Platform support beyond Carbon targets
9. 构建 Carbon 文件
# Compile and link (toolchain evolving — check docs)
carbon compile sample.carbon --output=sample.o
carbon link sample.o --output=sample

# Typecheck only
carbon compile --phase=check sample.carbon

# Or build from source with Bazel in carbon-lang repo
./scripts/run_bazelisk.py build //toolchain:install --symlink_prefix=carbon/

常见问题

症状原因解决方法
构建失败,提示缺少 LLVMLLVM 版本不正确使用与 carbon-lang README 中指定版本相符的 LLVM
C++ 导入错误不支持相关 C++ 特性简化头文件;用 C API 封装
语法与教程中的不同1.0 之前变动频繁查看最新的 carbon-lang 文档
任务没有可用的标准库尚未实现暂时桥接到 C++ 库
Explorer 与 CLI 的表现不同版本不同两者使用同一提交
与 C++ 链接时报错ABI 不匹配使用 Carbon 工具链链接驱动程序

相关技能

  • skills/compilers/cpp-templates — 正在迁移的 C++
  • skills/compilers/cpp-modules — 与 C++20 模块的比较
  • skills/compiler-internals/compiler-frontend — Carbon 编译器架构
  • skills/languages/hare-lang — 可选的系统编程语言
  • skills/compilers/clang — Carbon 的 Clang 后端
  • skills/compilers/llvm — LLVM IR 流程

评论

支持 Markdown