4 releases (breaking)
|0.15.0||Oct 21, 2020|
|0.14.0||Dec 19, 2019|
|0.11.0||Aug 14, 2019|
|0.10.0||Apr 15, 2019|
#783 in Procedural macros
38 downloads per month
Used in c2rust-refactor
C2Rust helps you migrate C99-compliant code to Rust.
The translator (or transpiler),
produces unsafe Rust code that closely mirrors the input C code.
The primary goal of the translator is to preserve functionality;
test suites should continue to pass after translation.
Generating safe and idiomatic Rust code from C ultimately requires manual effort.
We are currently working on analysis to automate some of the effort
required to lift unsafe Rust into safe Rust types.
This work is still in the early stages; please get in touch if you're interested!
We previously maintained a scriptable refactoring tool,
that reduces the tedium of refactoring, but this tool is now deprecated
so that we can move forward with a recent Rust toolchain.
Here's the big picture:
To learn more about using and developing C2Rust, check out the manual. The manual is still a work-in-progress, so if you can't find something please let us know.
C2Rust requires LLVM 7 or later with its corresponding clang compiler and libraries. Python 3.6 or later, CMake 3.4.3 or later, and openssl (1.0) are also required. These prerequisites may be installed with the following commands, depending on your platform:
Ubuntu 18.04, Debian 10, and later:
apt install build-essential llvm clang libclang-dev cmake libssl-dev pkg-config python3
Depending on the LLVM distribution, the
llvm-dev package may also be required.
For example, the official LLVM packages from apt.llvm.org require
llvm-dev to be installed.
pacman -S base-devel llvm clang cmake openssl python
NixOS / nix:
OS X: XCode command-line tools and recent LLVM (we recommend the Homebrew version) are required.
xcode-select --install brew install llvm python3 cmake openssl
The C2Rust transpiler now builds using a stable Rust compiler. If you are developing other features, you may need to install the correct nightly compiler version.
Installing from crates.io
cargo install c2rust
You can also set the LLVM version explicitly if you have multiple installed, like this, for example:
LLVM_CONFIG_PATH=llvm-config-14 cargo install c2rust
On OS X with Homebrew LLVM, you need to point the build system at the LLVM installation as follows:
LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config cargo install c2rust
On Linux with Linuxbrew LLVM, you need to point the build system at the LLVM installation as follows:
LLVM_CONFIG_PATH=/home/linuxbrew/.linuxbrew/opt/llvm/bin/llvm-config cargo install c2rust
LLVM_CONFIG_PATH accordingly if Linuxbrew was installed to your home directory.
On Gentoo, you need to point the build system to
the location of
llvm-config as follows:
LLVM_CONFIG_PATH=/path/to/llvm-config LIBCLANG_PATH=/path/to/libclang.so cargo install c2rust
If you have trouble with building and installing, or want to build from the latest master, the developer docs provide more details on the build system.
Installing from Git
If you'd like to check our recently developed features or you urgently require a bugfixed version of
you can install it directly from Git:
cargo install --git https://github.com/immunant/c2rust.git c2rust
Please note that the master branch is under constant development and you may experience issues or crashes.
You should also set
LLVM_CONFIG_PATH accordingly if required as described above.
Translating C to Rust
To translate C files specified in
compile_commands.json (see below),
c2rust tool with the
c2rust transpile compile_commands.json
c2rust refactor tool was also available for refactoring Rust code, see refactoring, but is now being replaced by a more robust way to refactor.)
The translator requires the exact compiler commands used to build the C code.
This information is provided via a compilation database file named
(Read more about compilation databases here).
Many build systems can automatically generate this file;
we show a few examples below.
Once you have a
compile_commands.json file describing the C build,
translate the C code to Rust with the following command:
c2rust transpile path/to/compile_commands.json
To generate a
Cargo.toml template for a Rust library, add the
c2rust transpile --emit-build-files path/to/compile_commands.json
To generate a
Cargo.toml template for a Rust binary, do this:
c2rust transpile --binary myprog path/to/compile_commands.json
--binary myprog tells the transpiler to use
main function from
myprog.rs as the entry point for a binary.
The translated Rust files will not depend directly on each other like normal Rust modules. They will export and import functions through the C API. These modules can be compiled together into a single static Rust library or binary.
There are several known limitations in this translator. The translator will emit a warning and attempt to skip function definitions that cannot be translated.
compile_commands.json file can be automatically created
It may be a good idea to remove optimizations (
-OX) from the compilation database,
as there are optimization builtins which we do not support translating.
When creating the initial build directory with
This only works on projects configured to be built by
This works on Linux and MacOS.
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ...
intercept-build (part of the scan-build tool)
is recommended for non-
intercept-build is bundled with
but a standalone version can be easily installed via
pip install scan-build
intercept-build <build command>
You can also use
intercept-build to generate a compilation database
for compiling a single C file. For example:
intercept-build sh -c "cc program.c"
bear (Linux only)
If you have
it can be used similarly to
bear <build command>
To report issues with translation or refactoring, please use our Issue Tracker.
I translated code on platform X, but it didn't work correctly on platform Y.
We run the C preprocessor before translation to Rust. This specializes the code to the host platform. For this reason, we do not support cross compiling translated code at the moment.
What platforms can C2Rust be run on?
The translator and refactoring tool support both macOS and Linux. Other features, such as cross checking the functionality between C and Rust code, are currently limited to Linux hosts.
Acknowledgements and Licensing
This material is available under the BSD-3 style license as found in the LICENSE file.
This material is based upon work supported by the United States Air Force and DARPA under Contract No. FA8750-15-C-0124. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the United States Air Force and DARPA. Distribution Statement A, "Approved for Public Release, Distribution Unlimited."