QT交叉编译
2025-06-30
这是在Ubuntu20上交叉编译Qt 5.15.2,并将其安装在Raspberry Pi CM4 上的指南。
随着软件和硬件的每次更新,一些文章的指引似乎都有些过时,或者不全,或者不准确。
因此,这里提供一个完整的流程,并对关键部分进行了必要的说明。
相比其他文档,本文档升级的交叉编译器版本,及对64位QT的支持。
准备工作
不同硬件、不同版本、不同架构、不能混用!
不同硬件、不同版本、不同架构、不能混用!
不同硬件、不同版本、不同架构、不能混用!
这是很多小伙伴踩坑的地方,比如参照最新的bookworm,或buster文档,对bullseye版本操作,导致奇怪的问题。
请务必根据自己的硬件、软件版本、架构等情况进行调整,以下是笔者的配置。
所以当你遇到问题时,请务必先检查文档是否与你使用的各个版本一致。
Raspberry Pi OS是基于debian的,所以不同的Raspberry Pi OS对应debian版本如下。
| Raspberry Pi OS | debian |
|---|---|
| 最新 | 12 (bookworm) |
| 本文 | 11 (bullseye) |
| 旧版 | 10 (buster) |
硬件
- 主机:E5-2695 v4 * 2 + 32 GB RAM
- 目标:Raspberry Pi CM4 微雪电子 2GB SRAM 8GB EMMC版本
软件
- 主机:Ubuntu 20.04 LTS 64位(在Windows 10中的VMWare Player中运行)
- 目标:Raspberry Pi OS (32位或64位的
bullseyewith desktop)
树莓派CM4
选择其中一个架构进行烧录,不同的架构,部分命令有所区别,请注意分辨。
32位镜像:2025-05-06-raspios-bullseye-armhf.img.xz 大小: 948306000 字节 : 904 MiB 64位镜像:·2025-05-06-raspios-bullseye-arm64.img.xz 大小: 892209344 字节 : 850 MiB
这里假设已经完成一些便捷的配置,如:SSH,VNC服务等。
注意,一些文档在 buster 或之前的版本要求开启KMS,而在 bullseye 或之后的版本不需要。
增加源码包的支持。
32位架构:
编辑 `/etc/apt/sources.list` 文件,取消对deb-src的注释
deb http://raspbian.raspberrypi.org/raspbian/ bullseye main contrib non-free rpi
# Uncomment deb-src lines below then 'apt-get update' to enable 'apt-get source'
deb-src http://raspbian.raspberrypi.org/raspbian/ bullseye main contrib non-free rpi
64位架构:
编辑 `/etc/apt/sources.list` 文件,取消对deb-src的注释
deb http://deb.debian.org/debian bullseye main contrib non-free
deb http://security.debian.org/debian-security bullseye-security main contrib non-free
deb http://deb.debian.org/debian bullseye-updates main contrib non-free
# Uncomment deb-src lines below then 'apt-get update' to enable 'apt-get source'
deb-src http://deb.debian.org/debian bullseye main contrib non-free
deb-src http://security.debian.org/debian-security bullseye-security main contrib non-free
deb-src http://deb.debian.org/debian bullseye-updates main contrib non-free
32位和64位架构,都需要编辑 `/etc/apt/sources.list.d/raspi.list` 文件,取消对deb-src的注释
deb http://archive.raspberrypi.org/debian/ bullseye main
# Uncomment deb-src lines below then 'apt-get update' to enable 'apt-get source'
deb-src http://archive.raspberrypi.org/debian/ bullseye main目前官方的源已经很快了,如果一定要更换,请参考如下内容,注意32位和64位的区别。
# 对于32位系统,编辑 `/etc/apt/sources.list` 文件,删除原文件所有内容,用以下内容取代
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ bullseye main non-free contrib rpi
# 对于64位系统 编辑 `/etc/apt/sources.list` 文件,用以下内容取代的:
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# 对于两个架构,编辑 `/etc/apt/sources.list.d/raspi.list` 文件,删除原文件所有内容,用以下内容取代:
deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bullseye main然后使用以下命令更新系统并安装所需的Qt和开发包
sudo apt-get update
sudo apt-get upgrade
sudo apt-get build-dep qt5-qmake libqt5gui5 libqt5webengine-data libqt5webkit5
sudo apt-get install libudev-dev libinput-dev libts-dev libxcb-xinerama0-dev libxcb-xinerama0 gdbserver注意build-dep和install的区别。
| 命令 | 功能 | 安装内容 | 典型用途 |
|---|---|---|---|
| apt-get install | 安装预编译的二进制软件包 | 可直接运行的软件 | 普通软件安装 |
| apt-get build-dep | 安装软件编译依赖 | 源代码编译所需的库和工具 | 从源代码编译软件 |
创建一个qt5.15的安装目录,注意修改替换pi为你的用户名
sudo mkdir /usr/local/qt5.15
sudo chown -R pi:pi /usr/local/qt5.15ubuntu宿主机
首先,更新Ubuntu20并安装一些必需的库。如果你用的时ubuntu22 或更高版本,以下内容不适用。
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc git bison python gperf pkg-config gdb-multiarch
sudo apt install build-essential
其他的一些工具
sudo apt-get install open-vm-tools open-vm-tools-desktop
sudo apt-get install openssh-server net-tools vim fcitx fcitx-googlepinyin对于所需的文件,我在/opt 下创建以下目录结构
sudo mkdir /opt/raspi
sudo mkdir /opt/raspi/build
sudo mkdir /opt/raspi/tools
sudo mkdir /opt/raspi/sysroot
sudo mkdir /opt/raspi/sysroot/usr
sudo chown -R 1000:1000 /opt/raspi
cd /opt/raspi下载Qt资源,或通过其他方式导入,并将其解压缩到raspi目录中
cd /opt/raspi
wget http://download.qt.io/archive/qt/5.15/5.15.2/single/qt-everywhere-src-5.15.2.tar.xz
tar xfv qt-everywhere-src-5.15.2.tar.xz现在我们需要稍微修改一下mkspec文件,以便我们可以在编译器中使用它。为此,请运行以下命令
cd /opt/raspi
cp -R qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabi-g++ qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabihf-g++
sed -i -e 's/arm-linux-gnueabi-/arm-linux-gnueabihf-/g' qt-everywhere-src-5.15.2/qtbase/mkspecs/linux-arm-gnueabihf-g++/qmake.conf
64位架构 需要额外的以下修改
cp -R qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rasp-pi4-v3d-g++/ qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rasp-pi4-64-v3d-g++/
sed -i -e 's/-mfpu=crypto-neon-fp-armv8//g' qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rasp-pi4-64-v3d-g++/qmake.conf
sed -i -e 's/hard-float/aarch64/g' qt-everywhere-src-5.15.2/qtbase/mkspecs/devices/linux-rasp-pi4-64-v3d-g++/qmake.conf交叉编译器没有选择 gcc-linaro-7.4.1 同时官方tools 也介绍 These toolchains are deprecated.
根据官方推荐,安装32位和64位架构的交叉编译器
sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu将树莓派原始库,通过rsync将其复制到宿主机的sysroot目录中,注意将用户及IP地址替换为你的实际值
cd /opt/raspi/
rsync -avz --rsync-path="sudo rsync" --delete pi@192.168.1.134:/lib sysroot/
rsync -avz --rsync-path="sudo rsync" --delete pi@192.168.1.134:/usr/include sysroot/usr/
rsync -avz --rsync-path="sudo rsync" --delete pi@192.168.1.134:/usr/lib sysroot/usr/
rsync -avz --rsync-path="sudo rsync" --delete pi@192.168.1.134:/usr/share sysroot/usr/在buster版本之前,需要复制/opt/vc,在bullseye版本之后,不需要。
rsync -avz --rsync-path="sudo rsync" --delete pi@192.168.1.134:/opt/vc sysroot/opt/
清理rsync复制的符号链接,指向正确的原始文件。参考其他文章下载Python脚本,并执行它。
wget https://raw.githubusercontent.com/riscv/riscv-poky/master/scripts/sysroot-relativelinks.py
sudo chmod +x sysroot-relativelinks.py
./sysroot-relativelinks.py sysroot现在我们可以配置构建并编译它
cd /opt/raspi/build
32位架构
../qt-everywhere-src-5.15.2/configure -release -opengl es2 -eglfs -kms -no-feature-eglfs_brcm -device linux-rasp-pi4-v3d-g++ -device-option CROSS_COMPILE=arm-linux-gnueabihf- -sysroot /opt/raspi/sysroot -prefix /usr/local/qt5.15 -extprefix /opt/raspi/qt5.15 -opensource -confirm-license -skip qtscript -skip qtwayland -skip qtwebengine -nomake tests -make libs -pkg-config -no-use-gold-linker -v -recheck
64位架构
../qt-everywhere-src-5.15.2/configure -release -opengl es2 -eglfs -kms -no-feature-eglfs_brcm -device linux-rasp-pi4-64-v3d-g++ -device-option CROSS_COMPILE=aarch64-linux-gnu- -sysroot /opt/raspi/sysroot -prefix /usr/local/qt5.15 -extprefix /opt/raspi/qt5.15 -opensource -confirm-license -skip qtscript -skip qtwayland -skip qtwebengine -nomake tests -make libs -pkg-config -no-use-gold-linker -v -recheck对比buster版本,这里的配置 -mks 与 -no-feature-eglfs_brcm 是新增的。
大致的原因新版系统默认不再使用 Broadcom 的专有 VideoCore 驱动,而是改用 标准 DRM/KMS(Mesa)驱动。
几分钟后,脚本应该完成,并且应该设置或不设置以下EGLFS条件
QPA backends:
DirectFB ............................... no
EGLFS .................................. yes [SHOULD BE YES]
EGLFS details:
EGLFS OpenWFD ........................ no
EGLFS i.Mx6 .......................... no
EGLFS i.Mx6 Wayland .................. no
EGLFS RCAR ........................... no
EGLFS EGLDevice ...................... yes [SHOULD BE YES]
EGLFS GBM ............................ yes
EGLFS VSP2 ........................... no
EGLFS Mali ........................... no
EGLFS Raspberry Pi ................... no [SHOULD BE NO]
EGLFS X11 ............................ yes
LinuxFB ................................ yes
VNC .................................... yes
XCB:
Using system-provided xcb-xinput ..... yes
Native painting (experimental) ....... no
GL integrations:
GLX Plugin ......................... no
EGL-X11 Plugin ..................... yes完整输出
Building on: linux-g++ (x86_64, CPU features: mmx sse sse2)
Building for: devices/linux-rasp-pi4-64-v3d-g++ (arm64, CPU features: cx16 neon)
Target compiler: gcc 9.4.0
Configuration: cross_compile compile_examples enable_new_dtags largefile neon precompile_header shared shared rpath release c++11 c++14 c++17 c++1z concurrent dbus reduce_exports stl
Build options:
Mode ................................... release
Optimize release build for size ........ 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:
NEON ................................. yes
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 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
Qt Core:
DoubleConversion ....................... yes
Using system DoubleConversion ........ yes
GLib ................................... yes
iconv .................................. no
ICU .................................... yes
Built-in copy of the MIME database ..... yes
Tracing backend ........................ <none>
Logging backends:
journald ............................. no
syslog ............................... no
slog2 ................................ no
PCRE2 .................................. yes
Using system PCRE2 ................... 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
Qt Gui:
Accessibility .......................... yes
FreeType ............................... yes
Using system FreeType ................ yes
HarfBuzz ............................... yes
Using system HarfBuzz ................ yes
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 ............... yes
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
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 ........................... no
EGLFS Raspberry Pi ................... no
EGLFS X11 ............................ yes
LinuxFB ................................ yes
VNC .................................... yes
XCB:
Using system-provided xcb-xinput ..... yes
Native painting (experimental) ....... no
GL integrations:
GLX Plugin ......................... no
EGL-X11 Plugin ..................... yes
Qt Sql:
SQL item models ........................ yes
Qt Widgets:
GTK+ ................................... no
Styles ................................. Fusion Windows
Qt PrintSupport:
CUPS ................................... yes
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. no
OCI (Oracle) ........................... no
ODBC ................................... yes
PostgreSQL ............................. yes
SQLite2 ................................ no
SQLite ................................. yes
Using system provided SQLite ......... no
TDS (Sybase) ........................... yes
Qt Testlib:
Tester for item models ................. yes
Serial Port:
ntddmodm ............................... no
Qt SerialBus:
Socket CAN ............................. yes
Socket CAN FD .......................... yes
SerialPort Support ..................... yes
Further Image Formats:
JasPer ................................. no
MNG .................................... no
TIFF ................................... yes
Using system libtiff ................. yes
WEBP ................................... yes
Using system libwebp ................. yes
Qt QML:
QML network support .................... yes
QML debugging and profiling support .... yes
QML just-in-time compiler .............. yes
QML sequence object .................... yes
QML XML http request ................... yes
QML Locale ............................. yes
Qt QML Models:
QML list model ......................... yes
QML delegate model ..................... yes
Qt Quick:
Direct3D 12 ............................ no
AnimatedImage item ..................... yes
Canvas item ............................ yes
Support for Qt Quick Designer .......... yes
Flipable item .......................... yes
GridView item .......................... yes
ListView item .......................... yes
TableView item ......................... yes
Path support ........................... yes
PathView item .......................... yes
Positioner items ....................... yes
Repeater item .......................... yes
ShaderEffect item ...................... yes
Sprite item ............................ yes
QtQuick3D:
Assimp ................................. yes
System Assimp .......................... no
Qt Scxml:
ECMAScript data model for QtScxml ...... yes
Qt Gamepad:
SDL2 ................................... no
Qt 3D:
Assimp ................................. yes
System Assimp .......................... no
Output Qt3D GL traces .................. no
Use SSE2 instructions .................. no
Use AVX2 instructions .................. no
Aspects:
Render aspect ........................ yes
Input aspect ......................... yes
Logic aspect ......................... yes
Animation aspect ..................... yes
Extras aspect ........................ yes
Qt 3D Renderers:
OpenGL Renderer ........................ yes
RHI Renderer ........................... no
Qt 3D GeometryLoaders:
Autodesk FBX ........................... no
Qt Wayland Client ........................ no
Qt Wayland Compositor .................... no
Qt Bluetooth:
BlueZ .................................. no
BlueZ Low Energy ....................... no
Linux Crypto API ....................... no
Native Win32 Bluetooth ................. no
WinRT Bluetooth API (desktop & UWP) .... no
WinRT advanced bluetooth low energy API (desktop & UWP) . no
Qt Sensors:
sensorfw ............................... no
Qt Quick Controls 2:
Styles ................................. Default Fusion Imagine Material Universal
Qt Quick Templates 2:
Hover support .......................... yes
Multi-touch support .................... yes
Qt Positioning:
Gypsy GPS Daemon ....................... no
WinRT Geolocation API .................. no
Qt Location:
Qt.labs.location experimental QML plugin . yes
Geoservice plugins:
OpenStreetMap ........................ yes
HERE ................................. yes
Esri ................................. yes
Mapbox ............................... yes
MapboxGL ............................. yes
Itemsoverlay ......................... yes
QtXmlPatterns:
XML schema support ..................... yes
Qt Multimedia:
ALSA ................................... yes
GStreamer 1.0 .......................... yes
GStreamer 0.10 ......................... no
Video for Linux ........................ yes
OpenAL ................................. no
PulseAudio ............................. yes
Resource Policy (libresourceqt5) ....... no
Windows Audio Services ................. no
DirectShow ............................. no
Windows Media Foundation ............... no
Qt TextToSpeech:
Flite .................................. no
Flite with ALSA ........................ no
Speech Dispatcher ...................... no
Qt Tools:
Qt Assistant ........................... yes
Qt Designer ............................ yes
Qt Distance Field Generator ............ yes
kmap2qmap .............................. yes
Qt Linguist ............................ yes
Mac Deployment Tool .................... no
makeqpf ................................ yes
pixeltool .............................. yes
qdbus .................................. yes
qev .................................... yes
Qt Attributions Scanner ................ yes
qtdiag ................................. yes
qtpaths ................................ yes
qtplugininfo ........................... yes
Windows deployment tool ................ no
WinRT Runner Tool ...................... no
Qt Tools:
QDoc ................................... no
Qt WebEngine Build Tools:
Use System Ninja ....................... yes
Use System Gn .......................... no
Jumbo Build Merge Limit ................ 8
Developer build ........................ no
Sanitizer .............................. no
QtWebEngine required system libraries:
fontconfig ........................... yes
dbus ................................. yes
nss .................................. yes
khr .................................. yes
glibc ................................ yes
QtWebEngine required system libraries for qpa-xcb:
x11 .................................. no
libdrm ............................... yes
xcomposite ........................... no
xcursor .............................. no
xi ................................... no
xtst ................................. no
Optional system libraries used:
re2 .................................. yes
icu .................................. no
libwebp, libwebpmux and libwebpdemux . yes
opus ................................. yes
ffmpeg ............................... no
libvpx ............................... yes
snappy ............................... yes
glib ................................. yes
zlib ................................. yes
minizip .............................. yes
libevent ............................. yes
jsoncpp .............................. yes
protobuf ............................. no
libxml2 and libxslt .................. yes
lcms2 ................................ yes
png .................................. yes
JPEG ................................. yes
harfbuzz ............................. yes
freetype ............................. yes
Qt PDF:
Support V8 ............................. no
Support XFA ............................ no
Support XFA-BMP ........................ no
Support XFA-GIF ........................ no
Support XFA-PNG ........................ no
Support XFA-TIFF ....................... no
Qt PDF Widgets:
Support Qt PDF Widgets ................. yes如果不是这种情况,或者出现任何其他错误消息,请检查并清理build下的文件,然后重新配置。
如果你想改变一些变量再次配置,也需要事先删除构建目录的内容。
如若不然,会产生一些奇奇怪怪的问题。
如果一切正常,则运行make和make install命令。
make -j$(nproc)
make install如果编译成功,编译后的文件(它们位于/opt/raspi/qt5.15目录中)可以复制到Raspberry Pi。同样,我们使用rsync命令来执行此操作。
cd /opt/raspi/
rsync -avz --rsync-path="sudo rsync" qt5.15 pi@192.168.1.134:/usr/local/如果树莓派空间很小,也可以重新烧录后在复制,以移除不需要的库和工具。大概节省700MB左右的空间。
QtCreator配置
在树莓派上,我们需要配置QtCreator以使用我们刚刚编译的Qt。
首先,我们需要安装QtCreator。
sudo apt-get install qtcreator接下来正常配置即可
| 架构 | gcc | g++ |
|---|---|---|
| 32位 | arm-linux-gnueabihf-g++ | arm-linux-gnueabihf-g++ |
| 64位 | aarch64-linux-gnu-gcc | aarch64-linux-gnu-g++ |
QtCreator运行时需要添加额外的环境变量
设置 DISPLAY 到 :0
设置 QT_QPA_PLATFORM 到 xcb