Rust 学习笔记

Posted by API Caller on July 19, 2019

听说是 Better C++, 不过我更多接触的是交叉编译与 FFI.

资料列表

安装配置

*NIX

略.

Windows

  • 安装 rustup 前先换源
1
2
set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
  • 安装 rustup

不考虑用 msvc, 所以先下载和配置好 mingw64mingw32

  • 修改 host triple 为 gnu
1
2
# https://www.rust-lang.org/zh-CN/tools/install 
./rustup-init.exe 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Current installation options:

   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>2

I'm going to ask you the value of each of these installation options.
You may simply press the Enter key to leave unchanged.

Default host triple?
x86_64-pc-windows-gnu

Default toolchain? (stable/beta/nightly/none)
stable

Modify PATH variable? (y/n)
y


Current installation options:

   default host triple: x86_64-pc-windows-gnu
     default toolchain: stable
  modify PATH variable: yes
  • cargo 换源
1
2
3
4
5
6
# 新建 %USERPROFILE%\.cargo\config 
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
1
2
3
rustup install stable-x86_64-pc-windows-gnu
rustup install nightly-x86_64-pc-windows-gnu
rustup default stable-x86_64-pc-windows-gnu
  • 交叉编译
1
2
3
4
5
# 先设置环境变量
set PATH=%MINGW32%\bin\;%PATH%

# 或者 64-bit
set PATH=%MINGW64%\bin\;%PATH%
1
2
3
4
5
6
7
8
# 配置 .cargo/config 文件  可以是全局也可以是项目内
[target.i686-pc-windows-gnu]
linker = "···/mingw32/bin/gcc.exe"
ar = "···/mingw32/bin/ar.exe"

[target.x86_64-pc-windows-gnu]
linker = "···/mingw64/bin/gcc.exe"
ar = "···/mingw64/bin/ar.exe"
1
2
3
cargo build
cargo build --release
cargo +nightly
  • 去黑框 (console)
1
2
3
4
5
6
7
8
9
10
# 配置项目内的 .cargo/config 文件
[target.i686-pc-windows-gnu]
linker = "···/mingw32/bin/gcc.exe"
ar = "···/mingw32/bin/ar.exe"
rustflags = ["-C", "link-args=-mwindows "]

[target.x86_64-pc-windows-gnu]
linker = "···/mingw64/bin/gcc.exe"
ar = "···/mingw64/bin/ar.exe"
rustflags = ["-C", "link-args=-mwindows "]
  • vscode rls

安装 rls 的时候 terminal 不要用 wsl, 否则无法安装.

Clion 配置调试

Run/Debug Configurations

  • Templates
    • Cargo Command
      • Environment Variables
        • PATH=···\mingw64\bin\;%PATH%

MSYS2 (有兼容问题还是别用了)

关于 stdio 的编译错误: 修复 GNU 版本的 Rust 在 Windows 上的兼容性问题 .

换源

1
2
3
4
5
6
7
8
9
10
11
12
pacman -S vim

vim /etc/pacman.d/mirrorlist.mingw32
# 开头添加 Server = http://mirrors.ustc.edu.cn/msys2/mingw/i686

vim /etc/pacman.d/mirrorlist.mingw64
# 开头添加 Server = http://mirrors.ustc.edu.cn/msys2/mingw/x86_64

vim /etc/pacman.d/mirrorlist.msys 
# 开头添加 Server = http://mirrors.ustc.edu.cn/msys2/msys/$arch

pacman -Sy # 刷新

安装各种 toolchain

1
2
3
pacman -S base-devel
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-i686-toolchain

交叉编译

go 惯了, 没办法, 走上来就关注这个, 总结一下笔记, 回头再细细整理.

其实就还是 gcc 那一套, 从这方面来说, 语言并没有提供太多便利.

此处用 host=ubuntu target=i686 举例

1
2
3
rustup show
rustup toolchain list
rustup target list
1
2
3
sudo apt install mingw-w64
rustup target add i686-pc-windows-gnu
rustup target add x86_64-pc-windows-gnu
1
2
3
# arm  记录用  无视
sudo apt install gcc-arm-linux-gnueabihf
rustup target add armv7-unknown-linux-gnueabihf
1
2
3
4
5
6
7
8
9
10
# ~/.cargo/config 或者项目下面直接 .cargo/config
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

[target.i686-pc-windows-gnu]
linker = "i686-w64-mingw32-gcc"
rustflags = "-C panic=abort"

[target.x86_64-pc-windows-gnu]
linker = "x86_64-w64-mingw32-gcc"
1
2
3
cargo build --target=i686-pc-windows-gnu 
cargo build --target=x86_64-pc-windows-gnu
cargo build --target=armv7-unknown-linux-gnueabihf

FFI

可能凡人总是会被自己熟悉的事物所耽搁吧, 记得摸 go 的时候也是走上来就摸 cgo.

WASM

似乎成了标配, 我对 wasm 还是很有兴趣的.

Refer

  1. Rust 环境配置事项一览
  2. Rust 在 Windows 下使用 CLion 进行调试
  3. 修复 GNU 版本的 Rust 在 Windows 上的兼容性问题
  4. rust-cross
  5. Can’t cross compile from Linux to Windows (i686) #32859
  6. help cross compiling for 32 bit on windows
  7. Cross-compile and link a static binary on macOS for Linux with cargo and rust
  8. 超方便的 rust 交叉编译
  9. japaric/docker