提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、重要说明
- 二、准备环境
- 1.下载qt5.12.8源代码
- 2.配置系统环境
- 3.安装必要工具
- 4.解压qt5源代码
- 5.开始配置编译
- 6.配置qtcreator
- 三、编译opengl
- 总结
前言
最近有个项目使用Qt5的ARM64版,有一种方法是直接在板子上安装Qt5,这种方法最简单,但是板子效率低一些,编译慢,画面也不太流畅所以才切换到交叉编译方向来。
这篇文章基于Ubuntu-20.04编译,板子的系统也是20.04,接下来我会给出我的编译心得,包括一些注意事项,请仔细查看,能少走不少弯路。
OS:Ubuntu-20.04 x86_64
Board:Ubuntu-20.04 aarch64
工具:aarch64-linux-gnu-9
一、重要说明
最好板子的环境和OS的环境一样,这样可以避免不必要的报错,而且由于Qt5程序依赖libc这个库,导致高版本系统编译的二进制文件放在低版本系统不能运行。
为什么选择aarch64-linux-gnu-9,因为高版本可能出现不兼容的现象,报各种错,所以建议编译器版本不要超过9
,9也是可以的。
注意:这个教程5.12.8版本一次编译通过,5.15.2可能报错,QT6也不确定是否能用!
二、准备环境
1.下载qt5.12.8源代码
5.12.8下载页面
zip和tar.xz都行,只不过xz压缩率更高,建议选xz
2.配置系统环境
sudo apt update
sudo apt install g++-9-aarch64-linux-gnu gcc-9-aarch64-linux-gnu cmake gdb clang
3.安装必要工具
sudo apt install qtbase5-dev qt5-qmake qtcreator
4.解压qt5源代码
tar -xmf qt-everywhere-src-5.12.8.tar.xz
5.开始配置编译
./configure -release -opensource -confirm-license -xplatform linux-aarch64-gnu-g++ -prefix ~/Qt5.12.8_ARM64 -nomake examples -no-opengl -silent -qpa linuxfb
注意:这个版本不带opengl,如果你用不到opengl就可以不编译,如果你用opengl就要用另一种方法,下面会说。
这种方法可能不会编译web引擎,如果你用不到可以不用管,如果你用的到就按照它的提示追加就行了。等qmake构建好之后就可以编译了,注意使用的线程数。
make -j6 && make install
6.配置qtcreator
上面已经安装好qtcreator了,打开开始配置交叉编译环境。
打开Tools
->Options
Kits
->Qt Versions
右边的Add
会打开一个文件管理器。
找到我们编译好的Qt5.12.8_ARM64
直到bin/qmake
,选中打开就会添加新环境。
接着打开Kits
条目,同样右边的Add
Name:
随便取
Device type:
和我一样
Device:
和我一样
Sysroot:
留空
Compiler:
按照我的,一定不要错了
,选刚刚安装的arm 64bit
Debuger:
gdb
Qt version:
和我一样,选刚才添加的
Cmake Tool:
选cmake就行了
最终Apply
->Ok
。
创建一个新项目或旧项目选择ARM64就可以交叉编译了,目前这个方法不能在x86_64上运行,你需要把二进制文件拷贝到aarch64设备上运行。
三、编译opengl
上面的方法没有opengl的支持,实际上我试了很多方法单纯在x86_64上编译opengl是不行的,需要借助sysroot
,可以理解成你板子的rootfs
。
你先去板子官方找一个rootfs,安装配置好opengl环境
,后面要用。这里以firefly的rk3399为例,我把rootfs
拷贝到我的Ubuntu-20.04电脑上了。
这个rootfs
是可以通过qemu-user-static
软件模拟环境的,这个就不细说了,一般支持定制的板子厂商的维基里都会说明的。
上面的软件照样装,流程照样走一边,只是到了构建的时候命令变了:
./configure -sysroot ~/firefly-rk3399-ubuntu2004-rootfs -release -opensource -confirm-license -xplatform linux-aarch64-gnu-g++ -extprefix ~/Qt5.12.8_ARM64 -nomake examples -opengl -silent -qpa linuxfb
-sysroot:
指定你自己的rootfs
目录
-extprefix:
这个叫外部安装路径,要不然编译好的安装到rootfs
里面去了
-opengl:
指定编译opengl Desktop,根据自己需要编译
等待构建完成…
贴下我的构建输出:
Building on: linux-g++ (x86_64, CPU features: mmx sse sse2)
Building for: linux-aarch64-gnu-g++ (arm64, CPU features: cx16 neon)
Target compiler: gcc 9.4.0
Configuration: cross_compile use_gold_linker compile_examples enable_new_dtags largefile neon precompile_header silent shared rpath release c++11 c++14 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++1z
Using ccache ........................... no
Using gold linker ...................... yes
Using new DTAGS ........................ 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 .... no
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 ................................... no
Using system zlib ...................... yes
Qt Core:
DoubleConversion ....................... yes
Using system DoubleConversion ........ no
GLib ................................... no
iconv .................................. yes
ICU .................................... no
Tracing backend ........................ <none>
Logging backends:
journald ............................. no
syslog ............................... no
slog2 ................................ no
Using system PCRE2 ..................... no
Qt Network:
getifaddrs() ........................... yes
IPv6 ifname ............................ yes
libproxy ............................... no
Linux AF_NETLINK ....................... yes
OpenSSL ................................ no
Qt directly linked to OpenSSL ........ no
OpenSSL 1.1 ............................ no
DTLS ................................... no
SCTP ................................... no
Use system proxies ..................... yes
Qt Gui:
Accessibility .......................... yes
FreeType ............................... yes
Using system FreeType ................ no
HarfBuzz ............................... yes
Using system HarfBuzz ................ no
Fontconfig ............................. no
Image formats:
GIF .................................. yes
ICO .................................. yes
JPEG ................................. yes
Using system libjpeg ............... no
PNG .................................. yes
Using system libpng ................ no
EGL .................................... yes
OpenVG ................................. no
OpenGL:
Desktop OpenGL ....................... yes
OpenGL ES 2.0 ........................ no
OpenGL ES 3.0 ........................ no
OpenGL ES 3.1 ........................ no
OpenGL ES 3.2 ........................ no
Vulkan ................................. no
Session Management ..................... yes
Features used by QPA backends:
evdev .................................. yes
libinput ............................... no
INTEGRITY HID .......................... no
mtdev .................................. no
tslib .................................. no
xkbcommon .............................. yes
X11 specific:
XLib ................................. yes
XCB Xlib ............................. yes
EGL on 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 ...................... no
EGLFS GBM ............................ no
EGLFS VSP2 ........................... no
EGLFS Mali ........................... no
EGLFS Raspberry Pi ................... no
EGLFS X11 ............................ yes
LinuxFB ................................ yes
VNC .................................... yes
Mir client ............................. no
XCB:
Using system-provided XCB libraries .. no
XCB XKB .............................. yes
XCB XInput ........................... yes
Native painting (experimental) ....... no
GL integrations:
GLX Plugin ......................... yes
XCB GLX .......................... no
EGL-X11 Plugin ..................... yes
Qt Sql:
SQL item models ........................ yes
Qt Widgets:
GTK+ ................................... no
Styles ................................. Fusion Windows
Qt PrintSupport:
CUPS ................................... no
Qt Sql Drivers:
DB2 (IBM) .............................. no
InterBase .............................. no
MySql .................................. no
OCI (Oracle) ........................... no
ODBC ................................... no
PostgreSQL ............................. no
SQLite2 ................................ no
SQLite ................................. yes
Using system provided SQLite ......... no
TDS (Sybase) ........................... no
Qt Testlib:
Tester for item models ................. yes
Qt SerialBus:
Socket CAN ............................. yes
Socket CAN FD .......................... yes
Further Image Formats:
JasPer ................................. no
MNG .................................... no
TIFF ................................... yes
Using system libtiff ................. no
WEBP ................................... yes
Using system libwebp ................. no
Qt QML:
QML network support .................... yes
QML debugging and profiling support .... yes
QML sequence object .................... yes
QML list model ......................... yes
QML XML http request ................... yes
QML Locale ............................. 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
Qt Scxml:
ECMAScript data model for QtScxml ...... yes
Qt Gamepad:
SDL2 ................................... no
Qt 3D:
Assimp ................................. yes
System Assimp .......................... no
Output Qt3D Job traces ................. 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
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
WinRT Bluetooth 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 ................................... no
GStreamer 1.0 .......................... no
GStreamer 0.10 ......................... no
Video for Linux ........................ yes
OpenAL ................................. no
PulseAudio ............................. no
Resource Policy (libresourceqt5) ....... no
Windows Audio Services ................. no
DirectShow ............................. no
Windows Media Foundation ............... no
Qt Tools:
QDoc ................................... no
Qt WebEngine:
Embedded build ......................... yes
Full debug information ................. no
Pepper Plugins ......................... no
Printing and PDF ....................... no
Proprietary Codecs ..................... no
Spellchecker ........................... yes
Native Spellchecker .................... no
WebRTC ................................. no
Use System Ninja ....................... no
Geolocation ............................ yes
WebChannel support ..................... yes
Use v8 snapshot ........................ yes
Kerberos Authentication ................ no
Support qpa-xcb ........................ no
Building v8 snapshot supported ......... yes
Use ALSA ............................... no
Use PulseAudio ......................... no
Optional system libraries used:
re2 .................................. no
icu .................................. no
libwebp, libwebpmux and libwebpdemux . no
opus ................................. no
ffmpeg ............................... no
libvpx ............................... no
snappy ............................... no
glib ................................. no
zlib ................................. yes
minizip .............................. no
libevent ............................. no
jsoncpp .............................. no
protobuf ............................. no
libxml2 and libxslt .................. no
lcms2 ................................ no
png .................................. no
JPEG ................................. no
harfbuzz ............................. no
freetype ............................. no
Required system libraries:
fontconfig ........................... no
dbus ................................. no
nss .................................. no
khr .................................. yes
glibc ................................ yes
Required system libraries for qpa-xcb:
x11 .................................. yes
libdrm ............................... no
xcomposite ........................... no
xcursor .............................. no
xi ................................... no
xtst ................................. no
Note: Also available for Linux: linux-clang linux-icc
Note: PKG_CONFIG_LIBDIR automatically set to /home/anold/firefly-rk3399-ubuntu2004-rootfs/usr/lib/pkgconfig:/home/anold/firefly-rk3399-ubuntu2004-rootfs/usr/share/pkgconfig:/home/anold/firefly-rk3399-ubuntu2004-rootfs/usr/lib/aarch64-linux-gnu/pkgconfig
Note: PKG_CONFIG_SYSROOT_DIR automatically set to /home/anold/firefly-rk3399-ubuntu2004-rootfs
Note: Disabling X11 Accessibility Bridge: D-Bus or AT-SPI is missing.
Note: No wayland-egl support detected. Cross-toolkit compatibility disabled.
WARNING: QDoc will not be compiled, probably because libclang could not be located. This means that you cannot build the Qt documentation.
Either ensure that llvm-config is in your PATH environment variable, or set 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.
On Windows, you must set LLVM_INSTALL_DIR to the installation path.
OpenGL:
Desktop OpenGL … yes
OpenGL ES 2.0 … no
OpenGL ES 3.0 … no
OpenGL ES 3.1 … no
OpenGL ES 3.2 … no
按照我的方法Desktop OpenGL必然为yes才算成功,你要是需要es2就加es2,你要确定你的rootfs支持es2否则编译不通过。
我这种方法不是完全编译,包含了大多数我能用到的组件,要想知道QT支持哪些组件,你只需要执行下面的命令挨个看。
cat qtbase/config_help.txt
比如OpenGL
-gtk ................. Enable GTK platform theme support [auto]
-lgmon ............... Enable lgmon support [auto] (QNX only)
-no-opengl ........... Disable OpenGL support
-opengl <api> ........ Enable OpenGL support. Supported APIs:
es2 (default on Windows), desktop (default on Unix),
dynamic (Windows only)
-opengles3 ........... Enable OpenGL ES 3.x support instead of ES 2.x [auto]
-egl ................. Enable EGL support [auto]
-angle ............... Use bundled ANGLE to support OpenGL ES 2.0 [auto]
(Windows only)
-combined-angle-lib .. Merge LibEGL and LibGLESv2 into LibANGLE (Windows only)
-qpa <name> .......... Select default QPA backend(s) (e.g., xcb, cocoa, windows)
A prioritized list separated by semi-colons.
至于怎么选就看你自己了,如果报错的话大概率是sysroot
的问题,你需要多检查几遍,或者评论我们一起探讨下。
make -j6 && make install
总结
1、本身并不难,找到诀窍才是重要的,选不对版本和工具链够头疼的,还是那句话:尽量维持环境一致性。
2、组件很多,编译也不快,最好还是知道哪些组件自己用得到免得反复编译耗时。