1. 搭建Qt交叉编译环境
任务说明
在Ubuntu主机上完成Qt交叉编译环境搭建,最终实现:在主机上编写一个简单的Qt窗口程序,通过交叉编译生成ARM架构可执行文件,并将其部署到ARM开发板上运行。本案例以全志T113(Cortex-A7)平台为例,工具链使用arm-linux-gnueabihf-。
环境准备
主机系统:Ubuntu 20.04/22.04 x86_64。需要安装的软件包:
sudo apt update
sudo apt install -y build-essential libgl1-mesa-dev libfontconfig1-dev \
libdbus-1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \
libssl-dev libudev-dev libxcb-* libx11-xcb-dev libglib2.0-dev \
gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf
Qt源码交叉编译
Qt交叉编译需要先为ARM架构编译Qt库。下载Qt 5.15.2源码(约500MB),然后执行以下配置与编译:
tar xf qt-everywhere-src-5.15.2.tar.xz
cd qt-everywhere-src-5.15.2
mkdir build-arm && cd build-arm
../configure -prefix /opt/qt5.15-arm \
-xplatform linux-arm-gnueabihf-g++ \
-release -opensource -confirm-license \
-nomake examples -nomake tests \
-no-opengl -no-gtk -no-xcb \
-no-feature-xlib -no-feature-xcb-xlib \
-qt-libpng -qt-libjpeg -qt-zlib \
-no-feature-cups -no-feature-printpreview \
-no-feature-eglfs -no-feature-linuxfb \
-no-feature-kms -no-feature-evdev \
-no-feature-libinput -no-feature-glib \
-skip qt3d -skip qtcanvas3d -skip qtdoc \
-skip qtgamepad -skip qtlocation -skip qtmacextras \
-skip qtpurchasing -skip qtserialport -skip qtspeech \
-skip qttools -skip qttranslations -skip qtvirtualkeyboard \
-skip qtwebengine -skip qtwebview -skip qtwinextras \
-skip qtconnectivity -skip qtdeclarative -skip qtgraphicaleffects \
-skip qtmultimedia -skip qtquickcontrols -skip qtquickcontrols2 \
-skip qtscript -skip qtsensors -skip qtsvg -skip qtwayland \
-skip qtwebsockets -skip qtxmlpatterns
make -j$(nproc)
sudo make install
编译耗时约30~60分钟。完成后/opt/qt5.15-arm目录即为ARM版Qt库。
配置Qt Creator交叉编译套件
安装Qt Creator(若未安装):
sudo apt install -y qtcreator
打开Qt Creator,依次进入 工具 → 选项 → Kits → 编译器,添加两个编译器:
- GCC(C):
/usr/bin/arm-linux-gnueabihf-gcc - G++(C++):
/usr/bin/arm-linux-gnueabihf-g++
然后在 Qt Versions 中点击“添加”,选择/opt/qt5.15-arm/bin/qmake。最后在 Kits 中新建套件,名称设为“ARM-Qt5.15”,编译器选择刚添加的交叉编译器,Qt Version选择“Qt 5.15.2 (qt5.15-arm)”,CMake工具保持默认。
编写并交叉编译测试程序
创建测试项目hello_arm,main.cpp内容如下:
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label("Hello ARM Qt!");
label.resize(320, 200);
label.show();
return app.exec();
}
项目文件hello_arm.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hello_arm
TEMPLATE = app
SOURCES += main.cpp
在Qt Creator中打开该.pro文件,选择“ARM-Qt5.15”套件,点击左下角“构建”按钮。构建完成后,在构建目录下生成hello_arm可执行文件。
部署到ARM开发板
将可执行文件复制到开发板(假设开发板IP为192.168.1.100,用户名root):
scp hello_arm root@192.168.1.100:/root/
ssh root@192.168.1.100
chmod +x /root/hello_arm
export QT_QPA_PLATFORM=linuxfb
/root/hello_arm
开发板需要具备framebuffer设备(/dev/fb0),且已配置好LCD显示。程序运行后屏幕上显示“Hello ARM Qt!”窗口即表示成功。
验证方法
在开发板上执行file /root/hello_arm,输出应包含ARM架构信息。程序运行时观察LCD屏幕是否出现窗口标题及文字。若程序无法启动,执行export QT_DEBUG_PLUGINS=1查看插件加载日志,确认linuxfb平台插件路径正确(需将/opt/qt5.15-arm/plugins/platforms目录同步到开发板对应位置)。