Building Qt 5 from Git/zh

From Qt Wiki
Jump to navigation Jump to search
This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh


在Git中构建Qt 5

简介

本文将给出一些提示,来让你从Git中获取模块化的Qt5并构建它(在桌面平台上)。请随时更新本篇文章以反映开发过程中的变化。通过 http://forum.qt.io/viewthread/7018 来报告本文相关的问题。

如果你也想编译Qt Creator,参考 Building Qt Creator from Git.

系统要求

所有的桌面平台

  • Git (>= 1.6.x)
  • Perl (>=5.14)
  • Python (>=2.6.x)
  • 可以工作的编译器

如需更多详细信息, 参考 Building Qt Sources

SSL (可选)

参考 Enabling and Disabling SSL Support

WebKit (可选)

对于 Windows来说, bison, flexgperf 可以从位于 <qt>32\bin 的Git仓库获得。你可以从http://www.rubyinstaller.org/ 获得Ruby。你可以从 icu-project.org (MSVC 2010 only)下载提前编译好的ICU软件包,或者 自行编译

Linux/X11

软件包的简易安装 (仅限Ubuntu 11.10 — 12.10 )

对于Ubuntu/Debian, Gabor Loki 已经提供了一个定制的PPA, 使用 sedkit-env-webkit meta package 你可以安装Qt/Qt WebKit所需的全部依赖包。你可以通过如下命令添加PPA:

sudo apt-add-repository ppa:u-szeged/sedkit && sudo apt-get update && sudo apt-get install sedkit-env-qtwebkit

对于其它的发行版,以如下的方式取得各自的组件。

Build-essentials

Ubuntu/Debian:
sudo apt-get install build-essential perl python git

===== Fedora:

su - -c "yum install perl-version"

=====

OpenSUSE:
sudo zypper install git-core gcc-c++ make

Libxcb

Libxcb 是基于X11/Xorg的平台的默认窗口系统后端,因此你应该安装libxcb及其附随的软件包。 不管xcb在你的系统上是什么版本,Qt5 应该都可以构建。(但是最好使用 v1.8 或者更高的版本). src/plugins/platforms/xcb/README 列出了所需的xcb软件包。

Ubuntu/Debian:
sudo apt-get install "^libxcb.'''" libx11-xcb-dev libglu1-mesa-dev libxrender-dev
Fedora up to 16:
su - -c "yum install libxcb libxcb-devel xcb-util xcb-util-devel"
Fedora 17+ (xcb-util-renderutil is currently available in updates repository until it goes stable):
su- -c "yum —enablerepo=updates install libxcb libxcb-devel xcb-util xcb-util-devel xcb-util-'''-devel libX11-devel libXrender-devel"

===== OpenSuSE 12.2

sudo zypper in xorg-x11-libxcb-devel xcb-util-devel xcb-util-image-devel xcb-util-keysyms-devel xcb-util-renderutil-devel xcb-util-wm-devel xorg-x11-devel libxkbcommon-devel

=====

===== ArchLinux:

sudo pacman -S needed libxcb xcb-proto xcb-util xcb-util-image xcb-util-wm

=====

在Chakra Linux上,除了ArchLinux所提到的软件包,你需要单独安装xcb-util-keysyms 软件包,其可以通过CCR得到。

===== Mandriva/ROSA/Unity:

urpmi 'pkgconfig(xcb)' 'pkgconfig(xcb-icccm)' 'pkgconfig(xcb-image)' 'pkgconfig(xcb-renderutil)' 'pkgconfig(xcb-keysyms)' 'pkgconfig(xrender)'

=====

Centos 5/6

安装Qt构建时所缺失的依赖:

yum install libxcb libxcb-devel xcb-util xcb-util-devel

你需要为CentOS-5/6 x86_64 安装 Red Hat DevTools 1.1,因为默认的CentOS上安装的是过时的GCC。

wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo -O /etc/yum.repos.d/devtools-1.1.repo
yum install devtoolset-1.1

你还需要初始化刚安装的开发工具:

scl enable devtoolset-1.1 bash

# Test- Expect to see gcc version 4.7.2 ( * not * gcc version 4.4.7 )
gcc -v

如需*CentOS*上的更多相关信息,参考 这篇文章.

OpenGL support

因为Qt Quick 2,推荐系统上有原生的OpenGL 2.0 驱动支持。

Accessibility

推荐使用accessibility来构建, 安装 libatspi 2libdbus-1 开发包。

Qt WebKit

Ubuntu/Debian:
sudo apt-get install flex bison gperf libicu-dev libxslt-dev ruby
Fedora:
su- -c "yum install flex bison gperf libicu-devel libxslt-devel ruby"

===== OpenSUSE:

sudo zypper install flex bison gperf libicu-devel ruby

=====

===== Mandriva/ROSA/Unity:

urpmi gperf

=====

Qt Multimedia

你至少需要安装alsa-lib (>= 1.0.15) 和 gstreamer (>=0.10.24, but <1.0 for now) 以及基本的插件包。

===== Ubuntu/Debian:

sudo apt-get install libasound2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev

=====

OS X

从 App Store安装最新的Xcode。 检查Xcode的安装是正确配置的:

xcodebuild -version && xcodebuild -showsdks

应该输出,比如:

Xcode 4.6.1
Build version 4H512
OS X SDKs:
 Mac OS X 10.7 -sdk macosx10.7
 OS X 10.8 -sdk macosx10.8

iOS SDKs:
 iOS 6.1 -sdk iphoneos6.1

iOS Simulator SDKs:
 Simulator- iOS 5.0 -sdk iphonesimulator5.0
 Simulator- iOS 6.1 -sdk iphonesimulator6.1

通过如下命令,你可以检查当前使用的Xcode是否正确:

xcode-select print-path

如果指向 /Developer ,你可能使用的是旧版本的Xcode。通过如下命令切换到最新版本:

sudo xcode-select switch /Applications/Xcode.app/Contents/Developer

Windows

Windows 图形驱动

QML2 需要 OpenGL 2.1(或者更高版本) 或者Open GL ES 2.0 才能工作。

在Windows上, 有两种办法:

  1. 使用 ANGLE-library 来将OpenGL调用映射为 DirectX (默认)
  2. 使用Windows系统上显卡厂商提供的原生OpenGL 驱动

Qt 5中已经绑定了一份ANGLE。如果要使用方法1,你需要安装 DirectX SDK (注意: 从Windows Kit 8开始,它被包含在Windows SDK中)。

如果要使用方法2,你需要确保你的显卡驱动支持OpenGL 2.1 或者更高版本 (注意:微软官方的Windows驱动仅支持OpenGL 1.1,这显然是不够的),然后在使用configure.exe时加上参数'-opengl desktop' 。

Windows上支持的编译器

  • Visual Studio 2013
  • Visual Studio 2012
  • Visual Studio 2010 or Windows SDK v7.0A (可以升级至 Windows SDK v7.1)
  • Visual Studio 2008 or Windows SDK v6.0A (可以升级至 Windows SDK v6.1)
  • MinGW-w64 based compiler with _g+_ version 4.7 或者更高 (比如 MinGW-builds, 可以参考 MinGW-64-bit).

注意:

  • Windows SDK v6.0A/v7.0A 包含和 Visual Studio 2008/2010同样的编译器。
  • Windows SDK 8.0 和更新的版本中没有包含编译器。
  • 自16.3.2012起,如果你想要同时安装Visual Studio 2010 和单独的SDK,可按照如下顺序(参考服务包中提供的readme.html):
    1. 安装 Visual Studio 2010
    2. 安装 Windows SDK 7.1. 可参考 Cannot_Install_Windows_SDK 页面。
    3. 安装 Visual Studio 2010 SP1
    4. 为Windows SDK 7.1安装 Visual C+ 2010 SP1编译器更新。

Windows 构建环境

我们推荐使用命令提示符窗口来提供Qt5的构建环境(参考 Qt Creator README )。在这个环境中, Python (例如 Active Python 2.7 或更新版本) 和 Perl (例如 Active State Perl 5.12 或更新版本) 应该在 PATH 环境变量中。 获取Perl: http://www.activestate.com/activeperl/downloads 获取Python: http://www.python.org/download/releases/ 获取Ruby: http://www.rubyinstaller.org/downloads/

提示:确保 Perlgit 前面添加到path, 否则将会用到过时的版本 (Perl 5.8),从而导致脚本运行失败。

多核构建: 当使用 nmake 并为了加快编译速度,编译器可以通过如下方式中的一种来利用所有的cpu核:

  • 向 Qt的 configure 传递选项 _-MP_
  • 设置环境变量 CL (指定Visual Studio 编译器的选项) 为 /MP (在命令行上:
    set CL=/MP
    
    )
  • 使用工具 jom 来代替nmake。

Windows上的ICU

Qt 5 可以通过 ICU 库来得到UNICODE和 Globalization 支持。 构建Qt Webkit时将会 用到 。你可以使用提前编译好的ICU, 或者 "自行编译 ICU":Compiling-ICU .

编译时,在调用 Windows SDK的配置脚本后, ICU的_include_ 和 lib 的绝对路径必须包含在 INCLUDELIB 环境变量中。

运行时, ICU DLLs必须能够被找到。可以将这些 DLLs 拷贝到应用程序文件夹,或者将ICU的 bin 的绝对路径添加到 PATH 环境变量中。


获取源码

首先克隆 git 中 Qt5 的顶层仓库:

git clone git://code.qt.io/qt/qt5.git qt5

或者 (如果你位于防火墙之后并想使用https协议):

git clone https://code.qt.io/qt/qt5.git qt5

然后切换到目标分支上(参考 Branch-Guidelines):

cd qt5
git checkout stable

按照 README.git- 文件,我们使用 init-repository 脚本来初始化仓库,从而可以克隆各个Qt5的子模块。相关选项:

  • --no-webkit
    
     : 此选项表明你不使用qtwebkit,这个模块非常大,编译将花费很长的时间而且是编译错误的主要来源。所以建议只有当你想使用它时再下载它。而且你可以在以后重新执行 init-repository来下载此模块。
  • --http
    
     : 如果位于防火墙之后就使用此选项。
  • --codereview-username <Jira/Gerrit username>
    
     : 如果你打算向Qt贡献代码,应当指定你的 codereview username 从而可以恰当的设置git remotes。
cd qt5
perl init-repository

配置与构建

Qt5的构建系统应完美灵活的防止"外部干扰" - 它应该不介意你是否在PATH中包含有其它版本的Qt,并且几乎完全忽略掉QTDIR变量。然而,你也要确保没有设置qmake特定的环境变量,像是QMAKEPATH 或者QMAKEFEATURES, 并且qmake -query 的输出不应当指向任何其它Qt版本 ($HOME/.config/Trolltech/QMake.conf 应当为空)。

从最上层目录配置构建。禁用 tests 和 examples 模块将会显著加快编译速度:

Linux / OS X 上:

./configure -developer-build -opensource -nomake examples -nomake tests

Windows上:

configure -developer-build -opensource -nomake examples -nomake tests

The

-developer-build

选项将会比正常Qt编译导出更多的符号,从而允许更多的类和函数进行单元测试。它同时默认进行了debug构建,并且将编译好的二进制文件安装在当前目录来避免 'make install'。 '-opensource' 选项将许可证设置为LGPL 2.1。

-nomake examples

-nomake tests

参数确保examples 和 tests 不会被默认编译,你可以在以后重新指定参数来再次进行编译。

  • 提示1: 在Linux上你应当使用
    -no-gtkstyle
    
    . This is because on a number of systems (at least SUSE and Gentoo)
    pkg-config cflags gtk+2.0
    
    actually returns paths that include the system Qt 4.x include directories.
  • 提示2:你可以使用
    -confirm-license
    
    参数来去掉关于许可证的问答。
  • 提示3:在Windows上, 如果
    sh.exe
    
    存在于你的 PATH (比如由于 git 或者 msys 的安装),你可能不能编译。 这样的错误信息大概是
    qt5-srcqtbasebinqmake.exe: command not found
    
    。如果这样的话,确保
    sh.exe
    
    不在PATH中。如果你的安装已经配置过的话,那么你必须重新配置。

现在通过以下步骤来进行编译:

对于 Linux / OS X:

make -j4 all

如果你在进行out-of-source 的开发者模式的编译,并且包含了编译QtWebKit 模块,那么上面的命令会在编译到QtWebKit的时候返回错误。可以通过下面的方式来进行编译,也就是忽略了 all 参数:

make -j 4

对于 Windows (MSVC),选择下面的一种方式来进行编译(取决于你的环境):

nmake
jom
mingw32-make

或者仅仅编译一个特定的模块,比如, declarative及其依赖的模块:

make module-qtdeclarative

编译 Qt WebKit

Windows

WebKit.org 有在Windows上编译Webkit的指令。编译时需要 ICU

为了方便,编译所需的工具 bison, flexgperf 已经提供并存放于 gnuwin32\bin. 如果你在使用shadow 编译,你必须将此路径添加在PATH中,除此之外无需更多特别的操作就可以使用它们了。

安装 (Linux / OS X)

  • 注意: 只有在configure时没有使用
    -developer-build
    
    或者
    -prefix "%PWD%/qtbase"
    
    选项 ,安装才是需要的。否则你可以直接从编译目录中使用Qt。

要安装,运行

make install

清理

为了得到 确实 干净的目录,运行:

git submodule foreach recursive "git clean -dfx"

因为在仓库的最上层 make confclean 不再有效。

更新

要同时更新 qt5.git 仓库以及其子模块至最新的有效版本,运行:

git pull
git submodule sync
git submodule update recursive

如果你准备每日构建, 考虑使用脚本 qt5_tool ,其存放于 qtrepotools/bin 路径下。 它提供了更新仓库、清理、编译的选项。比如,

qt5_tool -u -c -b

用来清理、更新和编译。

qt5_tool -p -c- b

用来将所有模块合并更新到它们主分支的HEAD。

你可能需要重新运行configure,这取决于源代码产生了什么样的改变。为确保起见,你可以先清理,然后再configure和make。

  • 提示1:
    git submodule update
    
    does a checkout in submodules, potentially hiding any local commits you've done! If the latter happened to you (and you haven't been working with branches anyhow),
    git reflog
    
    is your friend ….
  • 提示2: When creating scripts for updates on Windows, note that
    git clean
    
    often fails if some process locks a file or folder.

在子模块中使用最新分支

默认检出是不会包含各自子模块的最新的stable/dev 分支,而是一个能在一起有效工作的最新联合版本。如果你想得到子模块的最新改动,你可以在每个子模块中执行,比如:

cd qtdeclarative
git fetch
git checkout stable origin/stable

或者使用qt5_tool 来更新所有的仓库:

./qtrepotools/bin/qt5_tool -p

不过这样做的话,有可能编译会失败! 这样的话,你得向工作在当前问题模块上的人寻求帮助,来解决模块之间不兼容的问题。

Some advanced shell tricks can be useful when you are making or reviewing changes in multiple modules.

问题

Linux

configure 失败,错误信息"No QPA platform plugin enabled!" (Linux)

你需要安装libxcb 及其附随的软件包, 参考 'System Requirements'.

configure失败,错误信息像是 "cannot stat file …"

你的perl版本太旧了, Qt 5 beta1 需要至少 5.14。

qmlscene segfaults "Cannot create platform GL context, none of GLX, EGL, DRI2 is enabled" (Linux)

安装 libx11-xcb-dev 包:

sudo apt-get install libx11-xcb-dev

之后你必须重新运行configure 并且强制重新编译qtbase/src/plugins/platforms/xcb 。

WebKit doesn't compile, missing ICU

当前configure时并不会检查ICU,所以出现此错误时,通过以下方式来安装相应的包

在Ubuntu/Debian上:

sudo apt-get install libicu-dev

在Fedora上:

su- -c "yum install libicu-devel"
  • You can also compile Qt without Qt WebKit by deleting / renaming the qtwebkit, qtwebkit-examples-and-demos directories.
  • The —no-webkit option of configure added, see QTBUG-20577 issue.

Qt D-Bus fails to build due to "inconsistent user-defined literal suffixes"

This occours when you attempt to build Qt 5 with GCC 4.7 while D-Bus < 1.4.20 is present on your system. (For example, the default Fedora 17 installation is prone to this error.) The error message is this:

qdbusinternalfilters.cpp:124:36: error: inconsistent user-defined literal suffixes DBUS_INTROSPECT_1_0_XML_PUBLIC_IDENTIFIER and DBUS_INTROSPECT_1_0_XML_SYSTEM_IDENTIFIER in string literal

Note: The error is in the header files of D-Bus itself, and it has been fixed upstream, see https://bugs.freedesktop.org/show_bug.cgi?id=46147 Solution: either upgrade to a newer version of D-Bus or edit that one line of the header file manually.

[…]::isNull is not defined (from qvariant_p.h)

C+11 support is detected while your GCC doesn't properly support it Fixed by passing

-no-c11<code>
to the configure options

==== cc1: fatal error: .pch/release-shared/QtGui: No such file or directory ====
Currently unresolved bug with the build of assembly files, see discussion at http://comments.gmane.org/gmane.comp.lib.qt.devel/5933
Fixed by passing

-no-pch

to the configure options

ld: hidden symbol `void QQmlThread::postMethodToThread<QQmlDataBlob*, QQmlDataBlob*, QQmlDataLoaderThread>(void (QQmlDataLoaderThread::)(QQmlDataBlob), QQmlDataBlob* const&)' isn't defined

Bug with GCC versions < 4.4.x, see bug report at https://bugzilla.redhat.com/show_bug.cgi?id=493929 Fixed by adding

QMAKE_CXXFLAGS_RELEASE''= -fno-inline<code>
in '''qtdeclarative/src/qml/qml.pro'''

=== Windows ===

==== qdeclarative 由于缺少v8.lib 而不会编译(Windows) ====

Compiling v8 in qtbase requires Python. Make sure you have a python.exe in your %PATH%, go to qtjsbackend, run qmake && nmake.

==== 调试 OpenGL 问题 (Windows) ====

Set the environment variable <code>QT_QPA_VERBOSE=gl:1

and run the application with DebugView installed. The log will show the requested vs obtained OpenGL version. If the log tells you that it only has OpenGL 1.1, QML2 will not work. Note that qmlscene will not report errors about unsupported OpenGL versions.

疑问&建议