Ubuntu下编译安装Qt-5.6.2及qtcreator-4.6.0

环境: Ubuntu-14.04

QT源码包: qt-everywhere-opensource-src-5.6.2.tar.gz

QT源码包下载地址:http://download.qt.io/archive/qt/

编译器: g++ (安装: sudo apt-get install g++)

qtcreator安装包:qt-creator-opensource-linux-x86_64-4.6.0.run

qtcreator下载地址:http://download.qt.io/official_releases/qtcreator/


一、安装依赖库 

需安装以下3个包

$ sudo apt-get install libx11-dev libxext-dev libxtst-dev

二、配置、编译、安装

1、解压并进入:

$ tar zxvf qt-everywhere-opensource-src-4.6.2.tar.gz
$ cd qt-everywhere-opensource-src-4.6.2/


2、配置

默认配置即可:

$ ./configure

输出如下,需选择:

root@qihua-virtual-machine:~/library/qt/qt-everywhere-opensource-src-4.6.2# ./configure
Which edition of Qt do you want to use ?

Type 'c' if you want to use the Commercial Edition.
Type 'o' if you want to use the Open Source Edition.

o


This is the  Open Source Edition.

You are licensed to use this software under the terms of
the Lesser GNU General Public License (LGPL) versions 2.1.
You are also licensed to use this software under the terms of
the GNU General Public License (GPL) versions 3.

Type '3' to view the GNU General Public License version 3.
Type 'L' to view the Lesser GNU General Public License version 2.1.
Type 'yes' to accept this license offer.
Type 'no' to decline this license offer.

Do you accept the terms of either license? 

期间会让选择,选"o", "yes"


问题1:

checking for xcb...  yes.
checking for xcb-syslibs...  no.
The test for linking against libxcb and support libraries failed!
 You might need to install dependency packages, or pass -qt-xcb.
 See src/plugins/platforms/xcb/README.

解决1:上面的信息有提示,要么安装独立的包,要么使用 -qt-xcb 项参数。

没有 libxcb 相关的库,那就安装咯:

在 qtbase/src/plugins/platforms/xcb/README 中有说明需安装的包

$ sudo apt-get install libxcb1 libxcb1-dev libx11-xcb1 libx11-xcb-dev libxcb-keysyms1 libxcb-keysyms1-dev libxcb-image0 libxcb-image0-dev libxcb-shm0 libxcb-shm0-dev libxcb-icccm4 libxcb-icccm4-dev libxcb-sync0 libxcb-sync0-dev libxcb-xfixes0-dev libxrender-dev libxcb-shape0-dev libxcb-randr0-dev libxcb-render-util0 libxcb-render-util0-dev libxcb-glx0-dev libxcb-xinerama0-dev


问题2:

正在读取状态信息... 完成       
注意,选取 libxcb-sync-dev 而非 libxcb-sync0-dev
E: 未发现软件包 libxcb-sync0

无解

问题3:

预告:make时出错信息如下,可能也与问题2中安装libxcb-sync0失败有关

In file included from qxcbclipboard.cpp:37:0:
qxcbscreen.h:43:26: fatal error: xcb/xinerama.h: 没有那个文件或目录
 #include <xcb/xinerama.h>
                          ^
compilation terminated.


基于以上有问题未解决,被迫使用QT自带的xcb: -qt-xcb

$ ./configure -qt-xcb

实际上,使make一次通过的命令是这个:(不想走弯路的请用,本教程后续使用上面一条)

$ ./configure -qt-xcb -skip qtdeclarative


配置完成提示:


信息已经说明,只要 make 及 make install,就会安装在 /usr/local/Qt-5.6.2 目录


若不喜欢默认,亦可自定义编译配置,查看help,里面说得很清楚

$ ./configure --help


3、编译

$ make

问题4:

Project ERROR: Unknown module(s) in QT: quick
make[3]: *** [sub-textureandlight-make_first] 错误 3

解决4:配置时加上 -skip qtdeclarative 选项,

$ ./configure -qt-xcb -skip qtdeclarative

再重新 make 。。。。。。

这是个漫长的过程,15:27开始编译。。。。。。。。。。。。。。。。。。。。。。。。。。。大概17:30完成



(亲测编译4.8.6,$ ./configure && make 这两步异常顺利,不会出现任何问题)


4、安装

$ make install

安装一般不会出现什么问题


三、配置环境

1、编辑系统环境变量

$ vi /etc/profile

在最后加上这几行:

 39 export QTDIR=/usr/local/Trolltech/Qt-5.6.2
 40 export PATH=$QTDIR/bin:$PATH
 41 export MANPATH=$QTDIR/man:$MANPATH
 42 export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH

然后使其生效

$ source /etc/profile

2、验证安装

在已重新加载环境变量的情况下,任意目录输入

$ qmake -v

见到以上版本信息,说明安装成功。

下面就写个Hello World程序来测试下。


四、实例测试

1、创建一个目录,编译qt程序:

$ mkdir qt
$ cd qt
$ vi hello.cpp

程序如下:

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    //创建一个QApplication实例
    QApplication a(argc, argv);
    //创建一个label实例
    QLabel  label("Hello Qt");
    //改变label大小
    label.resize(100,50);
    //显示label
    label.show();
    //进入QApplication事件循环
    return a.exec();
  }

2、编译

依次执行以下命令:

$ qmake -project    // 创建 qt.pro文件
$ qmake        // 生成 Makefile
$ make        // 编译,生成 qt
若按正常的流程,至此已编译好可执行文件 qt (文件名取决于文件夹名)

过程如下图:


但却偏偏不正常:

root@qihua-virtual-machine:~/qt# make
g++ -c -pipe -O2 -std=gnu++0x -Wall -W -D_REENTRANT -fPIC -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -I. -I/usr/local/Qt-5.6.2/include -I/usr/local/Qt-5.6.2/include/QtGui -I/usr/local/Qt-5.6.2/include/QtCore -I. -I/usr/local/Qt-5.6.2/mkspecs/linux-g++ -o hello.o hello.cpp
hello.cpp:1:24: fatal error: QApplication: 没有那个文件或目录
 #include <QApplication>
                        ^
compilation terminated.
make: *** [hello.o] 错误 1

网上一搜,在 .pro 文件最后加上这一行

QT += widgets
解决问题!


3、运行

$ ./qt

可见,已运行成功!


五、安装qtcreator

首先下载一个版本的qtcreator-xxx.run文件

1、运行安装:

./qt-creator-opensource-linux-x86_64-4.6.0.run
这个简单,像window上一路按下一步就可以了。


2、添加环境变量:

不想每次都跑到qtcreator的安装目录下运行程序,而想要随时随地调出 qtcreator ,

那么,就将安装目录的bin加入到 /etc/profile 中:

export PATH=/opt/qtcreator-4.6.0/bin:$PATH
再 source /etc/profile 就可以了。


3、运行程序:

在任意目录:

$ qtcreator
即可调出qtcreator的应用程序

PS:当我用 ./configure -qt-xcb 配置时,会有如下的出错:

This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.

Reinstalling the application may fix this problem.
已放弃 (核心已转储)

折腾了一天,都没解决。

情况说明:原本我一开始装的QT版本是4.8.6,装完就装qtcreator,当时是OK的。后来删掉qt-4.8.6,再装5.6.2就完了。

尝试1:删掉qtcreator再重装。                   结果1:failed.

尝试2:重新make install qt-5.6.2。           结果2:failed too.


尝试3:删掉(或mv)qt-5.6.2的安装目录。   

结果3:qtcreator运行成功,终于打开了。但是,创建新项目或运行就失败了。

至此,看来是 QT安装的问题了,一移走库就可以打开。

目测跟配置 xcb 有关,但在ubuntu上装xcb失败了。

得想想办法安装xcb才行。


尝试4:重新配置、编译、安装,这次配置命令:

$ ./configure -no-xcb -skip qtdeclarative

结果4:错误信息换成以下:

root@qihua-virtual-machine:~/project/qt-opencv/image# qtcreator
Cannot mix incompatible Qt library (version 0x50602) with this library (version 0x50a01)
已放弃 (核心已转储)

这个主要因为我之前装了4.8.6版本的,现在又换新版本了,有些配置没清除掉,又不知怎么清除。。。。

所以还是没解决。

百度搜下有说解决方法,把库删掉或移走,但是治标不治本。


待解决中。。。。。。。。。。。。。。。。


至此,linux平台下的qt开发环境已基本安装成功。


猜你喜欢

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