在学习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. 青少年编程竞赛交流群周报(第040周)
  2. 用Visio制作周行事历
  3. 用C#2.0实现网络蜘蛛(WebSpider)
  4. MFC的静态库.lib、动态库.dll(包含引入库.lib)以及Unicode库示例
  5. android 禁用dlsym_Android 7.0 dlopen 函数分析
  6. 机器学习之基于A*搜索解决八数码问题15数码问题
  7. 博客园修改页面显示样式
  8. 【恋上数据结构】计数排序
  9. 热门好用的邮编查询API
  10. java每日一练(19_03_18)
  11. php 获取京东商品详情,用正则表达式,抓取京东的商品列表
  12. A Linux Environment Zero Overhead
  13. 泰山OFFICE技术讲座:字体属性的上标研究1:上标是什么
  14. JAVA学习笔记(三十一)-电影系统案例
  15. Android 带你玩转实现游戏2048 其实2048只是个普通的控件(转)
  16. Python下载小说遮天
  17. C++中public:的意思
  18. 云原生监控报警可视化
  19. 实时日志监控系统-全览
  20. 新型海上风电机组及压缩空气储能系统的建模与控制(Matlab代码实现)

热门文章

  1. Echarts实现横轴数据节点不一致的绘制
  2. 免费看小说,国产浏览器出手了,吊打各类阅读软件
  3. NGINX源码之:ngx_arrayngx_listngx_string
  4. java面试专题(商城面试问题)
  5. 离散数学题目总结归纳
  6. 按位与,按位或和按位异或是否都满足交换律和结合律?
  7. 市场风险的度量-下行风险最大回撤
  8. 机车出入库相关、调车转线、、后期杂谈
  9. 如何查看小方侦测云存储_小方智能摄像机和手机怎样连接?
  10. 系统地和机壳GND间电容与电阻的作用