Cpp2Rust/cpp2rust
소개
Cpp2Rust: Automatic Translation of C++ to Safe Rust
프로그래밍 언어 및 런타임측정된 스타 증가
- in 24h
- 0
- in 7 days
- -2
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalCpp2Rust
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.
The developer's manual describes how Cpp2Rust works internally and how to extend it.
Overview
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.
Requirements
On Ubuntu, install the required dependencies with:
sudo apt install libclang-23-dev clang++-23 ninja-build cmake
pip install ruff==0.15.22
Build
mkdir build
cd build
cmake -GNinja ..
ninja
ninja check
Run
Translate a single file
./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 {
printf!(b"hello world\n");
return 0;
}
Compile and run with:
rustc hello.rs -L build/libcc2rs-target/release
./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.
Test Suite
# 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
관련 프로젝트 살펴보기
불러오는 중…