ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

压缩Rust编译出来的文件体积

2022-07-10 18:01:00  阅读:213  来源: 互联网

标签:10 07 17 压缩 rest 编译 2022 hello Rust


操作方法

以下操作基于windows测试

创建hello world项目

cargo new hello-rust

编译

cargo build --release

输出日志

F:\project\rust\hello-rest>cargo build --release
   Compiling hello-rest v0.1.0 (F:\project\rust\hello-rest)
    Finished release [optimized] target(s) in 1.37s

查看文件大小

 F:\project\rust\hello-rest\target\release 的目录

2022/07/10  17:43    <DIR>          .
2022/07/10  17:43    <DIR>          ..
2022/07/10  17:29                 0 .cargo-lock
2022/07/10  17:29    <DIR>          .fingerprint
2022/07/10  17:29    <DIR>          build
2022/07/10  17:43    <DIR>          deps
2022/07/10  17:29    <DIR>          examples
2022/07/10  17:29                97 hello-rest.d
2022/07/10  17:43           155,648 hello-rest.exe  //152KB
2022/07/10  17:43         1,232,896 hello_rest.pdb
2022/07/10  17:29    <DIR>          incremental
               4 个文件      1,388,641 字节
               7 个目录  4,129,562,624 可用字节

新增优化参数

打开Cargo.toml新增如下内容

[profile.release]
opt-level = 'z'  # Optimize for size
lto = true
codegen-units = 1
panic = 'abort'

再次编译并查看文件大小

F:\project\rust\hello-rest>cargo build --release
   Compiling hello-rest v0.1.0 (F:\project\rust\hello-rest)
    Finished release [optimized] target(s) in 7.52s

F:\project\rust\hello-rest>dir target\release
 驱动器 F 中的卷没有标签。
 卷的序列号是 64EE-EC8A

 F:\project\rust\hello-rest\target\release 的目录

2022/07/10  17:43    <DIR>          .
2022/07/10  17:43    <DIR>          ..
2022/07/10  17:29                 0 .cargo-lock
2022/07/10  17:29    <DIR>          .fingerprint
2022/07/10  17:29    <DIR>          build
2022/07/10  17:45    <DIR>          deps
2022/07/10  17:29    <DIR>          examples
2022/07/10  17:29                97 hello-rest.d
2022/07/10  17:45           133,632 hello-rest.exe //130KB
2022/07/10  17:45         1,232,896 hello_rest.pdb
2022/07/10  17:29    <DIR>          incremental
               4 个文件      1,366,625 字节
               7 个目录  4,129,583,104 可用字节

 

将优化后的方案使用upx压缩

 

压缩后的文件体积

F:\project\rust\hello-rest>dir target\release
 驱动器 F 中的卷没有标签。
 卷的序列号是 64EE-EC8A

 F:\project\rust\hello-rest\target\release 的目录

2022/07/10  17:48    <DIR>          .
2022/07/10  17:48    <DIR>          ..
2022/07/10  17:29                 0 .cargo-lock
2022/07/10  17:29    <DIR>          .fingerprint
2022/07/10  17:29    <DIR>          build
2022/07/10  17:45    <DIR>          deps
2022/07/10  17:29    <DIR>          examples
2022/07/10  17:29                97 hello-rest.d
2022/07/10  17:45            62,464 hello-rest.exe //61KB
2022/07/10  17:45         1,232,896 hello_rest.pdb
2022/07/10  17:29    <DIR>          incremental
               4 个文件      1,295,457 字节
               7 个目录  4,129,517,568 可用字节

压缩后运行效果

 

 

 

参考来源:

https://www.huangyunkun.com/2020/05/06/cargo-rust-output-size/

https://down10.software/download-upx/download/  upx下载网站

https://github.com/DaviRain-Su/rust-no-std-source //嵌入式可以使用#![no_std]减少文件体积

标签:10,07,17,压缩,rest,编译,2022,hello,Rust
来源: https://www.cnblogs.com/passedbylove/p/16463625.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有