在嵌入式系统开发领域,针对特定硬件平台进行Qt框架的移植与适配是一项关键技术任务。作为业界主流的跨平台应用开发框架,Qt凭借其完善的图形界面支持能力,在嵌入式开发图形界面应用程序中占据重要地位。本文将基于ELF 1开发板详细阐述Qt 5.12 版本的交叉编译与系统移植方案。
1、源码下载
Qt5.12可在Qt官网或者GitHub等下载,以下为GitHub链接:
https://github.com/zhoujun59761/qtsrc512
2、源码配置修改
(1)解压源码包并设置权限
elf@ubuntu:~/work$ unzip qtsrc512-master.zip
elf@ubuntu:~/work$ chmod 777 -R qtsrc512-master(设置最大权限,避免编译时权限问题)
elf@ubuntu:~/work$ cd qtsrc512-master/
(2)修改qmake.conf配置文件
由于ELF 1开发板是 ARM 架构,所以需要修改 qmake.conf 文件以适应交叉编译环境。编辑文件路径为 qtbase/mkspecs/linux-arm-gnueabi-g++/qmake.conf,并进行如下修改。
elf@ubuntu:~/work/qtsrc512-master$ vi qtbase/mkspecs/linux-arm-gnueabi-g++/qmake.conf
内容如下:
#
# qmake configuration for building with arm-linux-gnueabi-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 = arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_CXX = arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_LINK = arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_LINK_SHLIB = arm-poky-linux-gnueabi-g++ -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 --sysroot=/opt/fsl-imx-x11/4.1.15-2.0.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi
QMAKE_CFLAGS = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_CXXFLAGS = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_LDFLAGS = -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now
# modifications to linux.conf
QMAKE_AR = arm-poky-linux-gnueabi-ar cqs
QMAKE_OBJCOPY = arm-poky-linux-gnueabi-objcopy
QMAKE_NM = arm-poky-linux-gnueabi-nm -P
load(qt_config)
3、交叉编译
(1)编写编译脚本
由于 configure 生成 Makefile 时需要大量参数,可以编写一个脚本保存这些配置。首先创建脚本文件 shell.sh。
elf@ubuntu:~/work/qtsrc512-master$ touch shell.sh
elf@ubuntu:~/work/qtsrc512-master$ chmod 777 shell.sh
在 shell.sh 中填入以下内容(-prefix 后面的路径需要根据实际情况修改)。
./configure -prefix /home/elf/work/qtsrc512-master/__install \
-opensource \
-confirm-license \
-release \
-shared \
-xplatform linux-arm-gnueabi-g++ \
-optimized-qmake \
-c++std c++11 \
--rpath=no \
-pch \
-skip qt3d \
-skip qtactiveqt \
-skip qtandroidextras \
-skip qtcanvas3d \
-skip qtconnectivity \
-skip qtdatavis3d \
-skip qtdoc \
-skip qtgamepad \
-skip qtlocation \
-skip qtmacextras \
-skip qtnetworkauth \
-skip qtpurchasing \
-skip qtremoteobjects \
-skip qtscript \
-skip qtscxml \
-skip qtsensors \
-skip qtspeech \
-skip qtsvg \
-skip qttools \
-skip qttranslations \
-skip qtwayland \
-skip qtwebengine \
-skip qtwebview \
-skip qtwinextras \
-skip qtxmlpatterns \
-make libs \
-make examples \
-nomake tools -nomake tests \
-gui \
-widgets \
-dbus-runtime \
--glib=no \
--iconv=no \
--pcre=qt \
--zlib=qt \
-no-openssl \
--xcb=qt \
--freetype=qt \
--harfbuzz=qt \
-no-opengl \
--libpng=qt \
--libjpeg=qt \
--sqlite=qt \
-plugin-sql-sqlite \
-recheck-all \
-no-strip
(2)执行环境变量
elf@ubuntu:~/work/qtsrc512-master$ . /opt/fsl-imx-x11/4.1.15-2.0.0/environment-setup-cortexa7hf-neon-poky-linux-gnueabi
(3)编译
运行脚本并进行编译。
elf@ubuntu:~/work/qtsrc512-master$ ./shell.sh
执行脚本可能会出现以下错误。
+ cd qtbase
+ /home/elf/work/qtsrc512-master/qtbase/configure -top-level -prefix /home/elf/work/qtsrc512-master/__install -opensource -confirm-license -release -strip -shared -xplatform linux-arm-gnueabi-g++ -optimized-qmake -c++std c++11 --rpath=no -pch -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtgamepad -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpurchasing -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtwayland -skip qtwebengine -skip qtwebview -skip qtwinextras -skip qtxmlpatterns -make libs -make examples -nomake tools -nomake tests -gui -widgets -dbus-runtime --glib=no --iconv=no --pcre=qt --zlib=qt -no-openssl --xcb=qt --freetype=qt --harfbuzz=qt -no-opengl --libpng=qt --libjpeg=qt --sqlite=qt -plugin-sql-sqlite -recheck-all
Please make sure to unset the QMAKESPEC, XQMAKESPEC, QMAKEPATH,
and QMAKEFEATURES environment variables prior to building Qt.
这是因为Ubuntu中已经有Qt的环境了,如需解决可以参考提示执行如下命令。
elf@ubuntu:~/work/qtsrc512-master$ unset QMAKESPEC XQMAKESPEC QMAKEPATH QMAKEFEATURES
继续执行shell.sh脚本。
elf@ubuntu:~/work/qtsrc512-master$ ./shell.sh
elf@ubuntu:~/work/qtsrc512-master$ make
elf@ubuntu:~/work/qtsrc512-master$ make install
编译完成后,__install 文件夹包含了 Qt 5.12 所需的所有文件。
elf@ubuntu:~/work/qtsrc512-master$ cd __install/
elf@ubuntu:~/work/qtsrc512-master/__install$ ls
bin doc examples include lib mkspecs plugins qml
其中,lib文件夹为所需的lib库,examples文件夹为demo工程。
4、功能测试
(1)打包Qt文件夹
将编译生成的文件打包。
elf@ubuntu:~/work/qtsrc512-master/__install$ tar -cjvf qt5.12.tar.bz2 *
(2)解压至开发板
将打包后的文件放到U盘,拷贝至开发板并解压。
root@ELF1:~# tar -mxvf qt5.12.tar.bz2 -C /
(3)执行环境变量
在开发板上设置环境变量,确保 Qt 正常运行。其中export QT_ROOT=/ 为qt5.12文件相关的路径(填解压的路径)。
export QT_ROOT=/
export QT_QPA_FONTDIR=/usr/share/fonts
export QT_QPA_PLATFORM_PLUGIN_PATH=$QT_ROOT/plugins
export QT_PLUGIN_PATH=$QT_ROOT/plugins
export LD_LIBRARY_PATH=$QT_ROOT/lib:$QT_ROOT/plugins/platforms
export QML2_IMPORT_PATH=$QT_ROOT/qml
//两种显示框架根据需求进行设置
export QT_QPA_PLATFORM=linuxfb:tty=/dev/fb0 //采用Linuxfb显示框架
export QT_QPA_PLATFORM=xcb:tty=/dev/fb0 //采用x11显示框架
(4)运行示例应用
Linuxfb显示框架运行 Qt 的示例应用进行功能测试。
root@ELF1:~# /examples/charts/areachart/areachart
效果如下:
X11 显示框架运行 Qt 的示例应用进行功能测试。
root@ELF1:~# export DISPLAY=:0.0
root@ELF1:~# /examples/charts/areachart/areachart
效果如下:
经过上述一系列步骤就可以顺利地将Qt 5.12版本交叉编译并成功移植至ELF 1开发板。衷心希望本文能为屏幕前的小伙伴在嵌入式开发领域的Qt移植工作带来实质性的帮助与指导!