zl程序教程

您现在的位置是:首页 >  工具

当前栏目

交叉编译Qt工程

Qt 编译 工程 交叉
2023-09-27 14:27:31 时间

交叉编译的概念请查看我之前的一篇文章:如何进行交叉编译?

本文的目标是:在Ubuntu20.04上编译Qt程序并在树莓派上运行。

一、树莓派上安装合适的Qt版本

如果你的板子上没有安装Qt,那么请安装对应的Qt版本。这一步非常重要,最好不要自行安装Qt版本,而是要安装发行版经过测试的、兼容的Qt版本。树莓派安装Qt也相当简单:

sudo apt-get update
sudo apt-get install qt5-default
sudo apt-get install qtcreator
sudo apt-get install qtmultimedia5-dev
sudo apt-get install libqt5serialport5-dev

安装好后,查看Qt的版本信息:
在这里插入图片描述
记下这个版本信息,稍后我们将在我们自己的PC上进行相同qt源码的交叉编译。这样做的目的有:

  • 不需要拷贝动态库到目标主机,减少麻烦
  • 匹配目标主机的Qt,不匹配导致的显示问题

二、下载对应的Qt源码,准备进行编译

2.1 下载源码

Qt官网下载比较缓慢,官方下载地址,可以考虑使用清华镜像源进行下载。地址:清华镜像地址,一个版本可能有很多测试版,较为稳定的版本都是后缀为rc2,点击下载即可。下面是镜像文件夹的说明[1]

目录说明
archive各种Qt开发工具安装包,新旧都有(可以下载Qt开发环境和源代码)
community_releases社区定制的Qt库,Tizen版Qt以及Qt附加源码包
development_releases开发版,新的和旧的不稳定版本,在Qt开发中的非正式版本
learning有学习Qt的文档教程和释放视频
official_releases正式发布版,是与开发版相对的稳定Qt库和开发工具(可以下载Qt开发环境和源代码)
onlineQt在线安装源
snapshot预览版,最新的开发测试中的Qt库和开发工具

在这里插入图片描述

2.2 README说明

解压并查看其内容,包含以下内容:

  • 配置脚本(配置所需模块和目标平台等)
  • 基础模块源码(如core、widget和network等)
  • 附加模块源码(如qtserialport、qt3d等)
  • qt.pro文件(用于指导如何编译所需模块)
  • README (如何去编译qt)

最好是阅读对应版本的README避免因为时效造成的不必要麻烦。README首先告诉编译这些源文件对系统的要求:

  • Perl 5.8 or later
  • Python 2.7 or later
  • C++ compiler supporting the C++11 standard

下面的选项以参数的方式传给configure,
License选择是商用还是开源:

   Licensing:

Opensource users:

    <source_package> = qt-everywhere-opensource-src-<version>
    <license>        = -opensource

Commercial users:

    <source_package> = qt-everywhere-enterprise-src-<version>
    &lt;license&gt;        = -commercial</pre>

如果你选择的是开源编译,那么这部分信息可以这么给定:qt-everywhere-opensource-src-<version> -opensource

平台选择,根据不同平台有不同的编译方法,这里我只关心Linux下的编译方法:

   Linux, Mac:

 cd &lt;path&gt;/&lt;source_package&gt;
 ./configure -prefix $PWD/qtbase &lt;license&gt; -nomake tests
 make -j 4

查看配置说明是非常重要的。配置的其他选项,可以使用传递–help给对应的configure脚本,它将返回一系列的选项含义,这一点非常重要,网上大多数的交叉编译都是针对某一特定版本的,选项有些或许已经过期,需要根据实际需要选择。

最后README给出一些常见选项,如:

 Some relevant configure options (see configure -help):

 -release              Compile and link Qt with debugging turned off.
 -debug                Compile and link Qt with debugging turned on.
 -nomake tests         Disable building of tests to speed up compilation
 -nomake examples      Disable building of examples to speed up compilation
 -confirm-license      Automatically acknowledge the LGPL 2.1 license.

版本对应帮助文档,当前Qt版本对应的接口文档安装。

 Documentation

After configuring and compiling Qt, building the documentation is possible by running
"make docs".-sysroot

After having built the documentation, you need to install it with the following
command:

make install_docs

默认安装位置在变量QT_INSTALL_DOCS,可以使用qmake --query查询。

三、进行Qt源码编译

3.1 应该附加什么选项

Qt源码编译并安装后,将会给定qmake版本,这个qmake版本至少指定了以下几个内容:

  • -prefix,编译好Qt的安装路径
  • -opensource,开源代码编译
  • -confirm-license,省去你在编译过程中的确认
  • -debug,附加Qt库附带debug信息(默认是release)
  • -xplatform <target>,目标平台(树莓派开发板)
  • -skip <repo>,跳过不需要的模块

网上一些经常会有这样一些选项:

  • no-opengl,不需要,说不定以后要用到opengl;
  • no-pch ,不能,因为版本没有;
  • shared ,不需要,默认值;
  • no-iconv,不能,只支持Unix平台;
  • no-xcb,不能,当前版本没有;

如果你的电脑空间不紧张且编译时间不太急的话,最好不要skip任何模块,万一哪一天需要用时再次编译就费事了。

3.2 做一些小修改

进入qt-everywhere-src-5.11.0-rc2/qtbase/mkspecs查看是否有对应交叉编译工具对应的配置文件,如果有,将xplatformtarget设置为对应的配置文件夹一样即可。交叉编译工具名称为arm-none-linux-gnueabihf,但是我只在mkspecs中找到arm-none-linux-gnueabi,大概是因为当时没有出硬浮点的arm?

若不一样,按照以下步骤进行修改:

  • 拷贝linux-arm-gnueabi-g++配置文件,新的名字为alinux-arm-gnueabihf-g++
cp -r linux-arm-gnueabi-g++ linux-arm-gnueabihf-g++
  • 进入新建好的文件夹linux-arm-gnueabihf-g++,修改qmake.conf中的编译套件名字为当前交叉编译工具链,修改后的qmake.conf:
# qmake configuration for building with arm-linux-gnueabihf-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-linux-gnueabihf-gcc
QMAKE_CXX               = arm-linux-gnueabihf-g++
QMAKE_LINK              = arm-linux-gnueabihf-g++
QMAKE_LINK_SHLIB        = arm-linux-gnueabihf-g++

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

3.3 编写编译脚本

#!/bin/bash
./configure -prefix /usr/local/qt5.12.10forPI \
-opensource \
-confirm-license \
-xplatform linux-arm-gnueabihf-g++ \
-debug \
-no-opengl \

非常奇怪,对应版本的Qt5.11.0交叉编译过程中出现了如下错误

 error: no matching function for call to ‘std::mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>::mersenne_twister_engine(QRandomGenerator::SystemGenerator&)’

查了一下,好像是版本的bug。所以我换成了附近的发行版5.12,开启多线程编译

make -j4

等了好一会才编译完成,将这个库安装到系统。

make install

3.4 测试

在这里插入图片描述

[1] https://blog.csdn.net/junxuezheng/article/details/102394021