RISC-V GNU Compiler Toolchain - How to compile on Windows

RISC-V GNU Compiler Toolchain - How to compile on Windows

This is a guide to compiling the RISC-V C and C++ cross-compiler on Windows 10. The RISC-V cross-compiler supports two build modes: a generic ELF/Newlib toolchain and a more sophisticated Linux-ELF/glibc toolchain.

These instructions concentrate on building the generic ELF/Newlib variant to support both a 32-bit Windows host and a 32-bit RISC-V target.

Getting MSYS2

  1. Install MSYS2 from http://msys2.github.io/

N.B. I used 64-bit version - msys2-x86_64-20161025.exe
N.B. This does not have a std:to_string() issue that pre-MSYS2 mingw32 exhibited.

MSYS2, after installation, provides three consoles.

  • MSYS2 MSYS - for general usage of MSYS2

  • MSYS2 mingw32 - for building 32-bit Windows applications

  • MSYS2 mingw64 - for building 64-bit Windows applications

http://www.davidegrayson.com/windev/msys2/ gives a very good explanation of the different MSYS2 consoles (mingw32, mingw64 and MSYS2).


  1. Install required tools and dependencies to build 32-bit RISCV toolchain

    1. Start an MSYS2 MSYS console (MSYS2 MSYS)

    2. install mingw-w64-i686-toolchain (i.e. the 32-bit toolchain) $ pacman -S mingw-w64-i686-toolchain N.B. Select all/default.

    3. Install make, autoconf, texinfo, diffutils, bison $ pacman -S autoconf bison diffutils make tar texinfo

    4. Close this MSYS2 MSYS console.

Getting the RISC-V cross-compiler sources

  1. Start an MSYS2 mingw32 console (MSYS2 MinGW 32 bit)

Download the latest source tree from riscv.org

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

N.B. This can take up to an hour to complete.

  1. Verify that you are using the correct gcc compiler

$ which gcc

This should display /mingw32/bin/gcc (not /bin/gcc)

$ gcc -v
gcc version 6.2.0 (Rev2, Built by MSYS2 project)

Installation (Newlib)

  1. Change into riscv-gnu-toolchain and run configure, targetting 32-bit RISC-V.

To build the Newlib cross-compiler, pick an install patch. If you choose, for example, /opt/riscv, then add /opt/riscv/bin to your PATH now. Then simply run the following command:

$ ./configure --prefix=/opt/riscv --with-arch=rv32g


  1. Adjust riscv-binutils-gdb/gdb/maint.h to include "sys/time.h".

Otherwise gdb won't build. [Fine] --> TODO, is this necessary [/END TODO - DROP THIS]

  1. After running the configure line above, edit the generated top-level Makefile by hand.

Adjust build-gcc-newlib line to contain --src=../src/newlib-gcc.., i.e. go from:

stamps/build-gcc-newlib: src/newlib-gcc stamps/build-binutils-newlib
cd $(notdir $@) && $(CURDIR)/$</configure \
        --target=riscv$(XLEN)-unknown-elf \
        --prefix=$(INSTALL_DIR) \
        --disable-libnls \


stamps/build-gcc-newlib: src/newlib-gcc stamps/build-binutils-newlib
cd $(notdir $@) && $(CURDIR)/$</configure \
        --target=riscv$(XLEN)-unknown-elf \
        --prefix=$(INSTALL_DIR) \
        --disable-nls \
        --src=../src/newlib-gcc \
N.B. Do not adjust srcdir --> this leads to problems with 'cp -a' on mingw.
  1. Run make.

$ make -j4 > make.log 2> make.err

N.B. Takes approximately 45 minutes on my machine with -j4, takes an hour and twenty minutes without (2.4GHz x 4, SSD)

  1. Strip the executables to reduce their size.

$ find /opt/riscv -executable -type f -exec strip {} ;

Sanity Checks

From the MSYS2 mingw32 console:

Check the file type of the generated gcc compiler is reasonable.

$ file /opt/riscv/bin/riscv-unknown-elf-gcc.exe

Should report PE32 executable (console) Intel 80386, for MS Windows

Use the generated gcc compiler to compile a Hello World application

$ riscv32-unknown-elf-gcc.exe hello.c -o hello.elf

Check the file type of the generated hello.elf image

$ file hello.elf

Should report ELF 32-bit LSB executable, UCB RISC-V, version 1 (SYSV), statically linked, not stripped

Running from DOS CMD prompt

  • Open a DOS CMD prompt

  • Adjust the path to include the MSYS64\mingw32\bin directory and the /opt/riscv/bin binaries.

    c:> set PATH=c:\MSYS64\mingw32\bin;c:\MSYS64\opt\riscv\bin;%PATH%

  • Run the executable as before.

    c:> riscv32-unknown-elf-gcc.exe hello.c -o hello.elf