当前位置: 首页 > news >正文

ubuntu20.04搭建RUST开发环境并与C语言交互

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

ubuntu20.04搭建RUST开发环境并与C语言交互

  • 前言
  • 开战
    • 一、确认环境版本
    • 二、环境搭建
    • 三、hello world!
    • 四、跟c语言进行交互
      • 1.rust调用C静态库
      • 2.C调用rust库
  • 总结
  • 参考


前言

开始学习rust,从网上扒资料搭建开发环境。后续再跟OpenHarmony-RISCV结合。


开战

一、确认环境版本

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、环境搭建

root@znvhwd:/home/ptg/rust# curl --proto ‘=https’ --tlsv1.2 https://sh.rustup.rs -sSf | sh
curl: (35) OpenSSL SSL_connect: 连接被对方重设 in connection to sh.rustup.rs:443
root@znvhwd:/home/ptg/rust# ls
root@znvhwd:/home/ptg/rust# sudo apt-get install git
正在读取软件包列表… 完成
正在分析软件包的依赖关系树
正在读取状态信息… 完成
git 已经是最新版 (1:2.25.1-1ubuntu3.13)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 31 个软件包未被升级。
root@znvhwd:/home/ptg/rust# curl --proto ‘=https’ --tlsv1.2 https://sh.rustup.rs -sSf | sh
curl: (35) OpenSSL SSL_connect: 连接被对方重设 in connection to sh.rustup.rs:443

获取rustup安装脚本失败,有资料说是没安装git导致,但环境中实际有git。大概率还是本地虚拟机网络的问题。
经排查修改DNS即可:Ubuntu修改DNS的方法

  1. 编辑 /etc/resolv.conf 文件
    sudo vim /etc/resolv.conf
  2. 加入以下代码
    nameserver 114.114.114.114
    nameserver 8.8.8.8

又遇新坑
在这里插入图片描述
不知道啥原因,曲线救国了。
浏览器打开https://sh.rustup.rs,直接下载到rustup-init.sh。然后“./”执行即可。
在这里插入图片描述
多灾多难。。
https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init
再次尝试,下载rustup-init。然后“./”执行。
报错:

error: error decoding response body: operation timed out

解决:

RUSTUP_DIST_SERVER=‘https://mirrors.ustc.edu.cn/rust-static’
RUSTUP_UPDATE_ROOT=‘https://mirrors.ustc.edu.cn/rust-static/rustup’

在这里插入图片描述
终于下完了,引用环境变量(环境变量已经默认写入到~/.bashrc)

source ~/.bashrc

试用:cargo
在这里插入图片描述
搞定。

三、hello world!

root@znvhwd:/home/ptg/rust# cargo new myos
Creating binary (application) myos package
note: see more Cargo.toml keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
root@znvhwd:/home/ptg/rust# ls
main.rs myos rustup-init rustup-init.sh
root@znvhwd:/home/ptg/rust# cd myos
root@znvhwd:/home/ptg/rust/myos# cargo run
Compiling myos v0.1.0 (/home/ptg/rust/myos)
Finished dev profile [unoptimized + debuginfo] target(s) in 0.20s
Running target/debug/myos
Hello, world!
root@znvhwd:/home/ptg/rust/myos# ls
Cargo.lock Cargo.toml src target
root@znvhwd:/home/ptg/rust/myos#
root@znvhwd:/home/ptg/rust/myos#
root@znvhwd:/home/ptg/rust/myos# cd src/
root@znvhwd:/home/ptg/rust/myos/src# ls
main.rs
root@znvhwd:/home/ptg/rust/myos/src# cat main.rs
fn main() {
println!(“Hello, world!”);
}

执行上面的命令应该是直接下载了一个git项目。

四、跟c语言进行交互

在Rust中调用C语言的代码需要以下几个步骤:

  1. 编写或获得C语言的代码。
  2. 创建Rust的外部函数接口(FFI)。
  3. 使用Rust的unsafe块调用C函数。

1.rust调用C静态库

参考 Rust调用C程序的实现步骤
编译得到一个C语言的静态库

/*swap.c*/
#include "stdint.h"int swap(int32_t* a, int32_t* b)
{int32_t tmp = *a;*a = *b;*b = tmp;return 0;
}

gcc -c swap.c
ar rcs libswap.a swap.o

在Rust中创建一个外部函数接口来使用这个库。
Cargo.toml文件中添加一个build.rs脚本以及libc依赖:

[package]
name = "myos"
version = "0.1.0"
edition = "2021"
build = "build.rs"[dependencies]
libc = "0.2"[build-dependencies]
cc = "1.0"

在build.rs脚本(笔者将其放在了项目根目录下)中告诉cargo如何构建C库

/*build.rs*/
extern crate cc;fn main()
{cc::Build::new().file("swap.c").compile("libswap.a");
}

创建Rust的外部函数接口,可以
修改hello rust的main.rs

/*main.rs*/
extern crate libc;extern "C"
{fn swap(a: *mut i32, b: *mut i32);
}fn main()
{println!!!!!!!!!!!("hello,rust!");let mut x = 5;let mut y = 10;unsafe{swap(&mut x as *mut i32, &mut y as *mut i32);}println!("x: {}, y: {}", x, y);
}

在这里插入图片描述
报错:

warning: spurious network error (3 tries remaining): [35] SSL connecterror (Recv failure: Connection reset by peer)
warning: spuriousnetwork error (3 tries remaining): [28] Timeout was reached

应该还是网络的问题。。
尝试wget 对应文件,SSL问题。
查了半天终于找到了解决方法

root@znvhwd:/home/ptg/rust/myos# cd ~/.cargo
root@znvhwd:~/.cargo# ls
bin config env registry
root@znvhwd:~/.cargo# cat config
[http]
check-revoke = false
root@znvhwd:~/.cargo# root@znvhwd:/home/ptg/rust/myos# cd ~/.cargo
root@znvhwd:~/.cargo# ls
bin config env registry
root@znvhwd:~/.cargo# cat config
[http]
check-revoke = false
root@znvhwd:~/.cargo# vim config

修改配置文件,应该是换了源

[http]
check-revoke = false
[source.crates-io]
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"

在这里插入图片描述
终于搞定了。

2.C调用rust库

参考 C语言和Rust语言的互相调用(1)(C调用Rust)

总结

没啥 ,找资料照做,遇到问题解决问题即可。

参考

https://zhaoseaside.blog.csdn.net/article/details/134484039
https://blog.csdn.net/fittec/article/details/137204059
https://zhuanlan.zhihu.com/p/687515644
https://www.jb51.net/program/307143aaq.htm
https://blog.csdn.net/phthon1997/article/details/126469708

http://www.lryc.cn/news/418244.html

相关文章:

  • C语言 ——— 学习、使用memmove函数 并模拟实现
  • 职场中必须明白的三个道理,不明白无出头之日,你越早知道越好
  • 做webserver项目的一些问题和思路总结
  • 大数据-70 Kafka 高级特性 物理存储 日志存储 日志清理: 日志删除与日志压缩
  • 基于S7-200 SMART实现PID控制仿真实验
  • 社交及时通讯平台完整版源码,uniapp技术,可打包成app
  • TensorFlow和Pytorch是什么?干什么用的?
  • 采购人可否自行选择采购方式?|数智化招采系统支持多种采购方式
  • ubuntu dde 改为中文
  • Nginx配置小细节,location和proxy_pass 斜杠/ 问题
  • java 解析 PDF OFD 发票 部分文字缺失
  • C/C++数字与字符串互相转换
  • [Spring] Spring AOP
  • 鸿蒙 webview 实现顶部 Progress进度条
  • Pytest-BDD实现接口自动化测试,并附全部代码
  • Sqli-labs-master靶场--布尔盲注
  • 【QGroundControl二次开发】十. QT添加GStreamer视频播放同时保存
  • double类型 精度丢失的问题
  • C++ 重要特性探究
  • c++_游戏_狼人杀
  • MySQL——数据类型、索引的建立、数据的约束
  • 常见框架漏洞详解③!!
  • 大数据基础知识
  • SQL Server 的透明数据加密
  • Windows图形界面(GUI)-MFC-C/C++ - 列表视图(List Control) - CListCtrl
  • 一机两用的简单介绍
  • uniapp离线打包热更新失败-AndroidStudio离线打包apk后无法下载打开-热更新失败-plus.runtime.install失败
  • 深植根基、蓬勃向上 | openKylin 2.0正式发布!
  • 【Material-UI】按钮组:尺寸与颜色详解
  • app抓包 burp配置