Building Your First RISC-V Barcode Reader App on Ubuntu 20.04

RISC-V is a free and open RISC instruction set architecture. The RISC-V community and ecosystem are boosting and will flourish in the next couple of years. To facilitate barcode app development for RISC-V, Dynamsoft has kicked off the build of RISC-V barcode SDK. In this article, I will show you how to utilize the experimental edition of Dynamsoft RISC-V barcode SDK to build a simple command-line barcode reader app and run it on QEMU RISCV emulator.

RISC-V Barcode SDK Download

So far, there is no full RISC-V barcode SDK package available online. Therefore, you need to download the required header files and library separately:

The next step is to extract the Linux package, and then replace the $(pwd)/Dynamsoft/BarcodeReader/lib/libDynamsoftBarcodeReader.so with the RISCV edition:

$ file libDynamsoftBarcodeReader.so 
libDynamsoftBarcodeReader.so: ELF 64-bit LSB shared object, UCB RISC-V, version 1 (SYSV), dynamically linked, with debug_info, not stripped
$ tar xvf dbr-linux-7.6.tar.gz
$ cp libDynamsoftBarcodeReader.so Dynamsoft/BarcodeReader/lib/libDynamsoftBarcodeReader.so

RISC-V GNU Compiler Toolchain Installation

To install the RISC-V compiler, you can either build it from the source code (take a bit long time) or quickly install the prebuilt toolchain via apt-get command.

Building the RISCV GNU Compiler

$ sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev
$ git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
./configure --prefix=/opt/riscv
$ sudo make linux

Installing the Prebuilt RISC-V Compiler

$ sudo apt install g++-riscv64-linux-gnu gcc-riscv64-linux-gnu

QEMU RISCV Emulator Installation

You may have noticed that there is a RISC-V ISA simulator called spike on GitHub. It is convenient to run a “hello world” program with the simulator. However, to run a barcode reader app with the shared library libDynamsoftBarcodeReader.so, you need an emulator.

You can download and build QEMU for RISCV64 with the following steps:

$ wget https://download.qemu.org/qemu-5.1.0.tar.xz
$ tar xvf qemu-5.1.0.tar.xz
$ cd qemu-5.1.0
$ ./configure --target-list=riscv64-softmmu 
$ make
$ sudo make install

The Fedora prebuilt images can save your time:

risc-v emulator qemu

You can boot Fedora as follows:

qemu-system-riscv64 \
   -nographic \
   -machine virt \
   -smp 4 \
   -m 2G \
   -kernel Fedora-Minimal-Rawhide-*-fw_payload-uboot-qemu-virt-smode.elf \
   -bios none \
   -object rng-random,filename=/dev/urandom,id=rng0 \
   -device virtio-rng-device,rng=rng0 \
   -device virtio-blk-device,drive=hd0 \
   -drive file=Fedora-Minimal-Rawhide-20200108.n.0-sda.raw,format=raw,id=hd0 \
   -device virtio-net-device,netdev=usernet \
   -netdev user,id=usernet,hostfwd=tcp::10000-:22
  • User name: riscv
  • Password: fedora_rocks!

Building and Running Barcode Reader Apps on RISC-V Emulator

If you have extracted the package of Dynamsoft Barcode Reader Linux edition, you can find the C++ sample project ReadBarcode under $(pwd)/Dynamsoft/BarcodeReader/samples/ directory.

Before building the project, you need to apply for a free 30-day trial license from the Dynamsoft customer portal, and then set the license key string in ReadBarcode.cpp:

reader.InitLicense("LICENSE-KEY");

Afterward, open Makefile and substitute the compiler with RISCV64 GCC:

CC=riscv64-linux-gnu-gcc

Save the Makefile and then build the project:

$ make

Alternatively, you can use the following command line to build the app without creating a Makefile:

$ riscv64-linux-gnu-g++ -o ReadBarcode ReadBarcode.cpp -lDynamsoftBarcodeReader -L<library directory> -Wl,-rpath=.

After successfully building the command-line barcode reader app with the RISC-V cross-compiler on Ubuntu, you can copy relevant files to the emulator environment with the scp command:

$ scp <user-name>@<ip address>:/<executable file path> ./
$ scp <user-name>@<ip address>:/<library file path> ./
$ scp <user-name>@<ip address>:/<image file path> ./

Note: If you do not build the app with -rpath, you must copy libDynamsoftBarcodeReader.so to /lib64/lp64d/ folder. Otherwise, you will get the error message:

error while loading shared libraries: libDynamsoftBarcodeReader.so: cannot open shared object file: No such file or directory

Now, you can finally run your first barcode reader app for RISC-V on QEMU emulator:

$ ./ReadBarcode AllSupportedBarcodeTypes.tif
RISCV barcode sdk

QEMU RISCV Documentation

https://wiki.qemu.org/Documentation/Platforms/RISCV

Source Code

https://github.com/Dynamsoft/riscv-barcode-sdk-experimental