GCC RISCV环境搭建

GCC RISCV环境搭建

在搭建环境之前有两个注意点一定要遵循

a、不要试着在windows下使用wsl搭建环境,直接使用ubantu虚拟机就行

b、安装文件的路径一定不要包含中文名称


1、这里使用的是Vmware,ubantu18-64位。安装riscv工具链需要clone几个G的文件,在开始搭建环境之前,强烈建议先解决好git clone速度过慢的问题。

2、搭建环境前安装如下依赖

sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev libusb-1.0-0-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev device-tree-compiler pkg-config libexpat-dev

3、下载 riscv-tools

输入如下命令进行下载,设置–recursiv能一次性clone所有的模块

git clone --recursiv https://github.com/riscv/riscv-tools.git

如果你的网络不是很好的话,当然也可以选择分模块进行克隆,输入如下clone命令,上述的clone命令和下面的等效

git clone https://https://github.com/riscv/riscv-tools.git
cd riscv-tools
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

4、下载riscv gun toolchain

同样的我们可以输入下面的命令一次性clone所有的模块

git clone --recursive https://github.com/riscv/riscv-gnu-toolchain

如果你网络不好的话,那么可以按照下面的命令进行逐个模块的clone

git clone https://github.com/riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git clone --recursive https://git.qemu.org/git/qemu.git
git clone --recursive https://github.com/riscv/riscv-newlib.git
git clone --recursive https://github.com/riscv/riscv-binutils-gdb.git
git clone --recursive https://github.com/riscv/riscv-dejagnu.git
git clone --recursive https://github.com/riscv/riscv-glibc.git
git clone --recursive https://github.com/riscv/riscv-gcc.git

逐个模块clone时要特别注意qemu模块的clone地址是https://git.qemu.org,而不是https://github.com。

逐个clone下来的目录结构和一次性clone有差别,我们输入如下命令就行纠正,下述命令表示将riscv-binutils-gdb文件夹下的文件全部复制到riscv-gdb和riscv-binutils下

cd riscv-binutils-gdb
cp -a * ../riscv-gdb
cp -a * ../riscv-binutils

5、对工具链进行编译

设置RISCV环境变量,修改~/.bashrc文件,在文件末尾添加如下文本

export RISCV="/home/{你的用户名}/riscv/riscv-gnu-toolchain"
export PATH=$PATH:$RISCV/bin

输入如下命令使得配置生效

source ~/.bashrc

输入如下命令产生makefile文件

./configure --prefix=$RISCV

然后执行make指令,如果出现权限不够的报错则执行sudo make指令,这里默认编译的是64位gcc riscv工具链。

6、编译riscv-tools

来到riscv-tools目录下执行./build.sh命令,进行编译

如果出现下面的错误,这时需要重新下载riscv-openocd,使用git clone --recursive https://github.com/riscv/riscv-opcodes.git

Configuring project riscv-openocd
configure: error: cannot find install-sh, install.sh, or shtool in ".." "../.." "../../.."

还有可能出现下面的一个错误,这是少了某些软件包

onfigure.ac:12: error: possibly undefined macro: AC_MSG_WARN
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:201: error: possibly undefined macro: AC_DEFINE
configure.ac:624: error: possibly undefined macro: AC_MSG_NOTICE
autoreconf: /usr/bin/autoconf failed with exit status: 1

解决办法

方法:sudo apt-get install libsysfs-dev
sudo apt-get install pkg-config

错误三如下

configure: error: device-tree-compiler not found

这也是少了一个软件包,输入下面的命令进行下载

sudo apt-get install device-tree-compiler

riscv-tools编译完成之后,环境就搭建完成了,这时我们可以进行测试

编写C代码文件

#include <stdio.h>
int main(void)
{
        printf("Hello World!\n");
        return 0;
}

使用riscv-gcc进行编译

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

用spike模拟器来执行

spike pk hello

如若执行成功则表明环境搭建成功


常见问题

问题一:在编译riscv-gnu-toolchain时出现如下报错

Makefile:541: recipe for target 'bfd.info' failed
make[4]: *** [bfd.info] Error 1
make[4]: Leaving directory '/mnt/d/2020-DaSan/小学期/riscv2/riscv-gnu-toolchain/build-binutils-newlib/bfd/doc'
Makefile:1648: recipe for target 'info-recursive' failed
make[3]: *** [info-recursive] Error 1
make[3]: Leaving directory '/mnt/d/2020-DaSan/小学期/riscv2/riscv-gnu-toolchain/build-binutils-newlib/bfd'
Makefile:2766: recipe for target 'all-bfd' failed
make[2]: *** [all-bfd] Error 2
make[2]: Leaving directory '/mnt/d/2020-DaSan/小学期/riscv2/riscv-gnu-toolchain/build-binutils-newlib'
Makefile:852: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/mnt/d/2020-DaSan/小学期/riscv2/riscv-gnu-toolchain/build-binutils-newlib'
Makefile:396: recipe for target 'stamps/build-binutils-newlib' failed
make: *** [stamps/build-binutils-newlib] Error 2

解决办法:最好查看一下文件安装路径中有没有中文,如果有一定要去除

**问题二:**在安装riscv-tools出现如下的错误,和问题一一样可能也是因为文件安装路径中包含中文所引起的

gcc: error: unrecognized argument in option ‘-mcmodel=medany’
gcc: note: valid arguments to ‘-mcmodel=’ are: 32 kernel large medium small; did you mean ‘medium’?
make: *** [file.o] Error 1
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值