QT5交叉编译保姆级教程(arm64、mips64)

什么是交叉编译?

简单说,就是在当前系统平台上,开发编译运行于其它平台的程序。

比如本文硬件环境是x86平台,但是编译出来的程序是在arm64架构、mips64等架构上运行

本文使用的操作系统:统信UOS家庭版22.0

一、安装QT5:

sudo apt update
sudo apt install qtbase5-dev -y
sudo apt install qtcreator -y

二、安装编译所需的组件

sudo apt install build-essential libgl1-mesa-dev gperf libclang-dev flex bison llvm -y

#安装python环境(QT中的一些功能、组件需要用到)

sudo apt install python -y
sudo apt install python-dev -y
sudo apt install python-pip -y

#注意,一些较新的系统,比如deepin v23中可能没有这几个包,需要修改为:
sudo apt install python2 -y
sudo apt install python2-dev -y
sudo apt install python3 -y
sudo apt install python3-dev -y
sudo apt install python3-pip -y

#安装aarch64(arm64)编译链工具。国产CPU,如飞腾、华为麒麟等是这个架构

sudo apt install g++-aarch64-linux-gnu -y
sudo apt install gcc-aarch64-linux-gnu -y

#安装mips64el编译链工具。龙芯3A3000、3A4000等是此架构。龙芯最新的LoongArch架构似乎还没有编译链工具

sudo apt install gcc-mips64el-linux-gnuabi64 -y
sudo apt install g++-mips64el-linux-gnuabi64 -y

三、下载QT5源码

QT官方下载地址:Index of /archive/qticon-default.png?t=N7T8https://download.qt.io/archive/qt

根据自身需求选择一个版本下载。源码版本不需要和当前安装的QT一致,但最好不要低于当前安装的版本。QT6和QT5差别太大,不要选择。

这里我随手选了一个5.12.2的版本,可以直接在网页上下载、用下载工具下载、还可以使用命令下载:

#下载源码:
wget https://download.qt.io/archive/qt/5.12/5.12.2/single/qt-everywhere-src-5.12.2.tar.xz
#下载好后,解压源码:
tar -xvf qt-everywhere-src-5.12.2.tar.xz​

四、配置和编译源码

  1. 配置编译QT5的aarch64架构源码

进入刚才解压好的源码目录中,打开终端输入:

#配置编译参数
./configure -release -opensource -confirm-license -xplatform linux-aarch64-gnu-g++ -prefix /usr/local/qt5.12.2arm -nomake examples -no-opengl -silent -qpa linuxfb

如图:

其中:/usr/local/qt5.12.2arm 是编译后的安装目录,可自行修改

执行成功的话,如图:

编译:

make -j 12

-j 12参数表示使用12个线程进行编译,根据电脑的CPU线程数进行设置即可。

编译过程较长,如果编译过程中需要进行其它操作,适当减少线程数,以免操作太卡。

编译成功后,如图:

将编译好的文件安装到指定位置(安装位置在前面配置参数时已设置):

sudo make install

    2.配置编译QT5的mips64架构源码

先将前面解压的qt-everywhere-src-5.12.2文件夹改名或者删除,然后重启一下电脑!

为什么要重启?因为我在编译完aarch64之后,接着编译mips64时编译出错,重启之后才正常。

为了避免不必要的错误,因此建议先重启一下。

使用命令重新解压源码:

tar -xvf qt-everywhere-src-5.12.2.tar.xz

QT源码里并没有mips64的编译项,因此这里需要手动创建。

进入刚才解压的源码目录,再进入其下级目录:qtbase/mkspecs 如图:

找到linux-aarch64-gnu-g++文件夹,将其复制一份。如图:

将其改名为:linux-mips64el-g++  然后双击进入,里面有两个文件:qmake.conf、qplatformdefs.h

打开qmake.conf文件,打开后如图:

将aarch64全部替换为mips64el、gnu替换成gnuabi64,替换后如图:

修改完成,保存关闭文件即可。

修改好的qmake.conf文件内容:

#
# qmake configuration for building with mips64el-linux-gnuabi64-g++
#

MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC                = mips64el-linux-gnuabi64-gcc
QMAKE_CXX               = mips64el-linux-gnuabi64-g++
QMAKE_LINK              = mips64el-linux-gnuabi64-g++
QMAKE_LINK_SHLIB        = mips64el-linux-gnuabi64-g++

# modifications to linux.conf
QMAKE_AR                = mips64el-linux-gnuabi64-ar cqs
QMAKE_OBJCOPY           = mips64el-linux-gnuabi64-objcopy
QMAKE_NM                = mips64el-linux-gnuabi64-nm -P
QMAKE_STRIP             = mips64el-linux-gnuabi64-strip
load(qt_config)

回到源码根目录,在终端中执行参数配置:

./configure -release -opensource -confirm-license -xplatform linux-mips64el-g++ -prefix /usr/local/qt5.12.2mips64 -nomake examples -no-opengl -silent -qpa linuxfb

如图:

 执行成功如图:

编译安装和aarch64是一样的:

make -j12  #编译源码。注意根据自已CPU的线程数修改12这个数
sudo make install  #安装编译好的源码

五、配置QT

打开Qt Creator,选择 工具->选项->Kits->Qt Versions 如图:

 

点击右侧的“添加...”按钮,在弹出的窗口中选择前面编译安装好的QT目录下的qmake,如图:

添加好后,点击右下角的“Apply”按钮。然后点击“构建套件(Kit)”选项卡:

点击右侧“添加”按钮,配置如图:

其中,名称可自行修改;

编译器可以选择gcc,只是我比较喜欢Clang,所以这里选的Clang;

注意:Qt mkspec 一定要填写正确

arm的Qt mkspec:linux-aarch64-gnu-g++

mips的Qt mkspec:linux-mips64el-g++

至于红色的感叹号,这个不用在意,不影响编译。

mips64的配置也差不多:

六、验证测试

现在,创建一个工程来验证一下交叉编译是否正常。

打开或者创建工程时,可能无法选择arm64、mips64,如图:

不要紧,选择其它的先完成创建!

点击左侧的“项目”,然后再点击“Build & Run”项下的arm64、mips64即可添加:

点击左侧工程,选择构建套件,即可切换:

先测试aarch64架构:

需要注意的是,异架构的程序是不能直接运行的。所以我们不运行它,直接点击左下方的“锤子”或者按Ctrl+B来构建。

编译没有出错。使用file命令,看一下编译后的文件:

如图所示,编译出来的程序确实是ARM aarch64 架构!

再看一下MIPS64的编译结果:

OK,搞定收工..

关于龙芯最新的“龙”(LoongArch)架构,似乎还没有编译链工具,因此还没法交叉编译。。

#2023.11.24补充内容

以前一直使用统信UOS,这两天突然想试一下深度(deepin)系统,虽然他们是一家..

于是装上deepin最新的v23 版本,选择了6.1内核。。结果却遇到了一些错误!

错误一、安装组件时,没有python、python-dev、python-pip这几个包,需更改为:python2、python2-dev、python3、python3-dev、python3-pip
错误二、较低的Qt源码版本(如文中使用的5.12.2),在执行configure参数配置、make编译时报错:error: ‘numeric_limits’ is not a member of ‘std’,如下图:

而在编译较新的版本,如5.15.11时则正常。

解决方法:使用文本编辑器打开报错的文件,在文件头部合适的位置加上这句:#include <limits>

5.12.2版本有5个报错文件,可以使用下面的命令快速修改:

#快速修改报错文件
sed -i '43a#include <limits>' ./qtbase/src/corelib/global/qendian.h
sed -i '44a#include <limits>' ./qtbase/src/corelib/tools/qbytearraymatcher.h
sed -i '48a#include <limits>' ./qtbase/src/tools/moc/generator.cpp
sed -i '54a#include <limits>' ./qtdeclarative/src/qml/jsruntime/qv4propertykey_p.h
sed -i '52a#include <limits>' ./qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h
#注意:命令里的文件路径使用的是相对路径,执行时一定要在源码根目录!

再次提醒,执行命令时一定要在源码根目录!命令可以直接粘贴在终端中执行,如图:

其它版本就不一一测试了..

Building on: linux-g++ (x86_64, CPU features: mmx sse sse2) Building for: linux-aarch64-gnu-g++ (arm64, CPU features: neon) Target compiler: gcc 6.3.1 Configuration: cross_compile use_gold_linker compile_examples enable_new_dtags largefile neon precompile_header shared rpath release c++11 c++14 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++14 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 ........................ 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 ................................ yes Qt directly linked to OpenSSL ........ no OpenSSL 1.1 ............................ no DTLS ................................... yes 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 ............... yes PNG .................................. yes Using system libpng ................ no EGL .................................... no OpenVG ................................. no OpenGL: Desktop OpenGL ....................... no 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 .............................. no X11 specific: XLib ................................. no EGL on X11 ........................... no QPA backends: DirectFB ............................... no EGLFS .................................. no LinuxFB ................................ yes VNC .................................... yes Mir client ............................. no 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 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 ............................. no 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 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 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 x11 .................................. no Required system libraries: fontconfig ........................... no dbus ................................. no nss .................................. no khr .................................. no glibc ................................ yes Required system libraries for qpa-xcb: libdrm ............................... no xcomposite ........................... no xcursor .............................. no xi ................................... no xrandr ............................... no xtst ................................. no Note: Also available for Linux: linux-clang linux-icc
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

skyyx2002

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值