如何搭建RISC-V编译环境与运行环境

发布时间:2021-12-10 19:06:32 作者:柒染
来源:亿速云 阅读:324

如何搭建RISC-V编译环境与运行环境,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

我们现在芯片被美国卡脖子,所以RISC-V是大势所趋。华为鸿蒙提供的编译器(https://repo.huaweicloud.com/harmonyos/compiler/)里也有RISC-V。虽然现在硬件资源比较少,但是通过软件模拟环境,可以提早做好准备。

RISC-V GNU Toolchain

源码获取

要体验RISC-V,首先需要安装交叉编译器。然而国内的网络很差,想要在GitHub上拿到源码(https://github.com/riscv/riscv-gnu-toolchain)编译非常困难。比较庆幸的是Gitee上有镜像(https://gitee.com/mirrors/riscv-gnu-toolchain),每天同步一次。

获取源码:

git clone https://gitee.com/mirrors/riscv-gnu-toolchain.git
cd riscv-gnu-toolchain
git submodule update --init --recursive

然而在我通过Gitee拿代码的时候,子模块下载速度依然非常慢,特别是QEMU里的一个子模块。于是我把QEMU从同步中删除了,因为不影响编译:

git rm --cached QEMU
git submodule update --init --recursive

考虑到下载速度,我把拿到的所有代码放到Gitee上(https://gitee.com/yushulx/riscv-gnu-toolchain)。这个代码库不做更新,只提供给网络差,又需要体验RISC-V的人。需要拿最新代码就去GitHub或者Gitee镜像。

编译安装

接下来就按照官方教程编译。

依赖工具

Ubuntu

$ sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev

Fedora/CentOS/RHEL OS

$ sudo yum install autoconf automake python3 libmpc-devel mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel expat-devel

Arch Linux

$ pacman -Syyu autoconf automake curl python3 mpc mpfr gmp gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib expat

OS X

$ brew install python3 gawk gnu-sed gmp mpfr libmpc isl zlib expat
编译GCC

编译riscv64-unknown-elf-gcc:

./configure --prefix=/opt/riscv
sudo make

编译64-bitriscv64-unknown-linux-gnu-gcc:

./configure --prefix=/opt/riscv
sudo make linux

编译32-bit:

./configure --prefix=/opt/riscv --with-arch=rv32gc --with-abi=ilp32d
sudo make linux

编译32-bit和64-bit:

./configure --prefix=/opt/riscv --enable-multilib
sudo make linux

这两个gcc的区别在于,elf-gcc是静态链接,linux-gnu-gcc是动态链接。

RISC-V运行环境

Simulator和Emulator的不同之处在于Emulator提供的是一个完整的模拟环境。

Simulator

首先编译安装pk: https://github.com/riscv/riscv-pk。

然后编译安装spike:https://github.com/riscv/riscv-isa-sim

编译程序

riscv64-unknown-elf-gcc -o hello hello.c

运行

spike $(which pk) hello

注意,如果用riscv64-unknown-linux-gnu-gcc编译,运行会报错:

bbl loader
not a statically linked ELF program

Emulator

下载安装QEMU https://www.qemu.org/download/#source

参考教程运行模拟环境:https://wiki.qemu.org/Documentation/Platforms/RISCV

另外也可以使用tinyemu: https://bellard.org/tinyemu/

Fedora镜像获取:https://dl.fedoraproject.org/pub/alt/risc-v/repo/virt-builder-images/images/

如何搭建RISC-V编译环境与运行环境

解压镜像:

unxz Fedora-Minimal-Rawhide-*-sda.raw.xz

启动模拟器:

 qemu-system-riscv64 \
   -nographic \
   -machine virt \
   -smp 4 \
   -m 2G \
   -kernel Fedora-Minimal-Rawhide-*-fw_payload-uboot-qemu-virt-smode.elf \
   -bios none \
   -object rng-random,filename=/dev/urandom,id=rng0 \
   -device virtio-rng-device,rng=rng0 \
   -device virtio-blk-device,drive=hd0 \
   -drive file=Fedora-Minimal-Rawhide-20200108.n.0-sda.raw,format=raw,id=hd0 \
   -device virtio-net-device,netdev=usernet \
   -netdev user,id=usernet,hostfwd=tcp::10000-:22

登录用户名riscv,密码fedora_rocks!

拷贝程序到模拟器中:

scp <user-name>@<ip address>:/<file path> ./

在模拟器环境中可以正常运行riscv64-unknown-elf-gccriscv64-unknown-linux-gnu-gcc编译出来的程序。

如何搭建RISC-V编译环境与运行环境

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 简单搭建hadoop运行环境
  2. android__Ubuntu-编译环境搭建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

risc-v

上一篇:怎么实现Zynq-7000开发环境的搭建

下一篇:如何解析搭建找usdt通道接口结算系统开发

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》