源代码形式编译 Qt 的操作步骤指南Instructions for building QT from source code

源代码形式编译 Qt 的操作步骤指南Instructions for building QT from source code

                                            By Dr. Meng DING,  2016.4.26
                                               For my OOP class students
I do hope you all can learn the general methodology and understand some
important concepts from this instruction, which means that after reading
this, you should have the ability of building any other libraries by
yourself, or at least know what you should do after downloading them.
If you have any question about installing Qt, please feel free to
contact with me and I would like to show you how to build it step-by-
step, but please do it in a group way.

1 准备工作
1.1 Qt 源代码的下载

Qt 官方网站下载某个开源版本(Community/Opensource)的源代码(本操作步骤说明以 Qt-4.8.6 为例)

官网下载地址:https://www.qt.io/download-open-source/。官网会默认下载最新版本的 Qt。如果想下载老版本 Qt,在该界面上寻找”View All

Downloads”,点击后,找”Archive for old versions,然后到想要下载的版本。

对于 4.8.6,下载链接为:http://download.qt.io/archive/qt/4.8/4.8.6/qt-everywhere-opensource-src-4.8.6.zip 。建议使用迅雷等下载工具下载。

下载完成后,解压该压缩包至硬盘的某个位置,并对目录进行改名。本指南将以C:\Qt\4.8.6为例 。上述步骤完成后,你会在C:\Qt\4.8.6下面看到整个解压的 Qt 文件目录结构。

1.2 本地环境准备

在编译开始前,首先要明确,Qt 是一个用 C++语言写的类库,是一堆.cpp 文件。你需要像对待你写的 C++程序一样,用某一个编译器编译并链接这些 cpp 文件。不同的是,你的程序一般会编译链接成为一个二进制可执行程序(exe 文件),但 Qt会被编译成为若干动态链接库 dll 文件(你可以看成是没有 main 的可执行函数代码)和 lib

因此,你的机器上必须装有一种至少支持 C++98 标准的 C++编译器,vs2010及以上,mingw,gcc 等可以。本指南虽以 vs2010 为例,但对于 vs 家族 2010 以上各版本均适用。

1

1.3 第三方依赖库

如果想要编译 Qt-5.0 以上版本,则需要阅读安装包内的 readme 文件,里面会有专门针对如何在 windows 下编译 Qt 的步骤。请注意,编译 5.0 以上版本需要你的本地环境上有 perl,python,ruby。无论你通过何种方式安装以上依赖,都需要将各自目录下的 bin 目录添加到系统环境变量 path 中。

如果你想添加其他的第三方依赖,比如 MySQL,则需要去 MySQL 官方网站下载对应的 C 语言依赖包。该包内会包括一些 lib 文件,dll 文件和 include 文件,你同样需要将 bin 目录添加到系统环境变量 path 中。

2 编译Qt
2.1 配置及产生编译Qt库的makefile

假设你装有 vs2010,则你需要打开一个 vs2010 的命令行窗口。具体方法是,点击开始,选择 visual studio 2010->开发者工具,然后选择一个版本的命令行窗口,本指南以本地 64 位编译环境为例。64 位版本和 32 位版本的 dll lib 文件不可通用,意味着你,在一台 32 位的操作系统下,你只能运行 32 位的程序,所以如果你的操作系统版本为 32 位,请换成 64 位版本,否则你就要编译 32 位版本的Qt 库。

打开本地 64 位命令行窗口(请选择 32 位版本,如果你需要 32 位的 Qt 库),进入你的Qt所在目录,即C:\Qt\4.8.6\。然后运行configure.exe ,它会自动根据本机环境和你指定的配置要求自动产生 makefile 文件。

makefile 文件实际就是一种工程文件,它和 vs 的工程文件*.vcprojts 文件起同样的效果,就是告诉编译器,有多少 cpp 文件需要编译,编译参数是什么(include 路径是什么),如何链接(链接参数是什么,lib 依赖路径是什么,有哪些依赖库文件)。

你可以通过执行 configure --help 来查看更加细节的用法和配置参数。这里推荐使用如下参数:

2

    configure -platform win32-msvc2010, -fast, -no-webkit, -
no-script, -no-multimedia

按下回车后,会问让你选择授权模式,我们选择 opensource,然后程序会开始自动产生配置文件,这个过程大概会需要几分钟至十几分钟。正常情况下,配置结束时会告诉你配置成功完成。

2.2 编译

完成配置后,键入 nmake 开始编译。nmake vs make,它等价于 GNU makemake 其实就是一个能够执行某种类脚本文件的程序,它会读取 Makefile 文件,然后按照给定的规则一条条执行编译命令,其本质和你在 vs 中点击“build”按钮或是“run”是一样的。

还有一些奇奇怪怪的诸如 qmake,cmake 一类的东西,这些和 make 有本质区别,cmake 是用来产生 makefile 文件用的,qmake 是用来产生 qt 工程文件(.pro)以及进一步产生编译器相关的 makefile 文件用的。

一般来说编译 Qt 大概需要 2-3 个小时,所以请确保给你的笔记本连接好电源。一般来说你不编译 script webkit 模块,而且选用的是 vs2010 或以上版本的编译器,不太会出现编译错误。 遇到错误时,请不要慌张和害怕,仔细查看命令行的输出,找到具体出现错误的 cpp 文件,然后 google baidu 之,网上一定会有明确的解答。

如果你选择编译 script 模块,webkit 模块,则整个编译过程会需要大概至少5-6 个小时,而且其中可能会遇到错误。错误原因很有可能是某个文件里由于编码原因引起的字符串问题,就是双引号的不正确解析,很好改正。

3 配置Qt环境

3

3.1 环境变量

Qt 编译成功后,编译得到的 lib 文件和 dll 文件会分别放在 lib/目录和bin/目录下。你需要将 bin 目录(本例下该目录路径为 C:\Qt\4.8.6\bin)添加到系统环境变量 path 中去。

3.2 IDE

3.2.1 Visual Studio

为了在 Visual Studio 下方便的使用 Qt,可以下载 qt-vs-addin 插件。对于 4.8.6 及以下版本,建议使用 qt-vs-addin-1.1.11,下载地址为:

http://download.qt.io/archive/vsaddin/qt-vs-addin-1.1.11-
opensource.exe

下载完成后,直接双击安装。安装成功后会在 vs 的菜单中新增一个名为”Qt”菜单,并且在 vs 的各种向导中,如建立工程向导、添加类向导中会增加对于 Qt 的支持。

3.2.2 Qt Creator

Qt 自己的 IDE,比较轻量级,也比较好用。这个 IDE 可以直接打开 qt 工程文件,即.pro 文件。

4 编写Qt应用程序
有关如何使用 Qt 编写 GUI 程序,请移步 qt 的帮助文档,assistance.exe,

仔细阅读其中的tutorial。 这里只做一个最基本的说明。4.1 IDE 方式

对于使用 vs2010 IDE,和建立一般工程一样,选择建立新的工程,但是在向导中选择 Qt4 工程,然后同样需要指定工程名以及路径,然后需要选择你的应用程序的基类,大型程序可以选用 QMainWindow,但是我们一般的窗口程序,选择 QWidget即可。

4

然后一路下一步,就可产生整个工程。这时你可以直接点击编译运行,你就可以看见你的第一个 Qt 窗口程序。

4.2 命令行形式

该方式需要你已经写好了应用程序的最基本代码。一般来说就是 main.cpp,然后是你的主窗口类程序 xxx.cpp, xxx.h

打开 vs2010 64 位命令行窗口,进入到你的工程文件所在目录,然后依次键入:

qmake -project
qmake
nmake
没有错误的情况下,就会在该目录下的 Debug 目录里面得到你的应用程序。其中在第二步,你也可以通过 qmake -tp vc 来产生 vs 的工程文件,然后即

//这一步产生 Qt 的工程文件(.pro)//这一步产生编译器相关的工程文件(makefile)//这一步以命令行的形式编译和链接程序.

可双击工程文件,回到你熟悉的 IDE 形式进行编程。对于 mac 环境,你可以通过qmake -spec macx-xcode 来产生 xcode 格式的工程文件。

关于 qmake 的具体用法,也可通过在 assistant 中查询 qmake 来仔细阅读。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Building on: linux-g++ (x86_64, CPU features: mmx sse sse2) Building for: linux-aarch64-gnu-g++ (arm64, CPU features: neon) Target compiler: gcc 6.3.1 Configuration: cross_compile use_gold_linker compile_examples enable_new_dtags largefile neon precompile_header shared rpath release c++11 c++14 concurrent dbus reduce_exports stl Build options: Mode ................................... release Optimize release build for size ........ no Building shared libraries .............. yes Using C standard ....................... C11 Using C++ standard ..................... C++14 Using ccache ........................... no Using gold linker ...................... yes Using new DTAGS ........................ yes Using precompiled headers .............. yes Using LTCG ............................. no Target compiler supports: NEON ................................. yes Build parts ............................ libs Qt modules and options: Qt Concurrent .......................... yes Qt D-Bus ............................... yes Qt D-Bus directly linked to libdbus .... no Qt Gui ................................. yes Qt Network ............................. yes Qt Sql ................................. yes Qt Testlib ............................. yes Qt Widgets ............................. yes Qt Xml ................................. yes Support enabled for: Using pkg-config ....................... yes udev ................................... no Using system zlib ...................... yes Qt Core: DoubleConversion ....................... yes Using system DoubleConversion ........ no GLib ................................... no iconv .................................. yes ICU .................................... no Tracing backend ........................ Logging backends: journald ............................. no syslog ............................... no slog2 ................................ no Using system PCRE2 ..................... no Qt Network: getifaddrs() ........................... yes IPv6 ifname ............................ yes libproxy ............................... no Linux AF_NETLINK ....................... yes OpenSSL ................................ yes Qt directly linked to OpenSSL ........ no OpenSSL 1.1 ............................ no DTLS ................................... yes SCTP ................................... no Use system proxies ..................... yes Qt Gui: Accessibility .......................... yes FreeType ............................... yes Using system FreeType ................ no HarfBuzz ............................... yes Using system HarfBuzz ................ no Fontconfig ............................. no Image formats: GIF .................................. yes ICO .................................. yes JPEG ................................. yes Using system libjpeg ............... yes PNG .................................. yes Using system libpng ................ no EGL .................................... no OpenVG ................................. no OpenGL: Desktop OpenGL ....................... no OpenGL ES 2.0 ........................ no OpenGL ES 3.0 ........................ no OpenGL ES 3.1 ........................ no OpenGL ES 3.2 ........................ no Vulkan ................................. no Session Management ..................... yes Features used by QPA backends: evdev .................................. yes libinput ............................... no INTEGRITY HID .......................... no mtdev .................................. no tslib .................................. no xkbcommon .............................. no X11 specific: XLib ................................. no EGL on X11 ........................... no QPA backends: DirectFB ............................... no EGLFS .................................. no LinuxFB ................................ yes VNC .................................... yes Mir client ............................. no Qt Sql: SQL item models ........................ yes Qt Widgets: GTK+ ................................... no Styles ................................. Fusion Windows Qt PrintSupport: CUPS ................................... no Qt Sql Drivers: DB2 (IBM) .............................. no InterBase .............................. no MySql .................................. no OCI (Oracle) ........................... no ODBC ................................... no PostgreSQL ............................. no SQLite2 ................................ no SQLite ................................. yes Using system provided SQLite ......... no TDS (Sybase) ........................... no Qt Testlib: Tester for item models ................. yes Qt SerialBus: Socket CAN ............................. yes Socket CAN FD .......................... yes Qt QML: QML network support .................... yes QML debugging and profiling support .... yes QML sequence object .................... yes QML list model ......................... yes QML XML http request ................... yes QML Locale ............................. yes QML delegate model ..................... yes Qt Quick: Direct3D 12 ............................ no AnimatedImage item ..................... yes Canvas item ............................ yes Support for Qt Quick Designer .......... yes Flipable item .......................... yes GridView item .......................... yes ListView item .......................... yes TableView item ......................... yes Path support ........................... yes PathView item .......................... yes Positioner items ....................... yes Repeater item .......................... yes ShaderEffect item ...................... yes Sprite item ............................ yes Qt Scxml: ECMAScript data model for QtScxml ...... yes Qt Gamepad: SDL2 ................................... no Qt 3D: Assimp ................................. yes System Assimp .......................... no Output Qt3D Job traces ................. no Output Qt3D GL traces .................. no Use SSE2 instructions .................. no Use AVX2 instructions .................. no Aspects: Render aspect ........................ yes Input aspect ......................... yes Logic aspect ......................... yes Animation aspect ..................... yes Extras aspect ........................ yes Qt 3D Renderers: OpenGL Renderer ........................ yes Qt 3D GeometryLoaders: Autodesk FBX ........................... no Qt Wayland Client ........................ no Qt Wayland Compositor .................... no Qt Bluetooth: BlueZ .................................. no BlueZ Low Energy ....................... no Linux Crypto API ....................... no WinRT Bluetooth API (desktop & UWP) .... no Qt Sensors: sensorfw ............................... no Qt Quick Controls 2: Styles ................................. Default Fusion Imagine Material Universal Qt Quick Templates 2: Hover support .......................... yes Multi-touch support .................... yes Qt Positioning: Gypsy GPS Daemon ....................... no WinRT Geolocation API .................. no Qt Location: Qt.labs.location experimental QML plugin . yes Geoservice plugins: OpenStreetMap ........................ yes HERE ................................. yes Esri ................................. yes Mapbox ............................... yes MapboxGL ............................. no Itemsoverlay ......................... yes QtXmlPatterns: XML schema support ..................... yes Qt Multimedia: ALSA ................................... no GStreamer 1.0 .......................... no GStreamer 0.10 ......................... no Video for Linux ........................ yes OpenAL ................................. no PulseAudio ............................. no Resource Policy (libresourceqt5) ....... no Windows Audio Services ................. no DirectShow ............................. no Windows Media Foundation ............... no Qt Tools: QDoc ................................... no Qt WebEngine: Embedded build ......................... yes Pepper Plugins ......................... no Printing and PDF ....................... no Proprietary Codecs ..................... no Spellchecker ........................... yes Native Spellchecker .................... no WebRTC ................................. no Use System Ninja ....................... no Geolocation ............................ yes WebChannel support ..................... yes Use v8 snapshot ........................ yes Kerberos Authentication ................ no Building v8 snapshot supported ......... yes Use ALSA ............................... no Use PulseAudio ......................... no Optional system libraries used: re2 .................................. no icu .................................. no libwebp, libwebpmux and libwebpdemux . no opus ................................. no ffmpeg ............................... no libvpx ............................... no snappy ............................... no glib ................................. no zlib ................................. yes minizip .............................. no libevent ............................. no jsoncpp .............................. no protobuf ............................. no libxml2 and libxslt .................. no lcms2 ................................ no png .................................. no JPEG ................................. no harfbuzz ............................. no freetype ............................. no x11 .................................. no Required system libraries: fontconfig ........................... no dbus ................................. no nss .................................. no khr .................................. no glibc ................................ yes Required system libraries for qpa-xcb: libdrm ............................... no xcomposite ........................... no xcursor .............................. no xi ................................... no xrandr ............................... no xtst ................................. no Note: Also available for Linux: linux-clang linux-icc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值