Cross Compilation
1. Build Environment
To run a C++ program on the board side, you need to compile its dependencies (or you can directly use the provided executables). To reduce the load on the edge device, we use an x86 Linux environment for cross-compilation (the example uses WSL; for WSL usage see the official documentation).
Two ways to set up the cross-compilation environment:
(1) Install the cross-compilation toolchain via apt:
If your system's libc version matches the target SoC platform's libc version (check with ldd --version), you can install with:
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnuTo uninstall:
sudo apt remove cpp-*-aarch64-linux-gnuIf your environment does not meet the above requirements, method (2) is recommended.
(2) Set up the cross-compilation environment via Docker:
You can use the Docker image we provide — stream_dev.tar — as the cross-compilation environment.
#安装dfss下载工具
pip3 install dfss
#安装docker镜像
python3 -m dfss --url=open@sophgo.com:sophon-demo/common/docker/stream_dev.tar # ubuntu 20.04, gcc-9If you are using Docker for the first time, run the following commands to install and configure it (only needed once):
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp dockerLoad the image from the downloaded image directory:
docker load -i stream_dev.tarYou can view the loaded image with docker images; it is named stream_dev:latest by default.
Create a container:
docker run --privileged --name stream_dev -v $PWD:/workspace -it stream_dev:latest
# stream_dev只是举个名字的例子, 请指定成自己想要的容器的名字
#已有docker容器可通过下面命令打开
docker run -v $PWD:/workspace -it stream_dev:latestThe workspace directory inside the container is mounted to the host directory where you ran docker run; you can compile the project inside this container. The workspace directory is under the root directory, and changes in it are mapped to the corresponding files in the local directory.
Note: When creating the container, you must be in the parent directory of soc-sdk (the dependency build environment) or above.
2. Package Dependency Files
2.1. Package libsophon
For libsophon_soc_x.y.z_aarch64.tar.gz (where x.y.z is the version number), decompress it.
The file can be found in the SDK package at: SDK-23.09-LTS-SP4\SDK-23.09-LTS-SP4\sophon-img_20241227_105055. You can download the SDK with:
#可通过命令下载SDK包(有libsophon_soc_x.y.z_aarch64.tar.gz文件可跳过此操作)
wget https://sophon-assets.sophon.cn/sophon-prod-s3/drive/24/12/31/10/SDK-23.09-LTS-SP4.zip
# 创建依赖文件的根目录
mkdir -p soc-sdk
# 解压libsophon_soc_x.y.z_aarch64.tar.gz
tar -zxf libsophon_soc_${x.y.z}_aarch64.tar.gz
# 将相关的库目录和头文件目录拷贝到依赖文件根目录下
cp -rf libsophon_soc_${x.y.z}_aarch64/opt/sophon/libsophon-${x.y.z}/lib soc-sdk
cp -rf libsophon_soc_${x.y.z}_aarch64/opt/sophon/libsophon-${x.y.z}/include soc-sdk2.2. Package sophon-ffmpeg and sophon-opencv
For sophon-mw-soc_x.y.z_aarch64.tar.gz (where x.y.z is the version number), decompress it.
The file can be found in the SDK package at: SDK-23.09-LTS-SP4\SDK-23.09-LTS-SP4\sophon-mw_20241223_163201.
# 解压sophon-mw-soc_x.y.z_aarch64.tar.gz
tar -zxf sophon-mw-soc_${x.y.z}_aarch64.tar.gz
# 将ffmpeg和opencv的库目录和头文件目录拷贝到soc-sdk目录下
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-ffmpeg_${x.y.z}/lib soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-ffmpeg_${x.y.z}/include soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-opencv_${x.y.z}/lib soc-sdk
cp -rf sophon-mw-soc_${x.y.z}_aarch64/opt/sophon/sophon-opencv_${x.y.z}/include soc-sdk3. Perform Cross Compilation
bmcv mode
After the cross-compilation environment is set up, use the cross-compilation toolchain to build the executable. Here we use the YOLOv5 C++ example from sophon-demo.
cd cpp/yolov5_opencv
mkdir build && cd build
#请根据实际情况修改-DSDK的路径,需使用绝对路径。
cmake -DTARGET_ARCH=soc -DSDK=/workspace/soc-sdk/ ..
makeAfter the build, a .soc file is generated in the corresponding directory, for example cpp/yolov5_bmcv/yolov5_bmcv.soc.
sail mode
If you use the sophon-sail interface, you must first configure sophon-sail for the SoC environment (i.e. the development board environment). The configuration steps are as follows.
Configure the sail environment.
Via cross-compilation (the example uses WSL), compile a SAIL that includes bmcv, sophon-ffmpeg, sophon-opencv.
If the build platform's libc version differs from the target, enter the Docker environment:
docker run -v $PWD:/workspace -it stream_dev:latest#解压sophon-sail_3.8.0.tar.gz
tar -zvxf sophon-sail_3.8.0.tar.gz
#进入到sophon目录下:cd sophon
mkdir build && cd build
cmake -DBUILD_TYPE=soc -DBUILD_PYSAIL=OFF -DCMAKE_TOOLCHAIN_FILE=../cmake/BM168x_SOC/ToolChain_aarch64_linux.cmake -DLIBSOPHON_BASIC_PATH=../../libsophon_soc_0.5.1-LTS_aarch64/opt/sophon/libsophon-0.5.1/ -DFFMPEG_BASIC_PATH=../../sophon-mw-soc_0.12.0_aarch64/opt/sophon/sophon-ffmpeg_0.12.0/ -DOPENCV_BASIC_PATH=../../sophon-mw-soc_0.12.0_aarch64/opt/sophon/sophon-opencv_0.12.0/ ..
make sailInstall the SAIL dynamic library and header files. The program will automatically create build_soc under the source directory, and the build output will be installed under build_soc:
make installCopy the sophon-sail folder under build_soc to the /opt/sophon directory of the target SOC; it can then be invoked on the SOC.
After the cross-compilation environment is ready, use the cross-compilation toolchain to build the executable.
cd cpp/yolov5_sail
mkdir build && cd build
#请根据实际情况修改-DSDK和-DSAIL_PATH的路径,需使用绝对路径。
cmake -DTARGET_ARCH=soc -DSDK=/path_to_sdk/soc-sdk -DSAIL_PATH=/wrokspace/sophon-sail/build_soc/sophon-sail ..
makeAfter the build, yolov5_sail.soc is generated in the yolov5_sail directory.
Because sophon-sail is moved to /opt/sophon, if you transfer files via SSH you must enter the root account. The root account has no initial password; before using it, run sudo passwd root from the linaro account to set a password.
Most Linux systems disable remote root login by default. Perform the following:
Edit the configuration file:
sudo vim /etc/ssh/sshd_config
#文件中加入
PermitRootLogin yesSave and exit, then restart SSH:
sudo service sshd restartAfter copying the sophon-sail library files to the target SOC according to the tutorial, you also need to set the following environment variable:
echo 'export LD_LIBRARY_PATH=/opt/sophon/sophon-sail/lib/:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc