Ubuntu 18.04搭建RISCV和QEMU环境

前言

因为公司项目代码需要在RISCV环境下测试,因为没有硬件实体,所以在Ubuntu 18.04上搭建了riscv-gnu-toolchain + QEMU模拟器环境。

安装riscv-gnu-toolchain

riscv-gnu-toolchain可以从GitHub上下载源码编译,地址为:https://github.com/riscv-collab/riscv-gnu-toolchain

首先将项目克隆到本地:

bash 复制代码
git clone https://github.com/riscv/riscv-gnu-toolchain

在编译前需要实现安装些依赖项目:

bash 复制代码
$ sudo apt-get install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev

创建riscv文件夹,存放编译后的toolchain,需要写入权限,例如:

bash 复制代码
sudo mkdir /opt/riscv
sudo chmod 777 /opt/riscv

然后可以在克隆下的riscv-gnu-toolchain文件夹里创建buid文件夹,进行编译,过程花很多时间,需要耐心等待:

bash 复制代码
cd riscv-gnu-toolchain
mkdir build
cd build
../configure --prefix=/opt/riscv --enable-multilib
make linux 

完成后就能在/opt/riscv/bin文件夹下看到如下内容:

然后配置bashrc,在bashrc中添加路径:

bash 复制代码
export PATH=$PATH:/opt/riscv/bin

接下来就能测试下riscv gcc的编译,创建一个hello.c文件,内容如下:

c 复制代码
#include <stdio.h>

void main() {
  printf("%s\n","hello RISCV");
}

然后编译:

bash 复制代码
riscv64-unknown-linux-gnu-gcc hello.c -o hello

编译无报错,生成可执行文件是无法直接运行的,加下来需要安装QEMU。

安装QEMU

riscv-gnu-toolchain文件夹里也有个qmeu的文件夹,根据.gitmodules里的内容:

我们也可以从GitLab上下载QEMU的源码进行编译,同样做些准备工作:

bash 复制代码
git clone https://gitlab.com/qemu-project/qemu.git
bash 复制代码
sudo mkdir /opt/qemu
sudo chmod 777 /opt/qemu

GitLab直接克隆下的master分支应该是最新版本8.2.0版本的,这里博主就遇到了一个坑,先在riscv-gnu-toolchain/qemu创建build目录,然后运行:

bash 复制代码
cd build
../configure --prefix=/opt/qemu
make
make install

结果在configure的时候有一些报错,例如下面的要使用python3.8,要安装python模块的等:

解决方法:

bash 复制代码
sudo apt install python3.8
pip install distlib
sudo ln -sf /usr/bin/pyhton3.8 /usr/bin/pyhton

费了好大劲解决了配置问题,结果编译没有通过。用git branch -a命令查看发现QEMU还有其他stable的分支:

然后果断用下面命令切换到了stable-7.2分支:

bash 复制代码
git checkout stable-7.2

然后重新configure和编译,编译成功,然后我们就能在/opt/qemu/bin下看到:

然后配置bashrc,在bashrc中添加:

bash 复制代码
export PATH=$PATH:/opt/qemu/bin
export QEMU_LD_PREFIX=/opt/riscv/sysroot # 否则qemu-riscv64会在/lib下搜索riscv64的动态链接

source bashrc之后我们就可以运行原来编译出的hello程序:

bash 复制代码
qemu-riscv64 hello
相关推荐
orion577 小时前
Missing Semester Class1:course overview and introduction of shell
linux
SkyWalking中文站11 小时前
认识 Horizon UI · 6/17:Trace 探索器
运维·监控·自动化运维
用户1204872216113 小时前
Linux驱动编译与加载
linux·嵌入式
火车叼位15 小时前
写给初级开发者:SSL、SSH、HTTPS 与证书体系全解析
运维
用户8055336980319 小时前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户8055336980319 小时前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
小猿姐1 天前
唯品会大规模数据库云原生实践:基于 KubeBlocks 管理数千实例的统一运维之路
运维·elasticsearch·云原生
七歌杜金房1 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
SkyWalking中文站2 天前
认识 Horizon UI · 5/17:3D 基础设施地图
运维·监控·自动化运维
tntxia2 天前
linux curl命令详解_curl详解
linux