c2rust简单使用

参考链接:

https://c2rust.com/manual/quickstart.html#translating-c-to-rust

https://c2rust.com/manual/docs/README-developers.html


diffutils为例,使用c2rust工具,生成c对应的rust代码。

本文档分为两个部分:

一部分是:对c2rust源码的操作,最终生成c2rust二进制文件。

另一部分:是对diffutils源码的操作,生成compile_commands.json文件,并通过compile_commands.json文件,生成最终的rust文件。

一、c2rust

官网地址:https://c2rust.com/

安装rustup环境:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

c2rust 源码下载:https://github.com/immunant/c2rust

 git clone git@github.com:immunant/c2rust.git

生成c2rust文件:

./scripts/build_translator.py

运行完成后,会生成 target/release/c2rust文件。

二、diffutils

下载源码:(本次测试的是3.6-8 版本)

git clone git@github.com:Distrotech/diffutils.git
cd diffutils/
./configure

下载生成json的工具:

pip install scan-build

生成json文件:

intercept-build make

执行完成后,会在当前目录下生成compile_commands.json文件。

这里因为编译命令就是make,所以使用intercept-build make,如果是其他命令,需要修改为对应命令。(本人只测试过make的情况)

在当前目录下执行如下命令,生成rust文件

~/c2rust/target/release/c2rust  transpile compile_commands.json

其中,~/c2rust/target/release/c2rust 是上一步生成的c2rust二进制文件。

最终效果如下:
在这里插入图片描述

通过c2rust转换出来的代码,直接使用是没有问题的,但是可读性很差,还是需要再手动调整下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值