RISC-V is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles. The RISC-V ISA is provided under open source licenses that do not require fees to use.

More information about RISC-V can be found at:

Create a C library file.

In this tutorial I will be using the following tools:
  • riscv-nuclei-elf-gcc
  • riscv-nuclei-elf-ar
  • riscv-nuclei-elf-ranlib
  • riscv-nuclei-elf-nm
  • Spike emulator
Software prerequisites
How to build the Nuclei RISC-V GNU Compiler Toolchain (riscv-gnu-toolchain) on macOS
How to build Spike emulator and Proxy Kernel (PK) on macOS

  • This tutorial is written and tested for the macOS.

  • Create the following 4 files and place these files in the same directory (for example ~/demo):


  • Goto demo directory:
    Type: cd ~/demo

  • Compile add.c:
    Type: riscv-nuclei-elf-gcc -c -march=rv32imac -mabi=ilp32 -mcmodel=medlow add.c

    Output: add.o is created.

  • Compile subtract.c:
    Type: riscv-nuclei-elf-gcc -c -march=rv32imac -mabi=ilp32 -mcmodel=medlow subtract.c

    Output: subtract.o is created.

  • Create archive file:
    Type: riscv-nuclei-elf-ar rc libcaldemo.a add.o subtract.o

    Note 1: Enter the archive name (.a) followed by the files (.o) which will be put inside the archive.
    Note 2: The archive filename MUST start with the prefix "lib": libcaldemo.a

    Output: libcaldemo.a is created.

  • View the content of the archive file:
    Type: riscv-nuclei-elf-ar t libcaldemo.a


    To extract all files from the archive file:
    Type: riscv-nuclei-elf-ar x libcaldemo.a

    You can also use the tar command:
    View content, type: tar -tvf libcaldemo.a
    Extract content, type: tar -xvf libcaldemo.a

  • Generate an index to the contents of the archive (libcaldemo.a) and store it in the archive:
    Type: riscv-nuclei-elf-ranlib libcaldemo.a

    The GNU ranlib program is another form of GNU ar; running ranlib is completely equivalent to executing ar -s: Type: riscv-nuclei-elf-ar -s libcaldemo.a

  • Use the nm tool to list this index:
    An archive with such an index speeds up linking to the library and allows routines in the library to call each other without regard to their placement in the archive.

    Type: riscv-nuclei-elf-nm --print-armap libcaldemo.a

    Archive index:
    add_one in add.o
    subtract_one in subtract.o

    00000000 T add_one

    00000000 T subtract_one

  • Compile program.c using the archive/library (libcaldemo.a):
    Type: riscv-nuclei-elf-gcc -march=rv32imac -mabi=ilp32 -mcmodel=medlow
    program.c -L. -lcaldemo -o program

    Note 1:
    • -l (lowercase L) is for specifying libraries. The linker searches for a file named liblibrary.a

    • -L is for specifying library locations.
      For example: -L. means currect directory.

    • -I (uppercase i) is for specifying include directories of header files.
      For example: If the header file is located at ~/demo/includes/calculate.h

      Type: riscv-nuclei-elf-gcc -march=rv32imac -mabi=ilp32 -mcmodel=medlow
      -I./includes program.c -L. -lcaldemo -o program

    Note 2:
    You can also compile program.c without using the archive/library file.
    Type: riscv-nuclei-elf-gcc -march=rv32imac -mabi=ilp32 -mcmodel=medlow
    program.c add.o subtract.o -o program

  • Execute the program:
    Type: spike $PK_PATH/pk program


    I am using the Spike emulator to execute the program because the program only works on a RISCV architecture. The toolchain used (riscv-nuclei-elf-xxx) targets RISCV hardware.