Building Qt 5 from Git

本文提供了一步一步的指南,说明如何从Git仓库克隆Qt5源码,配置系统环境,下载并编译Qt5,以及解决可能出现的问题。包括针对不同操作系统的系统需求,详细的操作步骤,以及在Windows上使用特定工具如ANGLE和DirectX SDK的说明。此外,还介绍了如何安装QtWebKit、清理和更新代码库的方法,以及在遇到常见错误时的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

This article will try to give some hints for checking out the modularized Qt5 from git and getting it built. Please feel free to update this article as things change during development. Raise issues related to the article on http://developer.qt.nokia.com/forums/viewthread/7018 .

System Requirements

You need obviously git (>= 1.6.x) to get the packages. To properly configure and build Qt 5 from source you also need Perl and Python, as well as a working compiler.

Linux/X11

Build essentials on Ubuntu/Debian:

  1. sudo apt -get install build -essential perl python git

Libxcb   [xcb.freedesktop.org]  is now the default window-system backend for platforms based on X11/Xorg, and you should therefore install libxcb and its accompanying packages. Qt5 should build with whatever libxcb version is available in your distro’s packages (but you may optionally wish to use v1.8 or higher to have threaded rendering support). src/plugins/platforms/xcb/README   [qt.gitorious.org]  lists the required packages.

Libxcb and friends on Ubuntu/Debian:

  1. sudo apt -get install  "^libxcb.*" libx11 -xcb -dev libglu1 -mesa -dev libxrender -dev

Libxcb and friends on Fedora up to 16:
  1. su  -  -"yum install libxcb libxcb-devel xcb-util xcb-util-devel"
Libxcb and friends on Fedora 17+ (xcb-util-renderutil is currently available in updates-testing repository until it goes stable):
  1. su  -  -"yum --enablerepo=updates-testing install libxcb libxcb-devel xcb-util xcb-util-devel xcb-util-*-devel"
perl-version on Fedora
  1. su  -  -"yum install perl-version"
Libxcb on ArchLinux:
  1. sudo pacman  ---needed libxcb xcb -proto xcb -util xcb -util -image xcb -util -wm

On Chakra Linux, other than packages mentioned for ArchLinux, you need to install package xcb-util-keysyms separately. It’s available from CCR.

Libxcb and friends on Mandriva/ROSA/Unity:
  1. urpmi  'pkgconfig(xcb)'  'pkgconfig(xcb-icccm)'  'pkgconfig(xcb-image)'  'pkgconfig(xcb-renderutil)'  'pkgconfig(xcb-keysyms)' 'pkgconfig(xrender)'

For QtQuick2 a graphics driver with native OpenGL 2.0 support is highly recommended.

QtJsonDb depends on libedit:

QtJsonDb dependencies on Ubuntu/Debian:

  1. sudo apt -get install libedit -dev

QtJsonDb dependencies on Mandriva/ROSA/Unity:

  1. urpmi  'pkgconfig(libedit)'

When you want to compile webkit, more dependencies need to be satisfied:

Webkit dependencies on Ubuntu/Debian:

  1. sudo apt -get install flex bison gperf libicu -dev libxslt -dev

Webkit dependencies on Fedora:
  1. su  -  -"yum install gperf"
Webkit dependencies on Mandriva/ROSA/Unity:
  1. urpmi gperf
QtWayland dependencies

To build the QtWayland module, the Wayland [wayland.freedesktop.org] development libraries should be installed ( libxcomposite-dev and libwayland-dev ) .

Mac OS X

XCode and git should be enough.

Windows

Windows Graphics Drivers

For QML2 to work, you need an OpenGL driver (NVidia,etc) that provides OpenGL 2.1 or higher for your graphics card. The Windows default driver provides only OpenGL 1.1, which is not sufficient.

Alternatively, the ANGLE-library [code.google.com] , which provides Open GL ES 2.0 by translating Open GL calls into Direct X, can be used. In order to build it, you need to

  1. Install the Direct X SDK [msdn.microsoft.com]
  2. Checkout and build ANGLE (SVN repository) as explained here [code.google.com]
  3. Note: When building for 64bit, de-activate the WarnAsError option in every project file (as otherwise integer conversion warnings will break the build).
  4. When configuring Qt, pass the command line options -opengl es2 -angle <path>
Supported Compilers on Windows
  • Windows SDK 7.1 with Visual Studio 2010 (recommended). As of 16.3.2012, installing the SDK requires installing the following packages in this order (see readme.html provided with the service pack):
  1. Install Visual Studio 2010
  2. Install Windows SDK 7.1
  3. Install Visual Studio 2010 SP1
  4. Install Visual C++ 2010 SP1 Compiler Update for the Windows SDK 7.1
  • Windows SDK 7 with Visual Studio 2008
  • MinGW [mingw.org] or TDM MinGW [tdm-gcc.tdragon.net] with g++ version 4.6 or higher (not supported for all modules)
Windows Build environment

We recommend creating a command prompt that provides the build environment (see the Qt Creator README [qt.gitorious.org] ). In this environment, Python (e.g. Active Python 2.7 later) and Perl (e.g. Active State Perl 5.12 or later) should be in the PATH.

Multicore building: When using nmake, the environment variable CL (specifying Visual Studio compiler options) can be set to set CL=/MP, instructing the compiler to use all cores. Alternatively, the tool jom [qt-project.org] can be used. Note however, that it has issues with module interdependencies, which can cause builds to fail.

Hint: Make sure that Perl is added to the path in front of git since that ships an outdated version (Perl 5.8), which will cause the scripts to fail.

ICU on Windows

Qt 5 can make use of the ICU [site.icu-project.org] library for UNICODE and Globalization support. This is required for building QtWebKit.

At compile time, the include and lib folders of the ICU installation must be appended to INCLUDE and LIB environment variables after calling the setup script of the Windows SDK.

At run-time, the ICU DLLs need to be found. This can be achieved by copying the DLLs to the application folder or adding the bin folder of the ICUinstallation to the PATH environment variable.

Getting the source code

First clone the top-level Qt 5 git repository:

  1. git clone git : //gitorious.org/qt/qt5.git qt5

or (if you’re behind a firewall and want to use the https protocol):

  1. git clone https : //git.gitorious.org/qt/qt5.git qt5

Following the README [qt.gitorious.org] – file we initialize the repository using the script init-repository which clones the various sub-modules of Qt5. Relevant options:

  • —no-webkit : Consider skipping qtwebkit by passing this option. This module is quite big, takes a long time to compile and if often a source of compile errors, so it is recommend to only download it if you intend to use it. You can always re-run init-repository later on to add it.
  • —http : Pass this option if you’re behind a firewall.

  1. cd qt5
  2. perl init -repository

If you plan to contribute to Qt, you should specify your codereview username so that the git remotes are properly set up:

  1. perl init -repository  --codereview -username  <Jira /Gerrit username >

Configuring and building

Setup your path so that we pick up the Qt5 tools rather than any old Qt4 ones that may be present on your system:

For Linux/Mac OS X:

  1. unset QTDIR
  2. export PATH = "$PWD/qtbase/bin:$PWD/qtrepotools/bin:$PATH"

For Windows:

  1. set QTDIR =
  2. set PATH =%CD %\qtbase\bin ;%CD %\qtrepotools\bin ;%PATH %

Now configure the build (from top level dir). Disabling tests and examples will greatly speed up compilation:

For Linux/Mac OS X:

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

For Windows:

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

The -developer-build options export more symbols than in a traditional Qt build in order to allow more classes and functions to be unit tested. It also defaults to a ‘debug’ build, and installs the binaries in the current directory, avoiding the need for ‘make install’. ‘-opensource’ sets the license to be LGPL 2.1. The -nomake examples and -nomake tests parameters make sure examples and tests aren’t compiled by default. You can always decide to compile them later by hand.

  • Hint1: On Linux you should also pass -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 Qt4.x include directories.
  • Hint2: You can add -confirm-license to get rid of the question whether you agree to the license.
  • Hint3: On Windows, you might not be able to build if sh.exe is in your PATH (for example due to a git or msys installation). Such an error is indicated by qt5-srcqtbasebinqmake.exe: command not found and alike. In this case, make sure that sh.exe is not in your path. You will have to re-configure if your installation is already configured.

Building happens by invoking the build perl script:

For Linux:

  1. . /build  -3

For Windows (MSVC):

  1. perl build

Or just make a specific module, e.g. declarative, and modules it depends on:

  1. make module -qtdeclarative

*Hint 1: The build script should be used for all configuration. For in-source developer builds, executing the platform’s make command also works.
*Hint 2: jom, the drop-in replacement of nmake on Windows, doesn’t work right now for top-level builds. You can, however, use it to (re-)build individual submodules.

Building QtWebKit

Linux, Mac

The tools bisonflex and gperf which are required for building.

  1. cd  qtwebkit
  2. export WEBKITOUTPUTDIR = "$PWD/WebKitBuild"
  3. perl Tools /Scripts /build -webkit  -- qt  --qmake = "$PWD/../qtbase/bin/qmake"  --install -libs =<install dir >  --debug  --makeargs = "$MAKEFLAGS"

Windows

WebKit.org [trac.webkit.org] has instructions for building WebKit on Windows. Besides ICU [site.icu-project.org] library , phtreads-win32[sourceware.org] is required. After unzipping a release package (say to c:\phtreads) , you need to:

  • Add c:\phtreads\pthreads.2 to the INCLUDE environment variable
  • Add c:\phtreads\Pre-built.2\lib\x64 or c:\phtreads\Pre-built.2\lib\x86 to the LIB environment variable
  • Add c:\phtreads\Pre-built.2\dll\x64 or c:\phtreads\Pre-built.2\dllx86 to the PATH environment variable

The tools bisonflex and gperf which are required for building are provided for convenience in the folder gnuwin32\bin.

  1. set PATH =%PATH %;%CD %\gnuwin32\bin
  2. cd  qtwebkit
  3. set WEBKITOUTPUTDIR =%CD %\WebKitBuild
  4. perl Tools\Scripts\build -webkit  -- qt  --qmake = "%CD%\\..\\qtbase\\bin\\qmake.exe"  --install -libs =<install dir >  --release  --makeargs = "%MAKEFLAGS%"

Installing (Linux / Mac OS X)

  • Note: Installation is only needed if you haven’t used the configure options -developer-build or -prefix “PWD/qtbase”. Otherwise you can just use Qt from the build directory.

To install, run

  1. make install

But if you’re installing into a non-standard path for testing qt5, you’ll need to set your path to include the installation directory’s bin directory. You should either make sure you don’t have QTDIR set in your environment, or set QTDIR to the installation path before running make install. For example, if installing into /opt/qt5 (this assumes you are using bash; for csh-derived shells you’ll need to run setenv equivalents first):

  1. PATH =/opt /qt5 :$PATH QTDIR =/opt /qt5 make install

Cleaning

To really get a clean tree use:

  1. git submodule  foreach  --recursive  'git clean -dfx'

since make confclean no longer works from the top-level of the repo.

Getting updates

To update both the qt5.git repo as well as the submodules to the list of revisions that are known to work, run

  1. git pull
  2. git submodule update  --recursive

If you are planning to do nightly builds, consider using the script qt5_tool that lives in qtrepotools/bin. It provides options for updating the repository, cleaning and building. For example, qt5_tool -u -c -b can be used to clean, update and build. qt5_tool -p -c – b would be used to pull all modules to the head of their master branches.

  • Hint1: git submodule update does a hard reset on submodules, potentially getting rid of 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 ….
  • Hint2: When creating scripts for updates on Windows, note that git clean often fails if some process locks a file or folder.

Using master branches in the submodules

By default the checkout will not contain the latest master branches of each individual submodule repository, but a combination of versions that are known to work together. If you want to get the absolute latest stuff you can do so on a per-module basis, e.g.

  1. cd qtdeclarative
  2. git fetch
  3. git checkout master origin /master

or use qt5_tool to update all repositories to master:

  1. . /qtrepotools /bin /qt5_tool  -p

However, there’s a good chance that compilation will fail due to incompatible versions of submodules! You might want to ask other persons actively working on a module how to resolve these incompatibilities.

Issues

Linux

configure fails with “No QPA platform plugin enabled!” (Linux)

You should install the libxcb and it’s accompanying packages, see ‘System Requirements’.

qmlscene segfaults “Cannot create platform GL context, none of GLXEGL, DRI2 is enabled” (Linux)

Try installing the libx11-xcb-dev package:

  1. sudo apt -get install libx11 -xcb -dev

afterwards you have to re-run configure and force qtbase/src/plugins/platforms/xcb to recompile.

WebKit doesn’t compile, missing ICU

Currently there is no configure time check for ICU, so install it through the package manager through

on Ubuntu/Debian:

  1. sudo apt -get install libicu -dev

on Fedora:

  1. yum search ICU

Alternatively you can also compile Qt without webkit by deleting / renaming the qtwebkit, qtwebkit-examples-and-demos directories. The -no-webkit option of configure does not work yet : https://bugreports.qt-project.org/browse/QTBUG-20577

Windows

qdeclarative doesn’t compile because of missing 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.

Debugging OpenGL issues (Windows)

Set the environment variable QT_QPA_VERBOSE=gl:1 and run the application with DebugView [technet.microsoft.com] 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.

Questions & Comments

Please raise questions & comments about this article in the forum : http://developer.qt.nokia.com/forums/viewthread/7018

http://qt-project.org/wiki/Building-Qt-5-from-Git

<think>好的,用户遇到了“Could not find qmake spec ''”的错误,并且项目文件是/home/user/桌面/qt-everywhere-src-5.15.16/qt.pro。我需要分析可能的原因,并提供解决方案。 首先,这个错误通常与qmake找不到对应的spec文件有关。Spec文件告诉qmake如何为特定平台生成Makefile。用户可能在构建Qt源码时遇到了这个问题,所以需要检查Qt的安装和配置。 用户的项目文件路径是qt-everywhere-src-5.15.16/qt.pro,这看起来像是Qt的源代码目录。可能用户尝试从源代码构建Qt,但配置不正确。此时,应该确认是否按照官方文档的步骤进行配置,特别是configure脚本的参数是否正确。例如,是否指定了-platform参数,或者是否选择了正确的目标平台。 接下来,可能的原因包括: 1. **未正确运行configure脚本**:构建Qt源码前必须运行configure,并且可能需要指定-platform参数,否则qmake无法确定目标平台,导致找不到spec文件。用户可能跳过了这一步,或者参数有误。 2. **源码目录结构问题**:如果用户手动移动了源码目录,或者目录结构不完整,可能导致mkspecs目录缺失或路径错误。需要检查Qt源码中的mkspecs目录是否存在,并且是否包含对应平台的spec文件。 3. **环境变量问题**:QMAKE环境变量可能未正确设置,指向了错误的qmake版本或路径。或者PATH变量中没有包含正确的Qt工具链路径。 4. **多版本Qt冲突**:系统中安装了多个Qt版本,qmake默认使用了错误的版本,导致spec文件不匹配。 5. **构建工具配置错误**:如果使用CMake或其他构建工具,可能需要显式指定Qt的路径,或者项目文件中有错误的配置。 针对这些可能的原因,我需要逐步引导用户检查每一步。例如,确认是否运行configure并指定正确的平台,检查mkspecs目录是否存在,设置正确的环境变量等。 此外,用户的项目路径中有“桌面”目录,可能在Linux系统中,路径包含空格或中文字符,导致qmake处理路径时出现问题。虽然Qt通常支持中文路径,但某些情况下可能会有问题,建议将项目移到全英文路径下。 最后,建议用户提供更多信息,如操作系统、Qt版本、configure命令的参数等,以便进一步诊断问题。</think>遇到错误 `Could not find qmake spec ''` 且项目文件为 `qt-everywhere-src-5.15.16/qt.pro` 时,通常是由于 **Qt 源码编译环境配置问题** 或 **qmake 无法识别目标平台** 导致。以下是详细分析与解决方案: --- ### **1. 问题背景** - **错误含义**:`qmake` 找不到对应的平台规范(spec),无法为项目生成构建文件。 - **常见场景**:尝试编译 Qt 源码(如 `qt.pro`)时,未正确配置平台参数或 Qt 源码目录结构不完整。 --- ### **2. 核心原因** #### **(1) 未正确运行 `configure` 脚本** - Qt 源码编译前需通过 `configure` 脚本生成平台相关的配置,若未运行或参数错误,`qmake` 将无法识别目标平台。 #### **(2) 源码目录结构损坏** - Qt 源码的 `mkspecs` 目录(包含平台规范文件)缺失或路径错误。 #### **(3) 环境变量冲突** - 系统环境变量中 `QMAKE` 或 `PATH` 指向了错误的 Qt 版本或工具链。 --- ### **3. 逐步解决方案** #### **步骤 1:确认已正确运行 `configure`** - **操作**: 1. 进入 Qt 源码目录: ```bash cd /home/user/桌面/qt-everywhere-src-5.15.16 ``` 2. **清理旧配置**(若存在): ```bash make distclean ``` 3. **运行 `configure` 脚本**并指定平台参数: ```bash ./configure -platform <spec> # 替换为你的目标平台(如 linux-g++、win32-msvc) ``` - 示例(Linux): ```bash ./configure -platform linux-g++ ``` - 示例(Windows,使用 MSVC 编译器): ```bash ./configure -platform win32-msvc ``` 4. 完成配置后,再尝试编译: ```bash make ``` #### **步骤 2:检查 `mkspecs` 目录** - **操作**: 1. 确认 Qt 源码中 `mkspecs` 目录存在: ```bash ls /home/user/桌面/qt-everywhere-src-5.15.16/qtbase/mkspecs ``` 2. 若目录缺失,需重新解压 Qt 源码或从官方仓库克隆完整代码。 #### **步骤 3:手动指定 `qmake` 路径** - **操作**: ```bash # 使用源码目录内的 qmake(编译生成的) /home/user/桌面/qt-everywhere-src-5.15.16/qtbase/bin/qmake -spec <spec> qt.pro ``` - 替换 `<spec>` 为实际平台,如 `linux-g++` 或 `win32-msvc`。 #### **步骤 4:解决环境变量冲突** - **操作**: 1. 检查当前 `qmake` 版本: ```bash which qmake # 查看路径 qmake -v # 查看版本 ``` 2. 若路径指向系统预装 Qt(而非源码编译的版本),需临时禁用其他 Qt 版本: ```bash export PATH="/home/user/桌面/qt-everywhere-src-5.15.16/qtbase/bin:$PATH" ``` #### **步骤 5:修复项目文件路径** - **现象**:项目路径包含空格或中文字符(如“桌面”)可能导致解析问题。 - **操作**: 1. 将 Qt 源码移动到纯英文路径(如 `~/projects/qt-src-5.15.16`)。 2. 重新运行 `configure` 和 `make`。 --- ### **4. 补充说明** - **Qt 源码编译要求**: - 确保已安装编译依赖(如 GCC、Python、libgl-dev 等)。 - 参考官方文档:[Building Qt 5 from Git](https://wiki.qt.io/Building_Qt_5_from_Git) - **错误日志分析**: - 若仍报错,检查 `config.log` 或 `make` 输出的详细日志,定位具体失败步骤。 --- ### **5. 总结流程** 1. 确保已运行 `configure` 并指定正确的 `-platform` 参数。 2. 检查 `mkspecs` 目录完整性。 3. 使用源码内的 `qmake` 并手动指定 `-spec`。 4. 避免路径中的中文字符。 5. 若问题持续,提供以下信息以进一步诊断: - 操作系统及版本 - `configure` 命令参数 - `qmake -v` 输出 - 错误日志片段
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值