5. 交叉编译Qt库(Qt6)

相对于Qt5,QT6进行了一些更广泛的架构更改,如 利用 C++17下一代的QMLQt Quick统一的 2D 和 3D使用CMake 构建 等等, 更多的更改参考下 这里

本章将介绍下使用cmake交叉编译下Qt6,然后在鲁班猫板卡上部署测试。

提示

Qt交叉编译的默认环境:鲁班猫板卡系统Debian10、PC虚拟机系统ubuntu20.04、交叉编译GCC是8.3.0、Qt源码版本6.2.4。

5.1. 配置环境

1、ubunut20.04环境配置:

# 安装相关库和工具等
sudo apt update
sudo apt install libssl-dev gperf pkg-config git bison ninja-build
sudo apt install make build-essential libclang-dev \
   libfontconfig1-dev libfreetype6-dev libx11-dev libx11-xcb-dev \
   libxext-dev libxfixes-dev libxi-dev libxrender-dev libxcb1-dev libxcb-glx0-dev \
   libxcb-keysyms1-dev libxcb-image0-dev libxcb-shm0-dev libxcb-icccm4-dev libxcb-sync-dev \
   libxcb-xfixes0-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0-dev \
   libxcb-util-dev libxcb-xinerama0-dev libxcb-xkb-dev libxkbcommon-dev libxkbcommon-x11-dev \
   libatspi2.0-dev libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

获取交叉编译器:

# 使用aarch64-linux-gnu 8.3.0交叉编译器
wget https://armkeil.blob.core.windows.net/developer/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz

# 解压文件,到一个目录下
tar -xf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz

2、鲁班猫板卡上环境安装(测试系统是Debian10 带桌面):

# 安装一些库和工具等,按自己需要安装
sudo apt update

# x11相关
sudo apt-get install -y libx11-dev freetds-dev libsqlite0-dev libpq-dev libiodbc2-dev firebird-dev \
   libxcb1 libxcb1-dev libx11-xcb1 libx11-xcb-dev  \
   libxcb-keysyms1 libxcb-keysyms1-dev libxcb-image0 libxcb-image0-dev libxcb-shm0 libxcb-shm0-dev \
   libxcb-icccm4 libxcb-icccm4-dev libxcb-sync1 libxcb-sync-dev libxcb-render-util0 \
   libxcb-render-util0-dev libxcb-xfixes0-dev libxrender-dev libxcb-shape0-dev libxcb-randr0-dev \
   libxcb-glx0-dev libxi-dev libdrm-dev libxcb-xinerama0 libxcb-xinerama0-dev libatspi2.0-dev \
   libxcursor-dev libxcomposite-dev libxdamage-dev libxss-dev libxtst-dev libpci-dev libcap-dev \
   libxrandr-dev libdirectfb-dev libaudio-dev libxkbcommon-x11-dev

# gst 1.0相关
sudo apt-get install -y libgstreamer1.0-0 gstreamer1.0-plugins-base \
   gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly \
   gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools libunwind-dev

# 其他等等
sudo apt-get install -y libfreetype6-dev libicu-dev libsqlite3-dev libasound2-dev libnss3-dev \
   libxss-dev libxtst-dev libpci-dev libcap-dev libsrtp0-dev libxrandr-dev libdirectfb-dev libaudio-dev \
   libavcodec-dev libavformat-dev libswscale-dev libts-dev libfontconfig1-dev

sudo apt-get install -y libssl-dev libdbus-1-dev libglib2-dev libegl1-mesa-dev \
   libgbm-dev libgles2-mesa-dev libgles2-mesa rsyslog  libjpeg-dev

3、ubuntu20.04上建立sysroot目录(使用rsync命令):

# 在虚拟机上,创建sysroot目录
mkdir -p ~/sysroot/usr
mkdir -p ~/sysroot/lib
cd ~
rsync -avz --rsync-path="sudo rsync" --delete cat@192.168.103.102:/lib sysroot
rsync -avz --rsync-path="sudo rsync" --delete cat@192.168.103.102:/usr/include sysroot/usr
rsync -avz --rsync-path="sudo rsync" --delete cat@192.168.103.102:/usr/lib sysroot/usr

# 也可以直接复制我们提供的文件到类似目录下

5.2. 安装CMake

Qt6主要推荐使用CMake来构建工程,qmake也是支持,但不是主要支持工具。交叉编译需要3.16版本以上的cmake(ubuntu20.04默认安装3.16), 我们这安装最新的CMake,需要简单拉取源码构建安装下:

git clone https://github.com/Kitware/CMake.git
cd CMake
./bootstrap
make && sudo make install

安装成功查看安装的CMake:

# 这里测试安装的是3.26版本
llh@llh-VirtualBox:~$ cmake --version
cmake version 3.26.20230328-gf4b8176

CMake suite maintained and supported by Kitware (kitware.com/cmake).

5.3. 编译主机Qt6

交叉编译Qt6时,需要主机Qt6的安装路径,交叉编译时使用 -DQT_HOST_PATH 指定,需要注意Qt6的版本应该一致,这里就单独编译下主机Qt6。 获取Qt6源码(6.2.4版本):

mkdir ~/qt && cd qt
wget https://download.qt.io/archive/qt/6.2/6.2.4/single/qt-everywhere-src-6.2.4.tar.xz

# 在qt目录下创建一个安装目录qt6Host和一个构建目录qt6HostBuild
# 解压前面获取的压缩包到构建目录下
mkdir qt6Host
mkdir qt6HostBuild && cd qt6HostBuild
tar -xvf ../qt-everywhere-src-6.2.4.tar.xz

提示

国内下载Qt6源码可能很慢,可以去镜像网站:https://mirrors.tuna.tsinghua.edu.cn/qt/archive/qt/6.2/ 手动下载。

配置测试Qt:

cmake -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DINPUT_opengl=es2 -DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/home/llh/qt/qt6Host

正常输出:

#..............前面省略...................#
   libxml2 and libxslt .................. yes
   lcms2 ................................ no
   png .................................. yes
   jpeg ................................. yes
   harfbuzz ............................. no
   freetype ............................. yes
   libpci ............................... no

Note: Hunspell disabled. Spelling correction will not be available.

WARNING: QtWebEngine won't be built. Could not find all necessary libraries for qpa-xcb support. X11:YES LIBDRM:YES
XCOMPOSITE:NO XCURSOR:NO XRANDR:NO XI:YES XPROTO:YES XSHMFENCE:NO XTST:YES

Qt is now configured for building. Just run 'cmake --build . --parallel'

Once everything is built, you must run 'cmake --install .'
Qt will be installed into '/home/llh/qt/qt6Host'

To configure and build other Qt modules, you can use the following convenience script:
      /home/llh/qt/qt6Host/bin/qt-configure-module

If reconfiguration fails for some reason, try to remove 'CMakeCache.txt' from the build directory

-- Configuring done (90.5s)
-- Generating done (3.2s)
-- Build files have been written to: /home/llh/qt/qt6hostbuild/qt-everywhere-src-6.2.4

可能有些库检测没有,看自己使用情况安装相关库。

编译安装Qt:

cmake --build . --parallel 4
cmake --install .

编译完成后,会下载文件到/home/llh/qt/qt6Host目录中,具体根据前面配置的-DCMAKE_INSTALL_PREFIX目录。

5.4. 交叉编译Qt6

创建cmake toolcain文件

使用cmake交叉编译Qt,需要一个toolcain.cmake文件,在文件中指定编译器路径,sysroot路径,编译链接参数等。 示例创建一个配置文件lubancat_toolchain.cmake:

lubancat_toolchain.cmake
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 这里设置cmake支持的最小版本
cmake_minimum_required(VERSION 3.18)
include_guard(GLOBAL)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

# 指定sysroot目录
set(TARGET_SYSROOT /home/llh/sysroot)

# 指定前面获取的交叉编译器路径
set(CROSS_COMPILER /opt/gcc-aarch64-linux-gnu-8.3.0/bin/aarch64-linux-gnu)

set(CMAKE_SYSROOT ${TARGET_SYSROOT})

set(CMAKE_C_COMPILER ${CROSS_COMPILER}-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER}-g++)

set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu)

# 库路径
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/lib -L${CMAKE_SYSROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE}  -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib -I${TARGET_SYSROOT}/usr/include -I${TARGET_SYSROOT}/usr/include/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")

# pkg config路径
set(ENV{PKG_CONFIG_PATH} ${TARGET_SYSROOT}/usr/lib/pkgconfig:${TARGET_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig:${TARGET_SYSROOT}/usr/share/pkgconfig/)
set(ENV{PKG_CONFIG_LIBDIR} ${TARGET_SYSROOT}/usr/lib/pkgconfig:${TARGET_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig:${TARGET_SYSROOT}/usr/share/pkgconfig/)
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})

set(QT_COMPILER_FLAGS "-march=armv8-a")
set(QT_COMPILER_FLAGS_RELEASE "-O2 -pipe")
set(QT_LINKER_FLAGS "-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-fuse-ld=gold -ldbus-1")

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

include(CMakeInitializeConfigs)

function(cmake_initialize_per_config_variable _PREFIX _DOCSTRING)
if (_PREFIX MATCHES "CMAKE_(C|CXX|ASM)_FLAGS")
   set(CMAKE_${CMAKE_MATCH_1}_FLAGS_INIT "${QT_COMPILER_FLAGS}")

   foreach (config DEBUG RELEASE MINSIZEREL RELWITHDEBINFO)
      if (DEFINED QT_COMPILER_FLAGS_${config})
      set(CMAKE_${CMAKE_MATCH_1}_FLAGS_${config}_INIT "${QT_COMPILER_FLAGS_${config}}")
      endif()
   endforeach()
endif()

if (_PREFIX MATCHES "CMAKE_(SHARED|MODULE|EXE)_LINKER_FLAGS")
   foreach (config SHARED MODULE EXE)
      set(CMAKE_${config}_LINKER_FLAGS_INIT "${QT_LINKER_FLAGS}")
   endforeach()
endif()

_cmake_initialize_per_config_variable(${ARGV})
endfunction()

创建lubancat设备文件

创建一个配置文件,在qtbase/mkspecs/devices/目录下新建一个配置文件,具体参考下前面Qt5编译。添加好配置文件,也可以使用configure编译,教程使用cmake方式编译。

qtbase/mkspecs/devices/linux-lubancat-g++/qmake.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#
# qmake configuration for the LubanCat running Linux for debian10
#

include(../common/linux_device_pre.conf)

QMAKE_LIBS_EGL         += -lEGL -lmali
QMAKE_LIBS_OPENGL_ES2  += -lGLESv2 -lEGL -lmali
QMAKE_CFLAGS            = -march=armv8-a
QMAKE_CXXFLAGS          = $$QMAKE_CFLAGS
QMAKE_LFLAGS += -static-libstdc++

QMAKE_INCDIR_POST +=     $$[QT_SYSROOT]/usr/include     $$[QT_SYSROOT]/usr/include/aarch64-linux-gnu/
QMAKE_LIBDIR_POST +=     $$[QT_SYSROOT]/usr/lib     $$[QT_SYSROOT]/lib/$${GCC_MACHINE_DUMP}          $$[QT_SYSROOT]/usr/lib/$${GCC_MACHINE_DUMP}
QMAKE_RPATHLINKDIR_POST +=     $$[QT_SYSROOT]/usr/lib     $$[QT_SYSROOT]/usr/lib/$${GCC_MACHINE_DUMP}     $$[QT_SYSROOT]/lib/$${GCC_MACHINE_DUMP}

DISTRO_OPTS += aarch64
DISTRO_OPTS += deb-multi-arch

include(../common/linux_arm_device_post.conf)
load(qt_config)

编译配置

#创建目录,解压前面获取的源码
mkdir -p ~/qt/qt6-cross
cd ~/qt/qt6-cross/
tar -xf ../qt-everywhere-src-6.2.4.tar.xz

#创建构建目录
mkdir build && cd build

# 编译配置
cmake ../qt-everywhere-src-6.2.4/ -GNinja -DCMAKE_BUILD_TYPE=Release  -DINPUT_opengl=es2 \
-DQT_BUILD_EXAMPLES=OFF -DQT_BUILD_TESTS=OFF -DQT_HOST_PATH=$HOME/qt/qt6Host -DCMAKE_STAGING_PREFIX=$HOME/sysroot/opt/prefix \
-DCMAKE_INSTALL_PREFIX=$HOME/sysroot/opt/prefix -DCMAKE_TOOLCHAIN_FILE=$HOME/qt/lubancat_toolchain.cmake  \
-DQT_QMAKE_TARGET_MKSPEC=devices/linux-lubancat-g++ -DQT_FEATURE_xcb=ON -DFEATURE_xcb_xlib=ON \
-DQT_FEATURE_xlib=ON -DFEATURE_qtwebengine_build=OFF

正常配置输出:

Configure summary:

Building for: linux-g++ (arm64, CPU features: neon)
Compiler: gcc 8.3.0
Build options:
Mode ................................... release
Optimize release build for size ........ no
Fully optimize release builds (-O3) .... no
Building shared libraries .............. yes
Using C standard ....................... C11
Using C++ standard ..................... C++17
Using ccache ........................... no
Using new DTAGS ........................ yes
Relocatable ............................ yes
Using precompiled headers .............. yes
Using LTCG ............................. no
Target compiler supports:
   Intrinsics without compiler architecture option  yes
   Extensions ........................... NEON
Sanitizers:
   Addresses ............................ no
   Threads .............................. no
   Memory ............................... no
   Fuzzer (instrumentation only) ........ no
   Undefined ............................ no
Build parts ............................ libs
Qt modules and options:
Qt Concurrent .......................... yes
Qt D-Bus ............................... yes
Qt D-Bus directly linked to libdbus .... yes
Qt Gui ................................. yes
Qt Network ............................. yes
Qt PrintSupport ........................ yes
Qt Sql ................................. yes
Qt Testlib ............................. yes
Qt Widgets ............................. yes
Qt Xml ................................. yes
Support enabled for:
Using pkg-config ....................... yes
udev ................................... yes
Using system zlib ...................... yes
Zstandard support ...................... yes
Thread support ......................... yes
Common build options:
Linker can resolve circular dependencies  yes
Qt Core:
backtrace .............................. yes
DoubleConversion ....................... yes
   Using system DoubleConversion ........ yes
GLib ................................... yes
ICU .................................... yes
Using system libb2 ..................... no
Built-in copy of the MIME database ..... yes
cpp/winrt base ......................... no
Tracing backend ........................ <none>
Logging backends:
   journald ............................. no
   syslog ............................... no
   slog2 ................................ no
PCRE2 .................................. yes
   Using system PCRE2 ................... yes
CLONE_PIDFD support in forkfd .......... yes
Qt Sql:
SQL item models ........................ yes
Qt Network:
getifaddrs() ........................... yes
IPv6 ifname ............................ yes
libproxy ............................... no
Linux AF_NETLINK ....................... yes
OpenSSL ................................ yes
   Qt directly linked to OpenSSL ........ no
OpenSSL 1.1 ............................ yes
DTLS ................................... yes
OCSP-stapling .......................... yes
SCTP ................................... no
Use system proxies ..................... yes
GSSAPI ................................. yes
Brotli Decompression Support ........... yes
Qt Gui:
Accessibility .......................... yes
FreeType ............................... yes
   Using system FreeType ................ yes
HarfBuzz ............................... yes
   Using system HarfBuzz ................ no
Fontconfig ............................. yes
Image formats:
   GIF .................................. yes
   ICO .................................. yes
   JPEG ................................. yes
      Using system libjpeg ............... yes
   PNG .................................. yes
      Using system libpng ................ yes
Text formats:
   HtmlParser ........................... yes
   CssParser ............................ yes
   OdfWriter ............................ yes
   MarkdownReader ....................... yes
      Using system libmd4c ............... no
   MarkdownWriter ....................... yes
EGL .................................... yes
OpenVG ................................. no
OpenGL:
   Desktop OpenGL ....................... no
   OpenGL ES 2.0 ........................ yes
   OpenGL ES 3.0 ........................ yes
   OpenGL ES 3.1 ........................ yes
   OpenGL ES 3.2 ........................ yes
Vulkan ................................. yes
Session Management ..................... yes
Features used by QPA backends:
evdev .................................. yes
libinput ............................... yes
INTEGRITY HID .......................... no
mtdev .................................. yes
tslib .................................. yes
xkbcommon .............................. yes
X11 specific:
   xlib ................................. yes
   XCB Xlib ............................. yes
   EGL on X11 ........................... yes
   xkbcommon-x11 ........................ yes
   xcb-sm ............................... yes
QPA backends:
DirectFB ............................... no
EGLFS .................................. yes
EGLFS details:
   EGLFS OpenWFD ........................ no
   EGLFS i.Mx6 .......................... no
   EGLFS i.Mx6 Wayland .................. no
   EGLFS RCAR ........................... no
   eglfs_egldevice ...................... yes
   eglfs_gbm ............................ yes
   EGLFS VSP2 ........................... no
   EGLFS Mali ........................... yes
   EGLFS Raspberry Pi ................... no
   EGLFS X11 ............................ yes
LinuxFB ................................ yes
VNC .................................... yes
VK_KHR_display ......................... yes
QNX:
   lgmon ................................ no
   IMF .................................. no
XCB:
   Using system-provided xcb-xinput ..... no
   GL integrations:
      GLX Plugin ......................... no
      XCB GLX .......................... no
      EGL-X11 Plugin ..................... yes
   #..............中间省略....................#
   minizip .............................. no
   libevent ............................. no
   libxml2 and libxslt .................. yes
   lcms2 ................................ no
   png .................................. yes
   jpeg ................................. yes
   harfbuzz ............................. no
   freetype ............................. yes
   libpci ............................... yes

Note: Hunspell disabled. Spelling correction will not be available.

WARNING: QDoc will not be compiled, probably because libclang could not be located. This means that you cannot build the Qt documentation.
Either set CMAKE_PREFIX_PATH or LLVM_INSTALL_DIR to the location of your llvm installation.
On Linux systems, you may be able to install libclang by installing the libclang-dev or libclang-devel package, depending on your distribution.
On macOS, you can use Homebrew's llvm package.
You will also need to set the FEATURE_clang CMake variable to ON to re-evaluate this check.
WARNING: Clang-based lupdate parser will not be available. LLVM and Clang C++ libraries have not been found.
You will need to set the FEATURE_clangcpp CMake variable to ON to re-evaluate this check.

Qt is now configured for building. Just run 'cmake --build . --parallel'

Once everything is built, you must run 'cmake --install .'
Qt will be installed into '/home/llh/sysroot/opt'

To configure and build other Qt modules, you can use the following convenience script:
      /home/llh/sysroot/opt/prefix/bin/qt-configure-module

If reconfiguration fails for some reason, try to remove 'CMakeCache.txt' from the build directory

-- Configuring done (106.7s)
-- Generating done (3.1s)
-- Build files have been written to: /home/llh/qt/qt6-cross/build

命令中配置 -DQT_HOST_PATH=$HOME/qt/qt6Host 该目录是前面编译主机Qt6的安装目录, -DCMAKE_TOOLCHAIN_FILE 是目标的Cmake toolcain文件,需要根据自己实际目录修改。

我们命令中配置 -DINPUT_opengl=es2 ,开启了opengl es,在config.summary主要关注EGLFS系列是否yes。如果是no,一般会提示 You might need to modify the include and library search paths by editing QMAKE_INCDIR_OPENGL[_ES2] ..., 这个时候去看构建目录下CMakeFiles/CMakeConfigureLog文件,查看具体的报错(可能是找不到库或者其他问题)。

命令中默认关闭了qtwebengine的编译,通过设置 -DFEATURE_qtwebengine_build=OFF。 另外,除了使用cmake编译,也支持使用configure进行编译。

交叉编译Qt6源码

在编译配置检测后,执行下面命令编译安装Qt:

# 编译
cmake --build . --parallel 4
# 安装
cmake --install .

安装完成后,会在安装目录下生成文件:

llh@llh-VirtualBox:~/sysroot/opt/prefix$ ls
bin  include  libexec  modules      plugins  translations
doc  lib      mkspecs  phrasebooks  qml

具体下载安装目录,根据前面配置的-DCMAKE_INSTALL_PREFIX。

5.5. 部署测试

同步文件到鲁班猫板卡(这是用rsync):

rsync -avz --rsync-path="sudo rsync" ~/sysroot/opt/prefix cat@192.168.103.102:/opt

提示

rsync命令中是 ~/sysroot/opt/prefix,表示同步prefix文件夹到板卡的/opt,如果是 ~/sysroot/opt/prefix/,将会同步prefix文件夹下的文件到板卡的/opt。

这里测试直接用命令行,使用Qt Creator需要自己配置下编译套件(kits),可以参考下前面章节。 进入解压的qt-everywhere-src-6.2.4.tar.xz目录,复制qtbase/examples/opengl/hellogl2例程到一个新的目录下,这是一个opengl例程:

# 创建一个qt6测试目录
cd ~
mkdir qt6_test && cd qt6_test

# 根据自己实际目录
cp -a ~/qt/qt6-cross/qt-everywhere-src-6.2.4/qtbase/examples/opengl/hellogl2 .
cd hellogl2

# 打开CMakeLists.txt,设置下库路径和交叉编译器路径(根据自己实际目录设置)等,添加下面语句:
set(TARGET_SYSROOT /home/llh/sysroot)
set(CROSS_COMPILER /opt/gcc-aarch64-linux-gnu-8.3.0/bin/aarch64-linux-gnu)
set(CMAKE_SYSROOT ${TARGET_SYSROOT})

set(CMAKE_C_COMPILER ${CROSS_COMPILER}-gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILER}-g++)
set(CMAKE_LIBRARY_ARCHITECTURE aarch64-linux-gnu)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/lib -L${CMAKE_SYSROOT}/lib/${CMAKE_LIBRARY_ARCHITECTURE}  -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib -I${TARGET_SYSROOT}/usr/include -I${TARGET_SYSROOT}/usr/include/${CMAKE_LIBRARY_ARCHITECTURE}")

set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")

# 编译配置,使用我们前面交叉编译出的qt-cmake工具,工具目录根据自己实际安装目录,在hellogl2目录下执行:
/home/llh/sysroot/opt/prefix/bin/qt-cmake .

# 输出
-- The CXX compiler identification is GNU 8.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/gcc-aarch64-linux-gnu-8.3.0/bin/aarch64-linux-gnu-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Performing Test HAVE_STDATOMIC
-- Performing Test HAVE_STDATOMIC - Success
-- Found WrapAtomic: TRUE
-- Performing Test HAVE_EGL
-- Performing Test HAVE_EGL - Success
-- Found EGL: /home/llh/sysroot/usr/include (found version "1.5")
-- Performing Test HAVE_GLESv2
-- Performing Test HAVE_GLESv2 - Success
-- Found GLESv2: /home/llh/sysroot/usr/include
-- Found XKB: /home/llh/sysroot/usr/lib/aarch64-linux-gnu/libxkbcommon.so (found suitable version "0.8.2", minimum required is "0.5.0")
-- Found WrapVulkanHeaders: /home/llh/sysroot/usr/include
-- Configuring done (2.8s)
-- Generating done (0.0s)
-- Build files have been written to: /home/llh/qt6_test/hellogl2

# 然后编译执行命令:
cmake --build .

# 输出:
[ 12%] Automatic MOC and UIC for target hellogl2
[ 12%] Built target hellogl2_autogen
[ 25%] Building CXX object CMakeFiles/hellogl2.dir/hellogl2_autogen/mocs_compilation.cpp.o
[ 37%] Building CXX object CMakeFiles/hellogl2.dir/glwidget.cpp.o
[ 50%] Building CXX object CMakeFiles/hellogl2.dir/logo.cpp.o
[ 62%] Building CXX object CMakeFiles/hellogl2.dir/main.cpp.o
[ 75%] Building CXX object CMakeFiles/hellogl2.dir/mainwindow.cpp.o
[ 87%] Building CXX object CMakeFiles/hellogl2.dir/window.cpp.o
[100%] Linking CXX executable hellogl2
[100%] Built target hellogl2

# 查看生成的可执行文件:
file hellogl2
hellogl2: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, not stripped

复制可执行文件到鲁班猫板卡:

scp hellogl2 cat@192.168.103.102:/home/cat

在鲁班猫板卡上,这里简单测试,导出下库环境,然后执行程序(详细板卡环境配置参考下后面 部署章节):

cat@lubancat:~$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/prefix/lib

# 查看可执行文件链接库
cat@lubancat:~$ ldd hellogl2
      linux-vdso.so.1 (0x0000007f93118000)
      libQt6OpenGLWidgets.so.6 => /opt/prefix/lib/libQt6OpenGLWidgets.so.6 (0x0000007f930c9000)
      libQt6OpenGL.so.6 => /opt/prefix/lib/libQt6OpenGL.so.6 (0x0000007f93036000)
      libQt6Widgets.so.6 => /opt/prefix/lib/libQt6Widgets.so.6 (0x0000007f928f9000)
      libQt6Gui.so.6 => /opt/prefix/lib/libQt6Gui.so.6 (0x0000007f92033000)
      libQt6Core.so.6 => /opt/prefix/lib/libQt6Core.so.6 (0x0000007f91a82000)
      libstdc++.so.6 => /usr/lib/aarch64-linux-gnu/libstdc++.so.6 (0x0000007f918cc000)
      libm.so.6 => /lib/aarch64-linux-gnu/libm.so.6 (0x0000007f9180f000)
      libgcc_s.so.1 => /lib/aarch64-linux-gnu/libgcc_s.so.1 (0x0000007f917eb000)
      libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007f9167a000)
      libGLESv2.so.2 => /usr/lib/aarch64-linux-gnu/mali/libGLESv2.so.2 (0x0000007f91668000)
      ......   # 后面省略

# 执行程序,这里是测试使用xcb,交叉编译qt时也默认编译了eglfs fb等插件
cat@lubancat:~$ ./hellogl2 -platform xcb
qt_cross_compiling12.png