Ubuntu下交叉编译Qt库并移植至ARM板上

环境:Ubuntu-14.04

交叉编译器:arm-linux-gnueabi-4.5.1

qt版本:qt-everywhere-opensource-src-5.6.2


说明:

交叉编译qt库前,若要对触摸屏支持,则需先交叉编译安装tslib。

可参考上一篇【Ubuntu下交叉编译tslib库并移植到ARM板上


一、交叉编译qt库

下载源码包,解压,进入源码目录

$ cd qt-everywhere-opensource-src-5.6.2

1、查看并修改编译架构信息(编译器)

这里应是采用arm-linux架构,进入以入目录,并查看相关信息:

在 qtbase/mkspecs 目录下可见到各种支持的架构,

很明显我们应该选择 linux-arm-架构,打开 qmake.conf 查看编译配置参数:

$ cd qtbase/mkspecs/
$ vi linux-arm-gnueabi-g++/qmake.conf


可见,主要是指定编译器相关的参数,如上图红框。

如果你的编译器不是arm-linux-gnueabi-,那么就要作出相应的修改。

如:我的编译器为 arm-linux- 或 arm-none-linux-gnueabi- ,因此需改成如下:

  1 #
  2 # qmake configuration for building with arm-linux-gnueabi-g++
  3 #
  4 
  5 MAKEFILE_GENERATOR      = UNIX
  6 CONFIG                 += incremental
  7 QMAKE_INCREMENTAL_STYLE = sublib
  8 
  9 include(../common/linux.conf)
 10 include(../common/gcc-base-unix.conf)
 11 include(../common/g++-unix.conf)
 12 
 13 # modifications to g++.conf
 14 QMAKE_CC                = arm-none-linux-gcc
 15 QMAKE_CXX               = arm-none-linux-g++
 16 QMAKE_LINK              = arm-none-linux-g++
 17 QMAKE_LINK_SHLIB        = arm-none-linux-g++
 18 
 19 # modifications to linux.conf
 20 QMAKE_AR                = arm-none-linux-ar cqs
 21 QMAKE_OBJCOPY           = arm-none-linux-objcopy
 22 QMAKE_NM                = arm-none-linux-nm -P
 23 QMAKE_STRIP             = arm-none-linux-strip
 24 load(qt_config)


2、配置编译参数

此次配置,主要配置安装目录,目标平台,以及一些相关模块组件要不要编译进去。。。qt库配置的项非常多,当然后亦可简化一些选默认

先返回源码目录

$ cd ~/library/qt/qt-everywhere-opensource-src-5.6.2

可输入help大概看下配置的参数有哪些选项:

$ ./configure --help

具体配置如下:

写一个脚本配置进入配置 autoconfig.sh :

#!/bin/sh

./configure \
-prefix /usr/local/arm/qt \
-opensource \
-confirm-license \
-release \
-make libs \
-xplatform  linux-arm-gnueabi-g++ \
-pch \
-sql-sqlite \
-qt-libjpeg \
-qt-libpng \
-tslib \
-qt-zlib \
-no-opengl \
-no-sse2 \
-no-openssl \
-no-cups \
-no-glib \
-no-iconv \
-no-separate-debug-info \
-nomake examples -nomake tools \
-no-pkg-config \
-I /usr/local/arm/tslib/include \
-L /usr/local/arm/tslib/lib

嫌麻烦,直接复制下面命令:

$ ./configure -prefix /usr/local/arm/qt -opensource -confirm-license -release -make libs -xplatform  linux-arm-gnueabi-g++ -pch -sql-sqlite -qt-libjpeg -qt-libpng -tslib -qt-zlib -no-opengl -no-sse2 -no-openssl -no-cups -no-glib -no-iconv -no-separate-debug-info -nomake examples -nomake tools -no-pkg-config -I /usr/local/arm/tslib/include -L /usr/local/arm/tslib/lib

注意:若需要支持触摸屏,记得加上 -tslib 项


配置完成如下:

Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into /usr/local/arm/qt

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.


PS:用qt-everywhere-opensource-src-5.8.0版本时,配置时遇到些奇怪的问题:

Note: No wayland-egl support detected. Cross-toolkit compatibility disabled.
...
ERROR: Feature 'tslib' was enabled, but the pre-condition 'libs.tslib' failed

解决:

之前因为装tslib库的时候没验证,后来发现tslib库有问题,猜测是这个问题而导致的。

重装了tslib库并成功验证,再回来配置同样出现这个问题!!!何解?

尝试过多种方法,亲测最佳:删除5.8版本,下个5.8以下的版本重来。(或者换个高版本工具链可能也许OK)

说明:看到一篇博客,说从qt-5.8开始,编译工具链要支持C++11标准的才行,工具链版本要4.8以上!!!


3、编译

$ make

大概要 2 hour。。。。。。。。。。。。。。。。。。。。。。。去吃个饭睡个觉再回来看看

未完待续,睡觉中ZZZZZZ。。。。

一觉醒来,编译完成:



二、安装

$ make install

安装所需的时间很短,一两分钟即可搞定。

完成后,到prefix指定安装目录看看是否安装成功:


检查目录、时间,均对应上了。

15:03也说明我刚真的睡觉去了哈哈


三、移植至ARM板

QT移植是不是也像其他库一样,将lib目录下的库文件移过去就可以了?

不知道,先试试吧。

lib目录下有150M。

内存不足的可自行进行裁剪,没用到的库就不要移过去了。

像我有内存较任性: (我是挂载NFS文件系统, ./ 是我开发板上的 /lib )

$ cp ../usr/local/qt/lib/lib* ./ -a


四、实例测试

1、编译 hello.cpp 文件:

为测试触摸屏,特意加入个滑动条:

#include <QApplication>
#include <QWidget>
#include <QLabel>
#include <QSlider>
#include <QHBoxLayout>

int main(int argc, char **argv)
{
	QApplication app(argc, argv);
	QWidget *window  = new QWidget;
	window->setWindowTitle("I am a slider");

	QLabel *label = new QLabel;		// QLabel控件,用于显示数字
	QSlider *slider = new QSlider(Qt::Horizontal);		// 滑动条
	slider->setRange(0, 100);

	QObject::connect(slider, SIGNAL(valueChanged(int)), label, SLOT(setNum(int)));
	slider->setValue(50);

	QHBoxLayout *layout = new QHBoxLayout;	//level 
	layout->addWidget(label);
	layout->addWidget(slider);
	window->setLayout(layout);

	window->resize(400, 240);

	window->show();

	return app.exec();
}

2、交叉编译

因为 Qt 安装在Ubuntu上已设置了环境变量,交叉编译安装的Qt在Ubuntu上就不设环境变量了。

通常,一个程序先在Ubuntu上跑通了再进行交叉编译,上述程序在Ubuntu上顺利通过。

若编译出错:找不到QApplication,则在 .pro 文件最后加上一行(交叉编译同理): 

QT += widgets

运行:



交叉编译的命令跟编译能在Ubuntu上运行的相似,只不过没配环境则要手动找出交叉编译的qmake的路径:

$ xxx/bin/qmake -project && xxx/bin/qmake && make   // xxx代表交叉编译安装qt的目录


注意红色,交叉编译的qmake的绝对路径。

最后生成的 “arm-qt” 即可运行文件,将其复制到ARM板上。


3、ARM板上运行

激动的一刻,来吧:

$ ./arm-qt

。。。。。。。。。。。。。。。。。。。。。。。。。。

~ # ./arm-qt 
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Reinstalling the application may fix this problem.
Aborted
~ # 

果然不出所料,又如愿地出错了【捂脸】

这个问题,网上找了好久,没找到答案。。。

然后自己折腾下,依稀记得好像忘了配置环境,会不会因为这个?试下,

/etc/profile添加环境变量 tslib + qt:(其中,前部分是之前移植tslib时已加入)

# touchscreen lib - tslib
export TS_ROOT=/usr/local/tslib
export LD_LIBRARY_PATH=$TS_ROOT/lib:$LD_LIBRARY_PATH
export TSLIB_CONSOLEDEVICE=none
export TSLIB_FDDEVICE=/dev/fb0
export TSLIB_TSDEVICE=/dev/event0
export TSLIB_CALIBFILE=$TS_ROOT/etc/pointercal
export TSLIB_CONFFILE=$TS_ROOT/etc/ts.conf
export TSLIB_PLUGINDIR=$TS_ROOT/lib/ts
# qt
export QT_ROOT=/usr/local/qt
export LD_LIBRARY_PATH=$QT_ROOT/lib/:$LD_LIBRARY_PATH
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
export LD_PRELOAD=$TS_ROOT/lib/libts.so

这样一试,还真的是这个问题所致。

期间,还遇到这样一个问题:

~ # ./arm-qt 
Couldnt load module pthres
ts_config() failed (No such file or directory)

解决:在最后加入这一行(上面已加)

export LD_PRELOAD=$TS_ROOT/lib/libts.so


扫清一切障碍之后,终于迎来光明了:


拖动滑动条数字会跟着变,说明一切正常。


搞了几天,终于可以打卡收摊。

                                                                                                                                       -------- 2018-04-30 21:27


猜你喜欢

转载自blog.csdn.net/qq_30155503/article/details/80118705