在学习Rust的时候,相信不少人和我一样一直受Rust编译慢的问题困扰。最近阅读一篇写得相当不错的英文博客《Tips for Faster Rust Compile Times》,加上最近刚刚入手一台华为云服务器,于是决定尝试一下效果如何(主要介绍mold,sccache)。

运行环境:
操作系统:​Ubuntu 22.04​
CPU与内存:1核2G
mold版本:v1.3.1

编译、安装mold

按照mold官方的教程,使用源代码进行编译、安装。


git clone https://github.com/rui314/mold.git
cd mold
git checkout v1.3.1
make -j$(nproc) CXX=clang++
sudo make install

在执行编译时,系统报错,是因为mold使用C++20编写,需要相应的编译器进行编译器(g++-10),而操作系统默认的编译器gcc版本较低(gcc 9.4.0)。接下来则先安装相应的编译器。

apt install g++-10
g++-10 --version
apt install gcc-10
gcc-10 --version

再次编译,报如下错误;

root@hecs-131857:/home/rust/mold# make -j$(nproc) CXX=clang++
clang++ -DMOLD_VERSION=\"1.0.0\" -DLIBDIR="\"/usr/local/lib\"" -DGIT_HASH=\"ed9924895d9b9584106791247596677db8113528\" -Ithird-party/mimalloc/include -Ithird-party/tbb/include -O2 -pthread -fPIE -fno-unwind-tables -fno-asynchronous-unwind-tables -std=c++20 -fno-exceptions -c -o out/main.o main.cc
In file included from main.cc:1:
./elf/mold.h:30:10: fatal error: 'xxh3.h' file not found

于是使用apt-file查询xxh3.h所在的库并进行安装。

apt-get install apt-file
apt-file update
apt-file search xxh3.h
apt install libxxhash-dev

再次编译,还是报错,继续安装cmake,编译......。

(cd out/mimalloc; CFLAGS=-DMI_USE_ENVIRON=0 cmake -G'Unix Makefiles' ../../third-party/mimalloc)
/bin/sh: 1: cmake: not found
make: *** [Makefile:115: out/mimalloc/libmimalloc.a] Error 127apt install cmake

最后进行安装。

root@hecs-131857:/home/rust/mold# make install
install -m 755 -d /usr/local/bin
install -m 755 mold /usr/local/bin
strip /usr/local/bin/mold
install -m 755 -d /usr/local/lib/mold
install -m 644 mold-wrapper.so /usr/local/lib/mold
strip /usr/local/lib/mold/mold-wrapper.so
install -m 755 -d /usr/local/share/man/man1
install -m 644 docs/mold.1 /usr/local/share/man/man1
ln -sf mold /usr/local/bin/ld.mold
ln -sf mold /usr/local/bin/ld64.mold

测试mold

接下来测试未使用mold和使用mold两种情况的编译速度。

测试1:未使用mold

root@hecs-131857:/home/rust/mp-share-it# cargo clean
root@hecs-131857:/home/rust/mp-share-it# cargo build
-------省略-------Compiling tower v0.4.11Compiling hyper v0.14.15Compiling hyper-tls v0.5.0Compiling axum v0.3.4Compiling reqwest v0.11.7Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)Finished dev [unoptimized + debuginfo] target(s) in 3m 20s

测试2:使用mold,(修改 ~/.cargo/config

[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/mold"]
root@hecs-131857:/home/rust/mp-share-it# cargo clean
root@hecs-131857:/home/rust/mp-share-it# cargo build
-------省略-------Compiling tower v0.4.11Compiling hyper v0.14.15Compiling hyper-tls v0.5.0Compiling axum v0.3.4Compiling reqwest v0.11.7Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)Finished dev [unoptimized + debuginfo] target(s) in 3m 04s

经过一番操作折腾(走了不少弯路,踩了一些坑),通过测试的结果来看,mold对于加快Rust的编译速度还是有一定作用的(还有很大的优化空间)。

编译、安装sccache

cargo install sccache# 修改配置
vim ~/.cargo/config## 添加如下配置
[build]
rustc-wrapper = "/root/.cargo/bin/sccache"

测试sccache

测试1:未使用sccache

root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
-------省略-------Compiling criterion-plot v0.4.4Compiling criterion v0.3.5Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)Finished release [optimized] target(s) in 2m 43s
root@hecs-131857:/home/rust/leetcode-rust#

测试2:使用sccache

root@hecs-131857:/home/rust/leetcode-rust# cargo clean
root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
-------省略-------Compiling criterion-plot v0.4.4Compiling criterion v0.3.5Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)Finished release [optimized] target(s) in 26.76s
root@hecs-131857:/home/rust/leetcode-rust# 

经过测试,使用sccache能大幅加快rust编译速度,节约了不少编译时间。

Ubuntu 22.04​下加快rust的编译速度相关推荐

  1. Ubuntu 18.04 下搭建 C/C++编译开发环境及GCC多版本切换

    关注公众号 风色年代(itfantasycc) 领 500G Java 微服务 开发资料 Ubuntu 18.04 下搭建 C/C++编译开发环境及GCC多版本切换_Linux教程_Linux公社-L ...

  2. WSL Ubuntu 20.04下Android源码编译与导入Android Studio

    源码下载见前文:WSL Ubuntu 20.04下Android源码下载 idegen 模块编译 在development/tools/idegen/README下可以看到关于 idegen 工具的相 ...

  3. Ubuntu 22.04操作系统下常用软件备忘

    时间过得真快,不知不觉间Ubuntu的桌面版本已经到了22.04.本文记录我在Ubuntu 22.04下使用的部分软件,一方面供刚开始使用Ubuntu系统的朋友们参考,另外就是方便自己以后重装系统.下 ...

  4. [2014.3.4]天嵌TQ210在Ubuntu 11.04下编译 OpenCV2.0.0(PC)

    (一)先安装cmake (1)网上通用 apt-get install cmake 但我安装后有问题,cmake-gui出现段错误. (2)参考<CMake安装和使用> 解压cmake-2 ...

  5. Ubuntu 12.04 下编译Android 4.0.3

    在Ubuntu 12.04 下编译了Android 4.0.3发现了一大堆错误,网上找了一堆的更改源码啊,Android.mk的解决方法,虽然可以编译成功,但都有点坑爹的成分,为了不误人子弟就分享出来 ...

  6. ubuntu 环境下调试mysql源码_【转】Ubuntu 16.04下 Mysql 5.7.17源码编译与安装

    Ubuntu 16.04下 Mysql5.7.17源码编译与安装 系统环境 一. 系统安装条件 1.cmake MySQL使用cmake跨平台工具预编译源码,用于设置mysql的编译参数. sudo ...

  7. Ubuntu 14.04下编译WebRTC

    阿里云  >  教程中心   >  python教程  >  Ubuntu 14.04下编译WebRTC Ubuntu 14.04下编译WebRTC 发布时间:2018-02-28 ...

  8. Ubuntu 11.04下编译kernel

    Ubuntu 11.04下编译kernel 1. 下载并解压Linux内核源代码, 内核源码下载地址为www.kernel.org,查看当前内核版本命令为uname -r.下载后解压至目录/usr/s ...

  9. Ubuntu 22.04 LTS下安装1030 GPU 的驱动(图文详解)

    文章目录 Ubuntu 22.04 LTS下安装1030 GPU 的驱动 法一:命令获取推荐驱动直接安装(简单有效) 1.检查你的操作系统,如下图: 2.查看是否有[GPU](https://so.c ...

最新文章

  1. sysctl -p 重新加载文件/etc/sysctl.conf -a 所有参数 -w 临时指定
  2. 【问题收录】[ubuntu]startx doesn't work
  3. mysql 上一条 下一条,MYSQL实现上一条下一条功能
  4. 17.matlab中各种文件的I/O操作2——fopen操作
  5. Win32路径操作相关API
  6. linux kill命令使用方法,Linux初学者的killall命令(8个例子)
  7. OCR系列——文本检测任务
  8. 给大家一个网站程序,fastmai网站源码
  9. 消息中间件常见问题汇总
  10. cefsharp作为采集工具(学习笔记)
  11. python把英语句子成分字母_英语句子成分分析报告(最完整版)
  12. 使用R语言进行单(双)因素方差分析
  13. 加拿大电子计算机工程留学,【加中留学】加拿大计算机工程专业哪些大学比较好...
  14. iphone各机型的分辨率及像素
  15. Coinversation 的选择!一文了解 Polkadot 生态中的智能合约
  16. 李宏毅2022机器学习hw6
  17. 极路由1S刷openwrt
  18. 每日词根——sol(完整可靠安慰,太阳严肃)
  19. 阿里云天池大赛赛题(机器学习)——工业蒸汽量预测(完整代码)
  20. c语言程序编译与烧录,一种解释自定义脚本并烧录的方法与流程

热门文章

  1. 调用百度API 对文本进行情感倾向分析(舆情分析)
  2. 不卷大厂了,78位高校青年教师晒出工资
  3. 洋葱omall是什么
  4. 360清理计算机内存操作,如何清理计算机内存
  5. 弹指间计算机协会 2021“千里码”程序设计竞赛 题面
  6. 走进Spring Cloud之二 eureka注册中心(Greenwich版本)
  7. emoji昵称php,php过滤微信用户昵称emoji表情方法汇总 - 旗云号
  8. IMS:MotionEvent 坐标点(上)
  9. 2017湖北职称计算机考试,2017湖北职称计算机考试报名:黄石职称计算机报名入口...
  10. 在那江南烈日与阵雨中-江南100赛记