```Cpp2Rust:自动将 C++ 转换为安全 Rust```
Cpp2Rust: Translates C++ to safe Rust automatically

原始链接: https://github.com/Cpp2Rust/cpp2rust

**Cpp2Rust** 是一款自动化的语法驱动型工具,可将 C++ 代码转换为安全的 Rust 代码,具体细节详见 PLDI 2026 的论文。该工具通过将 C++ 源代码解析为 Clang AST(抽象语法树),并将其转换为 Rust 代码来运行。 为在保留 C 语言风格指针语义(如指针算术和别名)的同时确保安全性,生成的代码会使用 `libcc2rs` 运行时库,将指针映射为与 Rust 借用检查器兼容的 `Ptr` 类型。虽然默认输出是使用引用计数模型的全安全 Rust 代码,但该工具也提供 `--model=unsafe` 标志,以便进行调试和性能优化。 该工具可集成到标准构建工作流中,支持通过 `compile_commands.json` 对单个文件或整个项目进行转换。使用前提是已安装 Clang 和 CMake,构建过程采用 Ninja 以实现高效的编译与测试。通过这种自动化方法,Cpp2Rust 简化了遗留 C++ 代码库向 Rust 内存安全保障机制的迁移。

抱歉。
相关文章

原文

Cpp2Rust translates C++ to fully safe Rust automatically. It is a syntax-driven translator based on clang's AST.

Cpp2Rust's algorithm is described in the paper Cpp2Rust: Automatic Translation of C++ to Safe Rust published at PLDI 2026.

Cpp2Rust first parses the input C++ file(s) with clang and produces an AST. It then traverses the AST and emits Rust code as strings, inserting calls to the libcc2rs runtime library where needed (e.g., for raw pointer semantics). Finally, the Rust code is pretty-printed using rustfmt to a single .rs file.

By default the reference counting model is used, which produces fully safe Rust. A generator of unsafe Rust is also available through the --model=unsafe command line argument for debugging and performance comparisons.

Runtime library (libcc2rs)

The generated code relies on a runtime library designed to simplify the translation process. C pointers are converted into the Ptr<T> type provided by libcc2rs. Ptr<T> models C pointer semantics, including null, arithmetic, and aliasing, while satisfying Rust's borrow checker through checked run-time operations.

On Ubuntu, install the required dependencies with:

sudo apt install libclang-22-dev clang++-22 ninja-build cmake
curl -LsSf https://astral.sh/ruff/install.sh | sh
mkdir build
cd build
cmake -GNinja ..
ninja
ninja check
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs

By default, the reference counting model is used (fully safe output). To generate unsafe Rust instead:

./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs --model=unsafe

Minimal example. Given hello.cpp:

#include <cstdio>
int main() {
  printf("hello world\n");
  return 0;
}

Running ./build/cpp2rust/cpp2rust --file=hello.cpp -o=hello.rs produces:

pub fn main() {
    std::process::exit(main_0());
}
fn main_0() -> i32 {
    println!("hello world");
    return 0;
}

Compile and run with:

rustc hello.rs -L ../libcc2rs/target/debug
./hello

Translate a whole program

First generate a compile_commands.json for your project. With CMake this is one extra flag:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..

Then run:

./build/cpp2rust/cpp2rust --dir=<dir> -o <output>.rs

<dir> must be the directory that contains compile_commands.json.

# Run all tests
ninja check

# Run only the unit tests
ninja check-unit

# Run libcc2rs unit tests
ninja check-libcc2rs

# Run libcc2rs-macros unit tests
ninja check-libcc2rs-macros

# Regenerate expected output for unit tests after intentional changes
REPLACE_EXPECTED=1 ninja check-unit
联系我们 contact @ memedata.com