{"id":13438250,"url":"https://github.com/immunant/c2rust","last_synced_at":"2026-02-11T11:12:57.586Z","repository":{"id":37502414,"uuid":"130285553","full_name":"immunant/c2rust","owner":"immunant","description":"Migrate C code to Rust","archived":false,"fork":false,"pushed_at":"2025-05-01T21:36:41.000Z","size":81790,"stargazers_count":4206,"open_issues_count":294,"forks_count":263,"subscribers_count":54,"default_branch":"master","last_synced_at":"2025-05-06T02:08:55.462Z","etag":null,"topics":["memory-safety","migration","rust","security-hardening","translation","transpiler"],"latest_commit_sha":null,"homepage":"https://c2rust.com/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/immunant.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-04-20T00:05:50.000Z","updated_at":"2025-05-05T06:11:42.000Z","dependencies_parsed_at":"2024-01-10T18:46:35.466Z","dependency_job_id":"8f55ace0-9b38-4ed7-8218-b4ba9300177b","html_url":"https://github.com/immunant/c2rust","commit_stats":{"total_commits":7151,"total_committers":69,"mean_commits":103.6376811594203,"dds":0.8204446930499231,"last_synced_commit":"1e6ce6f79bd18970ded7b44432a7d493888839c0"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/immunant%2Fc2rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/immunant%2Fc2rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/immunant%2Fc2rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/immunant%2Fc2rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/immunant","download_url":"https://codeload.github.com/immunant/c2rust/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252607037,"owners_count":21775415,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["memory-safety","migration","rust","security-hardening","translation","transpiler"],"created_at":"2024-07-31T03:01:03.993Z","updated_at":"2026-02-11T11:12:57.579Z","avatar_url":"https://github.com/immunant.png","language":"Rust","readme":"# C2Rust\n\n[![ci GitHub Actions Status]][github] [![c2rust-testsuite GitHub Actions Status]][github] [![Latest Version]][crates.io] [![Rustc Version]](#)\n\n[ci GitHub Actions Status]: https://github.com/immunant/c2rust/workflows/ci/badge.svg\n[c2rust-testsuite GitHub Actions Status]: https://github.com/immunant/c2rust/workflows/c2rust-testsuite/badge.svg\n[github]: https://github.com/immunant/c2rust/actions\n\n[Latest Version]: https://img.shields.io/crates/v/c2rust.svg\n[crates.io]: https://crates.io/crates/c2rust\n[rustc Version]: https://img.shields.io/badge/rustc-nightly--2022--08--08-lightgrey.svg \"rustc nightly-2022-08-08\"\n\n\u003c!-- ANCHOR: intro --\u003e\n\n## Intro\n\nC2Rust helps you migrate C99-compliant code to Rust.\nThe translator (or transpiler), [`c2rust transpile`](./c2rust-transpile/),\nproduces unsafe Rust code that closely mirrors the input C code.\nThe primary goal of the translator is to preserve functionality;\ntest suites should continue to pass after translation.\n\nGenerating safe and idiomatic Rust code from C ultimately requires manual effort.\nWe are currently working on analysis to automate some of the effort\nrequired to lift unsafe Rust into safe Rust types.\nHowever, we are building a [refactoring tool](c2rust-refactor) that reduces the tedium of doing so.\nThis work is still in the early stages; please get in touch if you're interested!\n\nYou can also [cross-check](cross-checks) the translated code against the original ([tutorial](docs/cross-check-tutorial.md)).\n\nHere's the big picture:\n\n![C2Rust overview](docs/c2rust-overview.png \"C2Rust overview\")\n\nTo learn more, check out our [RustConf'18](https://www.youtube.com/watch?v=WEsR0Vv7jhg) talk on YouTube\nand try the C2Rust translator online using the [Compiler Explorer](https://godbolt.org/z/GdEzYWGq4).\nThis uses the current `master` branch, updated every night.\n\n\u003c!-- ANCHOR_END: intro --\u003e\n\n## Documentation\n\nTo learn more about using and developing C2Rust, check out the [manual](https://c2rust.com/manual/).\nThe manual is still a work-in-progress, so if you can't find something please let us know.\n[c2rust.com/manual/](https://c2rust.com/manual/) also has not been updated since ~2019,\nso refer to the in-tree [./manual/](./manual/) for more up-to-date instructions.\n\n\u003c!-- ANCHOR: installation --\u003e\n\n## Installation\n\n### Prerequisites\n\nC2Rust requires LLVM 7 or later with its corresponding clang compiler and libraries.\nPython (through `uv`), CMake 3.5 or later and openssl (1.0) are also required.\nThese prerequisites may be installed with the following commands, depending on your platform:\n\nPython:\n\n```sh\ncurl -LsSf https://astral.sh/uv/install.sh | sh\nuv venv\nuv pip install -r scripts/requirements.txt\n```\n\n- **Ubuntu 18.04, Debian 10, and later:**\n\n    ```sh\n    apt install build-essential llvm clang libclang-dev cmake libssl-dev pkg-config git\n    ```\n\nDepending on the LLVM distribution, the `llvm-dev` package may also be required.\nFor example, the official LLVM packages from [apt.llvm.org](https://apt.llvm.org/) require `llvm-dev` to be installed.\n\n- **Arch Linux:**\n\n    ```sh\n    pacman -S base-devel llvm clang cmake openssl\n    ```\n\n- **NixOS / nix:**\n\n    ```sh\n    nix-shell\n    ```\n\n- **macOS:** Xcode command-line tools and recent LLVM (we recommend the Homebrew version) are required.\n\n    ```sh\n    xcode-select --install\n    brew install llvm cmake openssl\n    ```\n\nThe C2Rust transpiler now builds using a stable Rust compiler.\nIf you are developing other features,\nyou may need to install the correct nightly compiler version.\n\n### Installing from crates.io\n\n```sh\ncargo install --locked c2rust\n```\n\nYou can also set the LLVM version explicitly if you have multiple installed,\nlike this, for example:\n\n```sh\nLLVM_CONFIG_PATH=llvm-config-14 cargo install --locked c2rust\n```\n\nIf you're using LLVM from Homebrew (either on Apple Silicon, Intel Macs, or Linuxbrew),\nyou can run:\n\n```sh\nLLVM_CONFIG_PATH=\"$(brew --prefix)/opt/llvm/bin/llvm-config\" cargo install --locked c2rust\n```\n\nor for a specific LLVM version,\n\n```sh\nLLVM_CONFIG_PATH=\"$(brew --prefix)/opt/llvm@21/bin/llvm-config\" cargo install --locked c2rust\n```\n\nOn Gentoo, you need to point the build system to\nthe location of `libclang.so` and `llvm-config` as follows:\n\n```sh\nLLVM_CONFIG_PATH=/path/to/llvm-config LIBCLANG_PATH=/path/to/libclang.so cargo install --locked c2rust\n```\n\nIf you have trouble with building and installing, or want to build from the latest master,\nthe [developer docs](docs/README-developers.md#building-with-system-llvm-libraries)\nprovide more details on the build system.\n\n### Installing from Git\n\nIf you'd like to check our recently developed features or you urgently require a bugfixed version of `c2rust`,\nyou can install it directly from Git:\n\n```sh\ncargo install --locked --git https://github.com/immunant/c2rust.git c2rust\n```\n\nPlease note that the master branch is under constant development and you may experience issues or crashes.\n\nYou should also set `LLVM_CONFIG_PATH` accordingly if required as described above.\n\n### Nightly Tools\n\n`c2rust` and `c2rust-transpile` are installed by default and can be built on `stable` `rustc`.\nThe other tools, such as `c2rust-refactor`, use `rustc` internal APIs, however,\nand are thus pinned to a specific `rustc` `nightly` version: `nightly-2022-08-08`.\nThese are also not published to `crates.io`.\nTo install these, these can be installed with `cargo` with the pinned nightly.  For example,\n\n```sh\ncargo +nightly-2022-08-08 install --locked --git https://github.com/immunant/c2rust.git c2rust-refactor\n```\n\nHowever, we recommend installing them from a full checkout,\nas this will resolve the pinned nightly automatically:\n\n```sh\ngit clone https://github.com/immunant/c2rust.git\ncd c2rust\ncargo build --release\n```\n\nThese tools, like `c2rust-refactor`, can then also be invoked through `c2rust`\nas `c2rust refactor`, assuming they are installed in the same directory.\n\n\u003c!-- ANCHOR_END: installation --\u003e\n\u003c!-- ANCHOR: translating-c-to-rust --\u003e\n\n## Translating C to Rust\n\nTo translate C files specified in `compile_commands.json` (see below),\nrun the `c2rust` tool with the `transpile` subcommand:\n\n```sh\nc2rust transpile compile_commands.json\n```\n\n`c2rust` also supports a trivial transpile of source files, e.g.:\n\n```sh\nc2rust transpile project/*.c project/*.h\n```\n\nFor non-trivial projects, the translator requires the exact compiler commands used to build the C code.\nThis information is provided via a [compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html)\nfile named `compile_commands.json` (note that it must be named exactly `compile_commands.json`;\notherwise `libclangTooling` can have (silent) trouble resolving it correctly).\n(Read more about [compilation databases here](https://sarcasm.github.io/notes/dev/compilation-database.html)).\nMany build systems can automatically generate this file;\nwe show [a few examples below](#generating-compile_commandsjson-files).\n\nOnce you have a `compile_commands.json` file describing the C build,\ntranslate the C code to Rust with the following command:\n\n```sh\nc2rust transpile path/to/compile_commands.json\n```\n\nTo generate a `Cargo.toml` template for a Rust library, add the `--emit-build-files` option:\n\n```sh\nc2rust transpile --emit-build-files path/to/compile_commands.json\n```\n\nTo generate a `Cargo.toml` template for a Rust binary, do this:\n\n```sh\nc2rust transpile --binary myprog path/to/compile_commands.json\n```\n\nWhere `--binary myprog` tells the transpiler to use\nthe `main` function from `myprog.rs` as the entry point for a binary.\nThis can be repeated multiple times for multiple binaries.\n\nThe translated Rust files will not depend directly on each other like\nnormal Rust modules.\nThey will export and import functions through the C API.\nThese modules can be compiled together into a single static Rust library or binary.\n\nYou can run with `--reorganize-definitions` (which invokes `c2rust-refactor`),\nwhich should deduplicate definitions and directly import them\nwith `use`s instead of through the C API.\n\nThe refactorer can also be run on its own to run other refactoring passes:\n\n```sh\nc2rust refactor --cargo $transform\n```\n\nThere are several [known limitations](./docs/known-limitations.md) in this\ntranslator.\nThe translator will emit a warning and attempt to skip function\ndefinitions that cannot be translated.\n\n### Generating `compile_commands.json` Files\n\nThe `compile_commands.json` file can be automatically created\nusing either `cmake`, `meson`, `bear`, `intercept-build`, or `compiledb`.\n\nIt may be a good idea to remove optimizations (`-OX`) from the compilation database,\nas there are optimization builtins which we do not support translating.\n\n#### ... with `cmake`\n\nWhen creating the initial build directory with `cmake`,\nspecify `-DCMAKE_EXPORT_COMPILE_COMMANDS=1`.\nThis only works on projects configured to be built by `cmake`.\nThis works on Linux and MacOS.\n\n```sh\ncmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ...\n```\n\n#### ... with `meson`\n\nWhen creating the initial build directory with `meson`,\nit will automatically generate a `compile_commands.json`\nfile inside of `\u003cbuild_dir\u003e`.\n\n```sh\nmeson setup \u003cbuild_dir\u003e\n```\n\n#### ... with `bear`\n\n[`bear`](https://github.com/rizsotto/Bear) is recommended for projects whose build systems\ndon't generate `compile_commands.json` automatically\n(`make`, for example, unlike `cmake` or `meson`). It can also be useful\nfor `cmake` and `meson` to generate a subset of the full `compile_commands.json`,\nas it records all compilations that a subcommand does.\n\nIt can be installed with\n\n```sh\napt install bear\n```\n\nor\n\n```sh\nbrew install bear\n```\n\nUsage:\n\n```sh\nbear -- \u003cbuild command\u003e\n```\n\n`\u003cbuild command\u003e` can be `make`, `make`/`cmake` for a single target, or a single `cc` compilation:\n\n```sh\nbear -- make\nbear -- cmake --build . --target $target\nbear -- cc -c program.c\n```\n\nNote that since it detects compilations,\nif compilations are cached (by `make` for example),\nyou'll need a clean build first (e.g. `make clean`).\n\n#### ... with `intercept-build`\n\n`intercept-build` (part of the [scan-build](https://github.com/rizsotto/scan-build))\nis very similar, but not always as up-to-date and comprehensive as `bear`.\n`intercept-build` is bundled with `clang` under `tools/scan-build-py`,\nbut a standalone version can be easily installed via `pip` with:\n\n```sh\nuv tool install scan-build\n```\n\n#### ... with `compiledb`\n\nThe `compiledb` package can also be used for `make` projects if the other tools don't work.\nUnlike the others, it doesn't require a clean build/`make clean`.\nInstall via `pip` with:\n\n```sh\nuv tool install compiledb\n```\n\nUsage:\n\n```sh\n# After running\n./autogen.sh \u0026\u0026 ./configure # etc.\n# Run\ncompiledb make\n```\n\n\u003c!-- ANCHOR_END: translating-c-to-rust --\u003e\n\n## Contact\n\nTo report issues with translation or refactoring,\nplease use our [Issue Tracker](https://github.com/immunant/c2rust/issues).\n\nTo reach the development team, join our [discord channel](https://discord.gg/ANSrTuu)\nor email us at [c2rust@immunant.com](mailto:c2rust@immunant.com).\n\n## FAQ\n\n\u003e I translated code on platform X, but it didn't work correctly on platform Y.\n\nWe run the C preprocessor before translation to Rust.\nThis specializes the code to the target platform (usually the host platform).\nWe do, however, support cross-architecture transpilation with a different sysroot\n(cross-OS transpilation is more difficult because\nit can be difficult to get a sysroot for the target OS).\nFor example, on an `aarch64-linux-gnu` host, to cross-transpile to `x86_64-linux-gnu`,\nyou can run\n\n```sh\nsudo apt install gcc-x86-64-linux-gnu # install cross-compiler, which comes with a sysroot\nc2rust transpile ${existing_args[@]} -- --target=x86_64-linux-gnu --sysroot=/usr/x86_64-linux-gnu\n```\n\nThese extra args are passed to the `libclangTooling` that `c2rust-transpile` uses.\nYou sometimes also need to pass extra headers, as occasionally headers are installed globally\nin the default sysroot and won't be found in the cross-compiling sysroot.\n\n\u003e What platforms can C2Rust be run on?\n\nThe translator and refactoring tool support both macOS and Linux.\n\n## Uses of `c2rust transpile`\n\nThis is a list of all significant uses of `c2rust transpile` that we know of:\n\n| Rust | C | By | Safety | Description |\n| - | -- | - | - | - |\n| [`rav1d`](https://github.com/memorysafety/rav1d/) | [`dav1d`](https://code.videolan.org/videolan/dav1d) | @memorysafety, @immunant | fully safe | AV1 decoder |\n| [`rexpat`](https://github.com/immunant/rexpat) | [`libexpat`](https://github.com/libexpat/libexpat) | @immunant | safety unfinished | streaming XML parser |\n| [`unsafe-libyaml`](https://github.com/dtolnay/unsafe-libyaml) | [`libyaml`](https://github.com/yaml/libyaml) | @dtolnay | minor cleanup, fully unsafe | YAML parser and writer used by [`serde_yaml`](https://github.com/dtolnay/serde-yaml)\n| [`libyaml-safer`](https://github.com/simonask/libyaml-safer) | [`libyaml`](https://github.com/yaml/libyaml) | @simonask | fully safe | safe fork of [`unsafe-libyaml`](https://github.com/dtolnay/unsafe-libyaml) |\n| [`libbzip2-rs`](https://github.com/trifectatechfoundation/libbzip2-rs) | [`bzip2`](https://gitlab.com/bzip2/bzip2) | @trifectatechfoundation | fully safe | file compression |\n| [`tsuki`](https://github.com/ultimaweapon/tsuki) | [`lua`](https://www.lua.org/source/5.4/) | @ultimaweapon | fully safe | Lua interpreter |\n| [`spiro.rlib`](https://github.com/MFEK/spiro.rlib) | [`spiro`](https://github.com/raphlinus/spiro) | @ctrlcctrlv | fully safe | spline interpolation |\n| [`sapp-kms`](https://crates.io/crates/sapp-kms) | [`sokol`](https://github.com/floooh/sokol) | @not-fl3 | cleaned up, still unsafe | application rendering library |\n\nIf any other project successfully uses `c2rust`, feel free to add your ported project here.\n\n## Acknowledgements and Licensing\n\nThis material is available under the BSD-3 style license as found in the\n[LICENSE](./LICENSE) file.\n\nThe C2Rust translator is inspired by Jamey Sharp's [Corrode](https://github.com/jameysharp/corrode) translator.\nWe rely on [Emscripten](https://github.com/kripken/emscripten)'s\nRelooper algorithm to translate arbitrary C control flows.\nMany individuals have contributed bug fixes and improvements to C2Rust; thank you so much!\n\nThis material is based upon work supported by the United States Air Force and\nDARPA under Contracts No. FA8750-15-C-0124, HR0011-22-C-0020, and HR00112590133.\nAny opinions, findings and conclusions or recommendations expressed in this\nmaterial are those of the author(s) and do not necessarily reflect the views\nof the United States Air Force or DARPA.\n\nDistribution Statement A, \"Approved for Public Release, Distribution Unlimited.\"\n","funding_links":[],"categories":["Development tools","Rust","开发工具 Development tools","By Target Language","FFI Bindings","开发工具"],"sub_categories":["Transpiling","转化 Transpiling","Rust","Other dialects and variants","跨语言调用 Transpiling"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimmunant%2Fc2rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimmunant%2Fc2rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimmunant%2Fc2rust/lists"}