【Linux】QT5移植到Linux ARM

6 篇文章 0 订阅

相关文章

《【TOOLS】ubuntu如何安装QtCreator》

1. 前言

QT 是一种跨平台 C++图形用户界面应用程序开发框架。它既可以开发 GUI 程序,也可用于开发非 GUI 程序,比如控制台工具和服务器。

在嵌入式 Linux 系统上,移植QT时通常需要支持tslisb,它是开源并常用的触摸屏库。下面会通过2个部分来介绍整个移植过程:tslisb移植qt移植

2. tslib-1.22的移植

2.1 tslib官方源码下载

最新tslib官方源码下载地址如下:
https://github.com/libts/tslib/releases

2.2 交叉编译tslib源码

  • 在编译tslib之前确认自己的电脑上已经安装了automake autoconf libtool libsysfs-dev软件

    sudo apt-get install automake autoconf libtool libsysfs-dev
    
  • 自动配置

    ./autogen.sh
    

    运行结果如下:
    在这里插入图片描述

  • configure做准备, 防止编译时出错。在当前目录运行如下命令:

    echo  "ac_cv_func_malloc_0_nonnull=yes"  > tmp.cache
    
  • configure配置tslib工程,命令如下:

    ./configure --host=arm-linux --cache-file=tmp.cache  --prefix=/opt/tslib1.22 CC=/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf/bin/arm-none-linux-gnueabihf-gcc
    

    --prefix=/opt/tslib1.22 // 安装路径 (注意一定要写自己的安装路径)
    CC=...../arm-none-linux-gnueabihf-gcc // 指定编译器的绝对路径(注意:必须是绝对路径,否则报错) (注意一定是你交叉编译器的arm-linux-gcc的绝对安装路径)

    运行效果如下:
    在这里插入图片描述

  • 编译和安装

    1. make -j4 // -j4为4线程编译,可根据服务配置调整参数
    2. sudo make install // 将tslib安装到指定目录
    
  • 检查是否安装成功:如果出现bin,etc,include,lib这4个目录,如下图所示,说明交叉编译并安装tslib成功。
    在这里插入图片描述

3. QT5.15.2的移植

3.1 QT5.15.2官方源码下载

QT官方源码下载地址如下:
https://download.qt.io/archive/qt/5.15/5.15.2/single/
在这里插入图片描述

3.2 交叉编译QT

  • 将下载的qt-everywhere-opensource-src-5.7.0.tar.gz执行如下命令解压:

    tar -vxf qt-everywhere-src-5.15.2.tar.xz
    cd qt-everywhere-src-5.15.2
    
  • 修改交叉编译架构用到的信息:

    vim 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-none-linux-gnueabihf-gcc
      QMAKE_CXX               = arm-none-linux-gnueabihf-g++
      QMAKE_LINK              = arm-none-linux-gnueabihf-g++
      QMAKE_LINK_SHLIB        = arm-none-linux-gnueabihf-g++
      
      # modifications to linux.conf
      QMAKE_AR                = arm-none-linux-gnueabihf-ar cqs 
      QMAKE_OBJCOPY           = arm-none-linux-gnueabihf-objcopy
      QMAKE_NM                = arm-none-linux-gnueabihf-nm -P
      QMAKE_STRIP             = arm-none-linux-gnueabihf-strip
      load(qt_config)
    
  • 创建一个脚本文件,用于生成Makefile,在qt编译根目录执行命令:

    vim autoConfigure.sh
    

    输入下面内容并保存:

    #! /bin/sh
    
    ./configure \
    -v \
    -prefix /opt/qt-5.15.2 \
    -release \
    -opensource \
    -no-accessibility \
    -make libs \
    -xplatform linux-arm-gnueabi-g++ \
    -optimized-qmake \
    -pch \
    -qt-zlib \
    -qt-freetype \
    -tslib \
    -skip qtlocation \
    -no-iconv \
    -no-opengl \
    -no-sse2 \
    -no-openssl \
    -no-cups \
    -no-glib \
    -no-pkg-config \
    -no-separate-debug-info \
    -I/opt/tslib1.22/include -L/opt/tslib1.22/lib
    

    其中-prefix /opt/qt-5.15.2代表我们编译完QT5.15.2后要安装地址;-tslib代表QT对触摸板的支持,-I 和 -L后面分别为上面编译tslib的include和lib的安装目录。

    执行命令:

    chmod 777 qt.configure.sh
    ./autoConfigure.sh
    

    上述命令自动生成Makefile文件。

  • 编译和安装

    1. make -j4 // -j4为4线程编译,可根据服务配置调整参数
    2. sudo make install // 将tslib安装到指定目录
    
  • 我们切换到目标目录下看看是否安装成功:

    cd /opt/qt-5.15.2/
    ls
    

    如图所示:
    在这里插入图片描述
    /opt/qt-5.15.2/opt/tslib1.22 拷贝到开发板的文件系统中对应的目录中。

4. 设置QtCreator编译环境

首先参考这篇文章《【TOOLS】ubuntu如何安装QtCreator》安装QtCreator工具。

进入QtCreator开发环境,在菜单中选择Tools->Options,在弹出的对话框中,选择Kits,选择标签Compilers,按下图设置,选择手动添加C(GCC)和C++(G++)交叉编译器,如下图所示:
在这里插入图片描述
切换到Qt Versions标签,点击“Add”按钮,选择qmake,如图所示:
在这里插入图片描述
切换到Kits标签,点击“Add”按钮,将上面设置的CompilersQt Versions添加到新的配置,然后设置如下图所示:
在这里插入图片描述
保存以上的配置后,创建一个新的工程进行测试,创建时选择新添加的Kits:
在这里插入图片描述
验证是配置后的ARM交叉工具链编译,并且编译成功:
在这里插入图片描述

5. 配置开发板的环境

复制QtCreator使用的fonts字库到开发板的/opt/qt-5.15.2/lib/fonts目录:
在这里插入图片描述
配置开发板的环境变量:

vim /etc/profile

输入内容如下:

export QT_QPA_PLATFORM=linuxfb
export XDG_RUNTIME_DIR=/usr/lib/
export RUNLEVEL=3

更新环境变量:

source /etc/profile

备注:上面配置环境变量时,未对触摸屏设置。备注里面的配置其它博客参考过来,提供参考,未对下面的配置进行验证:

export TSLIB_ROOT=/opt/tslib1.4
export QT_ROOT=/opt/qt-5.7.0   
export TSLIB_TSDEVICE=/dev/input/event2
export TSLIB_TSEVENTTYPE=input         
export TSLIB_CONFFILE=/opt/tslib1.4/etc/ts.conf
export TSLIB_PLUGINDIR=/opt/tslib1.4/lib/ts    
export TSLIB_CONSOLEDEVICE=none                
export TSLIB_FBDEVICE=/dev/fb0             
export QWS_MOUSE_PROTO=tslib:/dev/input/event2
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib:$QT_ROOT/lib:$TSLIB_ROOT/lib:$TSLIB_ROOT/lib/
export QT_QPA_PLATFORM_PLUGIN_PATH=$QT_ROOT/plugins                                              
export QT_QPA_PLATFORM=linuxfb:tty=/dev/fb0                                                      
export QT_QPA_FONTDIR=$QT_ROOT/lib/fonts           
export QT_QPA_GENERIC_PLUGINS=tslib

配置好开发板的环境变量后,在ARM开发板运行上面编译好的QT程序,结果如下:
在这里插入图片描述

6. 移植过程遇到的问题

问题1: 在开发板运行qt hello002应用程序时报错:

hello002: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.28' not found (required by /opt/qt-5.15.2/lib/libQt5Widgets.so.5)
hello002: /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.28' not found (required by /opt/qt-5.15.2/lib/libQt5Core.so.5)

解决办法:
在开发板运行环境未找到对应版本的GLIBC,交叉编译工具有对应版本的库,将它拷贝到开发板:

benjamin@ubuntu:/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf$ ll ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++*
lrwxrwxrwx 1 7720 7720       19 Nov 21  2020 ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++.so -> libstdc++.so.6.0.28*
lrwxrwxrwx 1 7720 7720       19 Nov 21  2020 ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++.so.6 -> libstdc++.so.6.0.28*
-rwxr-xr-x 1 7720 7720 16701124 Nov 21  2020 ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++.so.6.0.28*
-rw-r--r-- 1 7720 7720     2394 Nov 21  2020 ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++.so.6.0.28-gdb.py
benjamin@ubuntu:/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf$ cp ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++* ~/nfs_rootfs/usr/lib
lib/     libexec/
benjamin@ubuntu:/opt/ToolChain/gcc-arm-10.2-2020.11-x86_64-arm-none-linux-gnueabihf$ sudo cp ./arm-none-linux-gnueabihf/libc/usr/lib/libstdc++* ~/nfs_rootfs/usr/lib/ -rfd

问题2: 在开发板运行qt hello002应用程序时报错:

QIconvCodec::convertToUnicode: using Latin-1 for conversion, iconv_open failed
QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed

解决办法一:

下载 http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz  
./configure CC=arm-none-linux-gnueabihf-gcc --prefix=$PWD/_install --host=arm-linux
make   
make install 

复制preloadable_libiconv.so到开发板的/lib目录,并配置在/etc/profile添加export LD_PRELOAD=/lib/preloadable_libiconv.so

解决办法二:

编译QT时不使用-no-iconv:
./configure  -no-iconv ...

问题3:移植libiconv的时候报错:

ENDS_ON_LIBINTL=1   -g -O2 -c xmalloc.c
In file included from progname.c:26:
./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function); did you mean ‘fgets’?
 1010 | _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
      | ^~~~~~~~~~~~~~~
make[2]: *** [Makefile:914: progname.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: Leaving directory '/home/benjamin/work/qt_project/libiconv-1.14/srclib'
make[1]: *** [Makefile:865: all] Error 2
make[1]: Leaving directory '/home/benjamin/work/qt_project/libiconv-1.14/srclib'
make: *** [Makefile:35: all] Error 2

解决办法:

vi libiconv-1.14/srclib/stdio.in.h

将698行的代码:_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");替换为:

#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)
 _GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");
#endif

问题4:在开发板运行qt hello002应用程序时报错:

qt.qpa.plugin: Could not find the Qt platform plugin "eglfs" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: linuxfb, minimal, offscreen, vnc.

Aborted

在执行qt程序时指定平台就可解决,如下:
解决办法一:

# hello002 -platform linuxfb

解决办法二:
/etc/profile末尾增加两句

export QT_QPA_PLATFORM=linuxfb

问题5:运行qt时不显示文字:

QFontDatabase: Cannot find font directory /opt/qt-5.15.2/lib/fonts.
Note that Qt no longer ships fonts. Deploy some (from https://dejavu-fonts.github.io/ for example) or switch to fontconfig.

解决办法:
复制QtCreator使用的fonts到开发板/opt/qt-5.15.2/lib/fonts目录:

benjamin@ubuntu:~/Qt/Tools/QtCreator/share/qtcreator/fonts$ ls
SourceCodePro-Bold.ttf  SourceCodePro-Regular.ttf
SourceCodePro-It.ttf    SourceCodePro.txt

问题6:

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'

解决办法:
/etc/profile末尾增加两句

export XDG_RUNTIME_DIR=/usr/lib/
export RUNLEVEL=3

7.其它参考资料

QT 5.15.2 ubuntu 静态编译:
https://www.jianshu.com/p/a10a254e4e4e
涉及到的build.sh修改如下:

#! /bin/bash

QT_INSTALL_PATH="-prefix /home/benjamin/Qt_static"     #Qt安装路径(自己修改)
QT_COMPLIER+="-platform linux-g++-64"  #编译器

CONFIG_PARAM+="-static "               #静态编译
CONFIG_PARAM+="-release "             #编译release
CONFIG_PARAM+="-recheck-all "
CONFIG_PARAM+="-nomake examples "    #不编译examples
CONFIG_PARAM+="-nomake tests "        #不编译tests

CONFIG_PARAM+="-xcb -xkbcommon -fontconfig -system-freetype -qt-zlib -qt-libpng -qt-libjpeg "

#选择Qt版本(开源, 商业), 并自动确认许可认证
CONFIG_PARAM+="-opensource "         #编译开源版本, -commercial商业版本
CONFIG_PARAM+="-confirm-license "      #自动确认许可认证

echo "./configure $CONFIG_PARAM $QT_COMPLIER $QT_INSTALL_PATH"
../configure $CONFIG_PARAM $QT_COMPLIER $QT_INSTALL_PATH

在配置-fontconfig打开后,会报如下错误:

ERROR: Feature 'fontconfig' was enabled, but the pre-condition

需要再添加一个-system-freetype,如果它还会报错,安装相关的依赖包:

sudo apt-get install libfontconfig1-dev libfreetype6-dev
  • 5
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值