本帖最后由 水精灵 于 2025-2-26 13:33 编辑

在嵌入式系统开发领域,针对特定硬件平台进行Qt框架的移植与适配是一项关键技术任务。作为业界主流的跨平台应用开发框架,Qt凭借其完善的图形界面支持能力,在嵌入式开发图形界面应用程序中占据重要地位。本文将基于ELF 1开发板详细阐述Qt 5.12 版本的交叉编译与系统移植方案。
01 源码下载
qt5.12可在qt官网或者GitHub等下载,以下为GitHub链接:
https://github.com/zhoujun59761/qtsrc512
02 源码配置修改
(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配置文件
由于目标板是 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
内容如下
  1. #

  2. # qmake configuration for building with arm-linux-gnueabi-g++

  3. #



  4. MAKEFILE_GENERATOR      = UNIX

  5. CONFIG                 += incremental

  6. QMAKE_INCREMENTAL_STYLE = sublib



  7. include(../common/linux.conf)

  8. include(../common/gcc-base-unix.conf)

  9. include(../common/g++-unix.conf)



  10. # modifications to g++.conf

  11. 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

  12. 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

  13. 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

  14. 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







  15. QMAKE_CFLAGS            = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard

  16. QMAKE_CXXFLAGS          = -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard

  17. QMAKE_LDFLAGS           = -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed  -Wl,-z,relro,-z,now





  18. # modifications to linux.conf

  19. QMAKE_AR                = arm-poky-linux-gnueabi-ar cqs

  20. QMAKE_OBJCOPY           = arm-poky-linux-gnueabi-objcopy

  21. QMAKE_NM                = arm-poky-linux-gnueabi-nm -P

  22. load(qt_config)
复制代码
图片1.png
参数 说明
arm-poky-linux-gnueabi-gcc 指定编译器
-march=armv7ve 指定目标架构为 ARMv7,并启用虚拟化扩展
-mfpu=neon 启用 NEON SIMD 指令集
-mfloat-abi=hard 启用硬件浮点运算
-mcpu=cortex-a7 为 Cortex-A7 处理器优化代码
--sysroot 指定目标系统的根目录,包含头文件和库文件
-O2 启用中等优化,以提高程序的执行效率
-O1 优化链接过程,提高链接效率
--hash-style=gnu 使用更高效的符号哈希表,提升加载速度
--as-needed 减少不必要的共享库链接,减小二进制文件的大小
-z relro 启用只读重定位,提高安全性,防止某些攻击
-z now 立即解析所有动态符号,提高安全性和启动性能
03 交叉编译
(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 后面的路径需要根据实际情况修改)。
  1. ./configure -prefix /home/elf/work/qtsrc512-master/__install \

  2. -opensource \

  3. -confirm-license \

  4. -release \

  5. -shared \

  6. -xplatform linux-arm-gnueabi-g++ \

  7. -optimized-qmake \

  8. -c++std c++11 \

  9. --rpath=no \

  10. -pch \

  11. -skip qt3d \

  12. -skip qtactiveqt \

  13. -skip qtandroidextras \

  14. -skip qtcanvas3d \

  15. -skip qtconnectivity \

  16. -skip qtdatavis3d \

  17. -skip qtdoc \

  18. -skip qtgamepad \

  19. -skip qtlocation \

  20. -skip qtmacextras \

  21. -skip qtnetworkauth \

  22. -skip qtpurchasing \

  23. -skip qtremoteobjects \

  24. -skip qtscript \

  25. -skip qtscxml \

  26. -skip qtsensors \

  27. -skip qtspeech \

  28. -skip qtsvg \

  29. -skip qttools \

  30. -skip qttranslations \

  31. -skip qtwayland \

  32. -skip qtwebengine \

  33. -skip qtwebview \

  34. -skip qtwinextras \

  35. -skip qtxmlpatterns \

  36. -make libs \

  37. -make examples \

  38. -nomake tools -nomake tests \

  39. -gui \

  40. -widgets \

  41. -dbus-runtime \

  42. --glib=no \

  43. --iconv=no \

  44. --pcre=qt \

  45. --zlib=qt \

  46. -no-openssl \

  47. --xcb=qt \

  48. --freetype=qt \

  49. --harfbuzz=qt \

  50. -no-opengl \

  51. --libpng=qt \

  52. --libjpeg=qt \

  53. --sqlite=qt \

  54. -plugin-sql-sqlite \

  55. -recheck-all \

  56. -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工程。
04 功能测试
(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/libQT_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
效果如下:
图片2.png
X11 显示框架运行 Qt 的示例应用进行功能测试。
root@ELF1:~# export DISPLAY=:0.0
root@ELF1:~# /examples/charts/areachart/areachart
效果如下:
图片3.png
经过上述一系列步骤就可以顺利地将Qt 5.12版本交叉编译并成功移植至ELF 1开发板。衷心希望本文能为屏幕前的小伙伴在嵌入式开发领域的Qt移植工作带来实质性的帮助与指导!
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Powered by Discuz! X3.5  © 2001-2013 Comsenz Inc.