Bez popisu

marion 038eb13e1e cell regex před 2 roky
src 038eb13e1e cell regex před 2 roky
static 9b0b123dcf actix web před 4 roky
.gitignore 8e132bb6b4 ocr test před 4 roky
Cargo.lock 6e25141919 cell regex před 2 roky
Cargo.toml 6e25141919 cell regex před 2 roky
README.md e824e7d654 交叉编译步骤记录 před 4 roky

README.md

Rust测试项目

基础使用

项目依赖在 Cargo.toml

各种轮子库 crates.io

各种轮子库文档 docs.rs

创建项目

cargo new <项目名>

加 --lib 参数创建库项目,加 --vcs none 不初始化本地git仓库

默认是 --bin 和初始化 .git

下载依赖和构建项目

cargo build

加 --release 参数开启优化,并将编译结果输出到 target/release 目录

默认是 --debug 和输出到 target/debug 目录

运行

如果未构建则先构建

cargo run

测试

cargo test

为项目构建文档

cargo doc

将库发布到crates.io

cargo publish

更多cargo工具链说明参考 Cargo 中文文档

更新Rust和已安装target

rustup update

rustip自更新

rustup self update

加国内镜像

vim ~/.cargo/config

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

交叉编译

以MAC OS X作为上位机为例

默认target为x86_64-apple-darwin,编译输出在/target下的debug或release

其它交叉编译target输出在/target下的target名称对应子目录中

查看已安装或支持的目标平台

rustup target list

Linux(musl)

安装musl相关交叉编译工具链

brew install FiloSottile/musl-cross/musl-cross

要下载很多内容,耐心等待

可以加 --with-arm --with-arm-hf --with-i486 等选择平台,默认是带 --with-x86_64,也可以通过 --without-x86_64 取消

安装位置在 /usr/local/Cellar/musl-cross

关于在mac下基于muslc交叉编译linux、win目标的rust(或涉及到cgo的golang)程序的官方文档见: filippo.io

安装target

rustup target add x86_64-unknown-linux-musl

vim ~/.cargo/config,追加

[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"

一般不需要用到的环境变量 export CROSS_COMPILE=x86_64-linux-musl-

Linux(gnu)

x86_64-unknown-linux-gnu的交叉编译工具链配置较繁琐,Linux桌面默认就是linux gnu的环境,而服务器上使用,建议musl即可

MacOS X中配置linux gnu的步骤可参考以下网站

参考网站1

参考网站2

Android

安装android NDK

此处下载安装

解压移动到合适的位置,如 ~/Library/Android/ndk

编译

cd ~/Library/Android/ndk

r21/build/tools/make-standalone-toolchain.sh \
    --platform=android-18 --toolchain=arm-linux-androideabi-clang3.6 \
    --install-dir=android-18-toolchain --arch=arm

安装target

rustup target add arm-linux-androideabi

vim ~/.cargo/config,追加

[target.arm-linux-androideabi]
linker = "~/Library/Android/ndk/android-18-toolchain/bin/arm-linux-androideabi-clang"

参考网站

编译

执行编译命令时带上目标平台参数即可,如

cargo build --release --target=x86_64-unknown-linux-musl

如遇 openssl 相关编译错误,参考:https://github.com/richfelker/musl-cross-make/issues/65

CI工具中做rust交叉编译也可以考虑用现成的或自定义docker镜像,如: https://github.com/clux/muslrust