Ubuntu18.04搭建RISCV环境

1、安装RISCV-tools

/home/yolo/riscv

git clone https://github.com/riscv/riscv-tools.git
cd riscv-tools
git submodule update --init --recursive


如果网不好,可以一个一个安装

git clone --recursive https://github.com/riscv/riscv-openocd.git
git clone --recursive https://github.com/riscv/riscv-isa-sim.git
git clone --recursive https://github.com/riscv/riscv-opcodes.git
git clone --recursive https://github.com/riscv/riscv-pk.git
git clone --recursive https://github.com/riscv/riscv-tests.git

2、下载riscv gun toolchain

/home/yolo/riscv/riscv-tools

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

3、先编译riscv gun toolchain,再编译riscv tools

安装依赖包

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

cd /home/yolo/riscv/riscv-tools/riscv-tools/riscv-gnu-toolchain

vim ~/.bashrc 添加环境变量

export  RISCV="/home/yolo/riscv/riscv-tools/riscv-gnu-toolchain"
export PATH=$PATH:$RISCV/bin

然后 source ~/.bashrc
sudo ./configure --prefix=$RISCV
sudo make

现在可以可以生成可执行文件hello

#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}
riscv64-unknown-elf-gcc  -o  hello hello.c
这时候的 hello,并不能用./hello执行,因为它是riscv asi的机器码,而我们的计算机是x86平台,我们可以spike模拟器来执行该文件

4、Spike和pk安装

/home/yolo/riscv/riscv-tools/riscv-isa-sim

sudo apt-get install device-tree-compiler
sudo mkdir build
cd build
 sudo ../configure --prefix=$RISCV
 sudo make
 sudo make install

/home/yolo/riscv/riscv-tools/riscv-pk

 sudo mkdir build
 cd build
 sudo ../configure --prefix=$RISCV --host=riscv64-unknown-elf
 sudo make
 sudo make install

中间有个BUG弄的我想哭crying

gcc: error: unrecognized argument in option ‘-mcmodel=medany’
gcc: note: valid arguments to ‘-mcmodel=’ are: 32 kernel large medium small; did you mean ‘medium’?
Makefile:332: recipe for target 'file.o' failed
make: *** [file.o] Error 1

就是因为checking for riscv64-unknown-elf-gcc... no

此时,尽管你在 .bashrc中添加RISCV并source,还是不行,需要你单独运行一下

export RISCV="/home/yolo/riscv/riscv-tools/riscv-gnu-toolchain"

export PATH=$PATH:$RISCV/binindecision

如果还是不行,切换root用户喔

模拟器已完成安装,执行文件hello

spike pk hello

输出“hello,world”,安装成功!

知乎大佬有网盘:https://zhuanlan.zhihu.com/p/162907810

不错的教程:

https://blog.csdn.net/shensen0304/article/details/95504258?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159737489319724848302436%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=159737489319724848302436&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-4-95504258.first_rank_v2_rank_v25&utm_term=ubuntu18.-4%E5%AE%89%E8%A3%85riscv&spm=1018.2118.3001.4187

https://www.cnblogs.com/mikewolf2002/p/10799553.html

riscv各种版本gcc工具链编译与安装 

https://blog.csdn.net/weiqi7777/article/details/88045720

 上一页

LInux