【rust】rust linux 环境搭建 (ubuntu&openwrt)

前言

rust中的几个概念:toolchain , target , component , channel , profile
toolchain:本机的编译环境,需要多个组件支持,可以更新、卸载等
target :主要针对交叉编译链,target对应编译后的文件需要运行的平台环境

0.环境

ubuntu-1804


1.安装步骤

1.下载并配置

官网参考链接:
https://www.rust-lang.org/zh-CN/tools/install

# cmd 1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# cmd 2 : input : 提示输入是否安装,输入1 ,开始默认安装
# cmd 3 : 更新当前shell环境
source $HOME/.cargo/env
# done 
# log 
/*
xxxx@:~/workspace/rust/demo$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /home/xxxx/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /home/xxxx/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /home/xxxx/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /home/xxxx/.profile
  /home/xxxx/.bashrc

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-unknown-linux-gnu
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
> 1
// 输入 1 ,开始默认安装

info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2021-03-25, rust version 1.51.0 (2fd73fabe 2021-03-23)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
 50.4 MiB /  50.4 MiB (100 %)  21.6 MiB/s in  2s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: using up to 500.0 MiB of RAM to unpack components
info: installing component 'clippy'
info: installing component 'rust-docs'
info: installing component 'rust-std'
 24.9 MiB /  24.9 MiB (100 %)  15.2 MiB/s in  2s ETA:  0s
info: installing component 'rustc'
 50.4 MiB /  50.4 MiB (100 %)   9.4 MiB/s in  5s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu installed - rustc 1.51.0 (2fd73fabe 2021-03-23)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env
// 更新
@:source $HOME/.cargo/env
*/

2.卸载rustup环境

# cmd : 出问题了就卸载,多卸载几次就明白这个环境怎么玩了
rustup self uninstall

2.本机demo构建、编译及运行

官方代码参考如下url:
https://doc.rust-lang.org/stable/rust-by-example/hello.html

1.项目结构

hello.rs

2.源码

//file name : hello.rs
fn main() {
    println!("Hello World!");
}

3.编译

# cmd :
rustc hello.rs

4.运行

# cmd 
./hello
# log :
/*
$ ./hello
Hello World!
*/

5.利用cargo搭建项目

见下文范例


3.openwrt平台开发环境搭建

1.交叉编译环境搭建

1.1 利用rustup搭建

前言:
这是官网的配置说明文件url :
https://doc.rust-lang.org/cargo/reference/config.html
如下是中文版rustup命令的一些说明,仅供参考:
https://wiki.jikexueyuan.com/index.php/project/rust-primer/install/rustup.html
Tip :关于toolchain可以访问如下url for more details:
https://doc.rust-lang.org/nightly/rustc/platform-support.html

# cmd : 查看已经支持的toolchain
rustup target list
# cmd : 查看当前环境toolchain
rustup show
# cmd 1 : 安装 
rustup target add armv7-unknown-linux-gnueabi
# log : 
/*
$ $ rustup target add armv7-unknown-linux-gnueabi
info: downloading component 'rust-std' for 'armv7-unknown-linux-gnueabi'
info: installing component 'rust-std' for 'armv7-unknown-linux-gnueabi'
info: using up to 500.0 MiB of RAM to unpack components
 19.5 MiB /  19.5 MiB (100 %)  12.9 MiB/s in  1s ETA:  0s
 */
# cmd : 再看一下当前环境toolchain
 rustup show
 # log :
 /*
 $ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home:  /xxxxx/.rustup

installed targets for active toolchain
--------------------------------------

armv7-unknown-linux-gnueabi
x86_64-unknown-linux-gnu

active toolchain
----------------

stable-x86_64-unknown-linux-gnu (default)
rustc 1.52.0 (88f19c6da 2021-05-03)
*/
# ret :这样的环境就可以生成armv7和x86_64的代码了

1.2 利用SDK中toolchain

参考如下文件链接:
https://blog.csdn.net/yujianliam/article/details/118144905

2.使用cargo命令新建开发环境

前言:新建项目,参考如上代码可以。
下文使用 cargo 命令搭建项目。

# cmd 
cargo new test_toolchain_demo
# ps : cargo new 名字要求没有冲突,有冲突换个项目名
# log
/*
$ cargo new test_toolchain_demo
     Created binary (application) `test_toolchain_demo` package
*/
"新建项目会默认生成如下文件(夹):"
Cargo.toml	   		//项目管理
.git/				//版本控制
.gitignore			//git忽略文件(夹)
src/				//源码文件夹
   main.rs			//hello范例代码

3.编译

# cmd 
cargo b 
或者
cargo build
# log :
/*
$ cargo b
   Compiling test_toolchain_demo v0.1.0 (/xxxxxxx/test_toolchain_demo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.33s
 */
 # ret :
 编译后会在target/debug/文件夹下有个一个与项目名相同的二进制文件。

PS:cargo build --release // <=> target/release/xxxxx
PS:cargo run // <=> cargo build + bash

4.运行

前言:
可以尝试直接在编译主机上运行,例如这里若是程序在ubuntu上正常执行,那你的交叉编译链多半是有问题的。
在本机上执行生成的二进制文件(ldd仅供参考):
./target/debug/test_toolchain_demo //或者 release/文件夹下

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过得精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值