rust docker交叉编译静态单体执行文件

   1. 这是有人专门做好对编译对应平台的docker image(https://github.com/messense/rust-musl-cross).

Rust toolchainCross Compile TargetDocker Image Tag
stablex86_64-unknown-linux-muslx86_64-musl
stablei686-unknown-linux-musli686-musl
stablearm-unknown-linux-musleabiarm-musleabi
stablearm-unknown-linux-musleabihfarm-musleabihf
stablearmv7-unknown-linux-musleabihfarmv7-musleabihf
stablearmv5te-unknown-linux-musleabiarmv5te-musleabi
stablemips-unknown-linux-muslmips-musl
stablemipsel-unknown-linux-muslmipsel-musl

用法:

(1)先拉取对应镜像 docker pull messense/rust-musl-cross:armv7-musleabihf

(2)为了方便使用,把docker命令做成别名

alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src messense/rust-musl-cross:armv7-musleabihf'
(3)这样就可以用"rust-musl-builder"这个命令简化使用
 rust-musl-builder cargo build --release

(4)上面就和使用普通cargo打包一样了,调这个命令其它就相当于挂载本地项目到容器中打包。

(5)以上镜像为了打包静态单体,镜像中已包含了(为了有这个教程,就是在centos中打包静态单体时需要openssl,解决了普通openssl的打包,发现静态打包还需要musl-gcc,而centos找了半天也没有找到,所以就想到了docker省得自己搭环境了)

  • The standard musl-libc libraries.
  • OpenSSL, which is needed by many Rust applications.

 2.使用另一个镜像,其实是先找到这个镜像的(https://github.com/emk/rust-musl-builder

rust-musl-builder uses musl-libc, musl-gcc, and the new rustup target support. It includes static versions of several libraries:

  • The standard musl-libc libraries.
  • OpenSSL, which is needed by many Rust applications.
  • libpq, which is needed for applications that use diesel with PostgreSQL.
  • libz, which is needed by libpq.

 支持缓存构建:

You may be able to speed up build performance by adding the following -v commands to the rust-musl-builder alias:

-v cargo-git:/home/rust/.cargo/git
-v cargo-registry:/home/rust/.cargo/registry
-v target:/home/rust/src/target

You will also need to fix the permissions on the mounted volumes:

rust-musl-builder sudo chown -R rust:rust \
  /home/rust/.cargo/git /home/rust/.cargo/registry /home/rust/src/target

用法(和上面的用法一样,大部分用docker交叉编译都类似,镜像里帮你做好了编译环境所需要的一起,尤其是墙的问题更坑):

1. 
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
2. rust-musl-builder cargo build --release

 PS:若在运行编译时,报权限问题,chmod 777 -R 项目路径授权。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值