Create a C library file.
Information
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
Procedure
- 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):
add.c
subtract.c
calculate.h
program.c
- 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
Output:
add.o
subtract.o
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
Note:
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
Output:
Archive index:
add_one in add.o
subtract_one in subtract.o
add.o:
00000000 T add_one
subtract.o:
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
Output:
value=10
value+1=11
value-1=9
Note:
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.
|