Загрузка
Prev Tutorial: Cross compilation for ARM based Linux systems
Next Tutorial: Building OpenCV for Tegra with CUDA
| Original author | Kumataro |
| Compatibility | Ubuntu >=23.04 |
| OpenCV >=4.8.0 |
OpenCV may use a lot of 3rdparty libraries for video and image decoding, rendering, acceleration and complex math algorithms. The 3rd party components are found by CMake on the build host cross-compilation allows to build OpenCV for foreign architecture or OS, but we loose that large world of components and have to cross-compile each dependency separately and point to it during OpenCV build.
Debian/Ubuntu MultiArch helps to fix this. It allows to install several foreign architecture libraries on host system and use them during OpenCV dependencies resolution.
Install necessary tools and toolchains for cross-compilation.
sudo apt update -y
sudo apt install -y \
git \
cmake \
pkgconf \
build-essential \
ninja-build \
crossbuild-essential-armhf \
crossbuild-essential-arm64If you want to enable Python 3 wrapper, install these packages too.
sudo apt install -y \
python3-minimal \
python3-numpyIn this tutorial, following working folder structure are used.
/home
+ kmtr - please replace your account name.
+ work
+ opencv - source, cloned from github
+ opencv_contrib - source, cloned from github
+ build4-full_arm64 - artifact(for aarch64 target), created by cmake
+ build4-full_armhf - artifact(for armhf target), created by cmakecd ~
mkdir work
cd work
git clone --depth=1 https://github.com/opencv/opencv.git
git clone --depth=1 https://github.com/opencv/opencv_contrib.gitThese steps are on host.
apt and dpkg are package management systems used in Ubuntu and Debian.
Following are setup steps to use MultiArch.
Execute sudo apt edit-sources to add foreign arch libraries at end of file.
Example 1: arm64 and armv7 for Ubuntu 23.04
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar main restricted
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar-updates main restricted
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar-updates universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar multiverse
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar-updates multiverse
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports lunar-backports main restricted universe multiverse
[=,] :
[=,] :
[=,] :Example 2: arm64 and armv7 for Ubuntu 23.10
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic main restricted
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic-updates main restricted
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic-updates universe
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic multiverse
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic-updates multiverse
deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports mantic-backports main restricted universe multiverse
[=,] :
[=,] :
[=,] :Update apt database to apply new apt sources.
Execute sudo apt update.
sudo apt updateUpdate dpkg settings to support foreign architectures.
Execute sudo dpkg --add-architecture arm64 and/or sudo dpkg --add-architecture armhf.
sudo dpkg --add-architecture arm64
sudo dpkg --add-architecture armhfsudo dpkg --print-architecture shows what is host architecture.
sudo dpkg --print-architecture
amd64And sudo dpkg --print-foreign-architectures shows what foreign architectures are supported.
sudo dpkg --print-foreign-architectures
arm64
armhfWith MultiArch, several shared libraries and pkg-config information for each architectures are stored into /usr/lib.
/usr
+ lib
+ aarch64-linux-gnu - shared libraries for arm64
+ pkgconfig - pkg-config files for arm64 libraries
+ arm-linux-gnueabihf - shared libraries for armhf
+ pkgconfig - pkg-config files for armhf libraries
+ share
+ pkgconfig - pkg-config files(for header files)Confirm to work pkg-config using PKG_CONFIG_PATH, PKG_CONFIG_LIBDIR and PKG_CONFIG_SYSROOT_DIR options.
for aarch64:
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
pkg-config --list-allfor armv7:
PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf \
PKG_CONFIG_SYSROOT_DIR=/ \
pkg-config --list-allFollowing is to compile for target (aarch64) at host (x86-64).
This step is on host.
Install libfreetype-dev, libharfbuzz-dev and FFmpeg packages for target (arm64) into host (x86-64).
sudo apt install -y \
libavcodec-dev:arm64 \
libavformat-dev:arm64 \
libavutil-dev:arm64 \
libswscale-dev:arm64 \
libfreetype-dev:arm64 \
libharfbuzz-dev:arm64If you want to enable Python 3 wrapper, install these packages too.
sudo apt install -y \
libpython3-dev:arm64If succeed, pkg-config can show information about these packages.
For Freetype2 and Harfbuzz:
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
pkg-config freetype2 harfbuzz --cflags --libs
-I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/ -///-- - -For FFmpeg:
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
pkg-config libavcodec libavformat libavutil libswscale --cflags --libs
-I/usr/include/aarch64-linux-gnu -L/usr/lib/aarch64-linux-gnu -lavcodec -lavformat -lavutil -lswscaleThis step is on host.
Execute cmake to make cross-compile configuration for aarch64.
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
cmake -S opencv \
-B build4-full_arm64 \
-DCMAKE_TOOLCHAIN_FILE=/home/kmtr/work/opencv/platforms/linux/aarch64-gnu.toolchain.cmake \
-DOPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules \
-GNinjaIf you want to enable Python 3 wrapper, extra options are needed.
PYTHON3_REALPATH=`realpath /usr/bin/python3`
PYTHON3_BASENAME=`basename ${PYTHON3_REALPATH}`
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu \
PKG_CONFIG_SYSROOT_DIR=/ \
cmake -S opencv \
-B build4-full_arm64 \
-DCMAKE_TOOLCHAIN_FILE=/home/kmtr/work/opencv/platforms/linux/aarch64-gnu.toolchain.cmake \
-DOPENCV_EXTRA_MODULES_PATH=opencv_contrib/modules \
-DPYTHON3_NUMPY_INCLUDE_DIRS="/usr/local/lib/${PYTHON3_BASENAME}/dist-packages/numpy/core/include/" \
-= \
-= \
-= \
-= \
-Following is cmake outputs.
Host is Linux x86_64.Target is Linux aarch64.-- General configuration for OpenCV 4.8.0-dev =====================================
-- Version control: 408730b
--
-- Extra modules:
-- Location (extra): /home/kmtr/work/opencv_contrib/modules
-- Version control (extra): faa5468
--
-- Platform:
-- Timestamp: 2023-12-01T22:02:14Z
-- Host: Linux 6.5.0-13-generic x86_64
-- Target: Linux 1 aarch64
-- CMake: 3.27.4
-- CMake generator: Ninja
-- : ///
-- :
--
-- / :
-- :
-- :
-- :
-- :
-- :
-- ( ): +
-- ( ): +
-- ( ): +
--
-- /++:
-- ?:
-- ++ :
-- ++ : ///---++ ( .)
:
:
--
-- /:
-- :
-- :
-- : (.)
-- : (.)
-- : (.)
-- : (.)
-- :
-- :
-- /: (/.)
--If enabling Python 3 wrapper is succeeded, Python 3: section shows more.
--
-- Python 3:
-- Interpreter: /usr/bin/python3.11 (ver 3.11.6)
-- Libraries: /usr/lib/aarch64-linux-gnu/libpython3.11.so
-- numpy: /usr/local/lib/python3.11/dist-packages/numpy/core/include/ (ver undefined - cannot be probed because of the cross-compilation)
-- install path: lib/python3.11/dist-packages//-
--
-- ( ): ///.
--This step in in host.
Build and install. (This install means only that copying artifacts to install folder.)
cmake --build build4-full_arm64
sudo cmake --install build4-full_arm64Archive artifacts(built libraries and headers) to opencv_arm64.tgz with tar command.
tar czvf opencv_arm64.tgz -C build4-full_arm64/install .And send opencv_arm64.tgz to target.
This step is executed on the target system.
Install dependency run-time libraries for OpenCV/OpenCV contrib libraries at target.
sudo apt install -y \
libavcodec60 \
libavformat60 \
libavutil58 \
libswscale7 \
libfreetype6 \
libharfbuzz0b
sudo ldconfigIf you want to enable Python 3 wrapper, install these packages too.
sudo apt install -y \
python3-minimal \
python3-numpyThis step is on target.
Receive opencv_arm64.tgz from host (generated at Step3), and extract to /usr/local.
sudo tar zxvf opencv_arm64.tgz -C /usr/local
sudo ldconfigYou can use OpenCV libraries same as self-compiling. Following is OpenCV sample code. Compile and run it on target.
Makefile
a.out : main.cpp
g++ main.cpp -o a.out \
-I/usr/local/include/opencv4 \
-lopencv_coremain.cpp
#include <iostream>
#include <opencv2/core.hpp>
int main(void)
{
std::cout << cv::getBuildInformation() << std::endl;
return 0;
}Execute make and run it.
make a.out
./a.outIf you want to enable Python 3 wrapper, execute following command to confirm.
python3 -c "import cv2; print(cv2.getBuildInformation())"Following is to compile for target (armhf) at host (x86-64).
linux-libc-dev:armhf is required.-DENABLE_NEON=ON is needed.sudo apt install -y \
linux-libc-dev:armhf \
libavcodec-dev:armhf \
libavformat-dev:armhf \
libavutil-dev:armhf \
libswscale-dev:armhf \
libfreetype-dev:armhf \
libharfbuzz-dev:armhf
PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/arm-linux-gnueabihf \
PKG_CONFIG_SYSROOT_DIR=/ \
cmake -S opencv \
-B - \
-= \
-=///////-.. \
-=/ \
-
-- -
-- -
. - -/ .Following is cmake outputs.
Host is Linux x86_64.Target is Linux arm.-- General configuration for OpenCV 4.8.0-dev =====================================
-- Version control: 408730b
--
-- Extra modules:
-- Location (extra): /home/kmtr/work/opencv_contrib/modules
-- Version control (extra): faa5468
--
-- Platform:
-- Timestamp: 2023-12-02T03:39:58Z
-- Host: Linux 6.5.0-13-generic x86_64
-- Target: Linux 1 arm
-- CMake: 3.27.4
-- CMake generator: Ninja
-- : ///
-- :
--
-- / :
-- :
-- :
-- :
-- :
--
-- /++:
-- ?:
-- ++ :
-- ++ : ///---++ ( .)
:
:
--
-- /:
-- :
-- :
-- : (.)
-- : (.)
-- : (.)
-- : (.)
-- :
-- :
-- /: (/.)
--(Good) All libraries which are required from freetype modules are installed.
linux-vdso.so.1 (0xABCDEFG01234567)
libopencv_imgproc.so.408 => /usr/local/lib/libopencv_imgproc.so.408 (0xABCDEF001234567)
libfreetype.so.6 => /lib/aarch64-linux-gnu/libfreetype.so.6 (0xABCDEF001234567)
libharfbuzz.so.0 => /lib/aarch64-linux-gnu/libharfbuzz.so.0 (0xABCDEF001234567)
libopencv_core.so.408 => /usr/local/lib/libopencv_core.so.408 (0xABCDEF001234567)
: