您的位置:首页 > 编程语言 > Qt开发

Qt之Qt/Qt Lite 自编译详解(VS/MinGW/...)

2017-07-30 12:28 543 查看
2018/3/24 目前QT更新到了5.10.1。后续文章增加了对该版本的说明。

写在前面

  现在,网上关于Qt编译的文章数不胜数。写这篇文章仅仅是对于自我学习的一个记录。主要是对实际学习中遇到的一些棘手问题做个备忘录。

  编译工具使用 VS2017 和 MinGW5.3.0 为例,来进行说明。

Qt Configure

  Qt 的配置系统可以说相当复杂。看完源码中的各种配置文件,自杀的心都有了。。。

  在Qt的众多配置选项中,有些是固定针对特定平台的;有些是自动检测的;对于以上两种,一般不需要太关心。需要关注的配置选项中,个人感觉需要重点关注的可以分为以下三大类种:

基本配置选项

Qt 组件配置选项(Qt Modules、Qt Features等)

Third-Party Libraries配置参数

  当编译目录和源码目录不一样时,称为影子编译(shadow build)。(Qt Creator默认是使用影子编译来编译工程的)。Qt源码的编译也可以使用影子编译,方法就是新建一个目录,然后cd到该目录中运行configure。影子编译在处理一份源码对应多种编译结果时尤为方便。

mkdir ~/qt-build
cd ~/qt-build
~/qt-source/configure -prefix /opt/Qt5.6       /* 在新建的目录中执行编译,则编译产生的文件就会放在新目录,不会对源码目录有影响 */


可以通过
configure --help
或者
configure -h
查看完整的configure配置参数

具体可参看http://doc.qt.io/qt-5/configure-options.html

基本配置选项

  基本配置选项主要就是
Build options
Build environment
。这两部分给出了我们要如何编译Qt源码。主要的参数项目(部分)如下:

Build options:
-opensource .......... Build the Open-Source Edition of Qt
-commercial .......... Build the Commercial Edition of Qt
-confirm-license ..... Automatically acknowledge the license

-release ............. Build Qt with debugging turned off [yes]
-debug ............... Build Qt with debugging turned on [no]
-debug-and-release ... Build two versions of Qt, with and without
debugging turned on [yes] (Apple and Windows only)
-optimize-debug ...... Enable debug-friendly optimizations in debug builds
[auto] (Not supported with MSVC)
-optimize-size ....... Optimize release builds for size instead of speed [no]
-optimized-tools ..... Build optimized host tools even in debug build [no]
-force-debug-info .... Create symbol files for release builds [no]
-separate-debug-info . Split off debug information to separate files [no]
-strip ............... Strip release binaries of unneeded symbols [yes]
-force-asserts ....... Enable Q_ASSERT even in release builds [no]
-developer-build ..... Compile and link Qt for developing Qt itself
(exports for auto-tests, extra checks, etc.) [no]

-shared .............. Build shared Qt libraries [yes] (no for UIKit)
-static .............. Build static Qt libraries [no] (yes for UIKit)
-framework ........... Build Qt framework bundles [yes] (Apple only)

-platform <target> ... Select host mkspec [detected]
-xplatform <target> .. Select target mkspec when cross-compiling [PLATFORM]
-device <name> ....... Cross-compile for device <name>
-device-option <key=value> ... Add option for the device mkspec

Build environment:

-sysroot <dir> ....... Set <dir> as the target sysroot
-gcc-sysroot ......... With -sysroot, pass --sysroot to the compiler [yes]

-pkg-config .......... Use pkg-config [auto] (Unix only)

-D <string> .......... Pass additional preprocessor define
-I <string> .......... Pass additional include path
-L <string> .......... Pass additional library path
-F <string> .......... Pass additional framework path (Apple only)

-sdk <sdk> ........... Build Qt using Apple provided SDK <sdk>. The argument
should be one of the available SDKs as listed by
'xcodebuild -showsdks'.
Note that the argument applies only to Qt libraries
and applications built using the target mkspec - not
host tools such as qmake, moc, rcc, etc.

-android-sdk path .... Set Android SDK root path [$ANDROID_SDK_ROOT]


Qt 组件配置选项

  该部分主要就是Qt Modules配置参数、Qt Features配置参数 、Qt Parts等等。其中,Qt Features即从Qt忘了几开始,官方出的Qt Lite配置系统 针对的部分。

Qt Modules

  关于Qt各模块的详细说明,感兴趣的可以去http://doc.qt.io/qt-5/qtmodules.html查看。针对这一部分,Qt Configure中通常可以使用以下命令进行裁剪:
configure -skip moudule


  查看Qt源码不难发现,每个模块就对应了Qt源码根目录下的一个文件夹,所为skip某个模块,就是跳过对应的某个目录。例如:Qt的qtconnectivity模块及对应了Qt源码目录的qtconnectivity文件夹。查看该文件夹中可发现,其中有 bluetooth和nfc。这也就表明了,如果不编译该模块,这就没有蓝牙和NFC。

注意:

1. 好多模块是有相互依赖关系的,去掉模块时,需要自行处理该问题。

2. 去掉对应模块后,其Qt Features中,对应的特性也将被去掉。且不能在命令中显示配置了。例如去掉了Qt3d模块,则在Features中,的no-features 不能使用。

Qt Features

  上面说了,在Qt源码根目录下,每个文件夹都是Qt的一个模块,其中就有个
qtbase
的文件夹,看名字就知道这是Qt的基本组成部分,该目录下,也有一些可以精简的Qt 模块。其实之前也是可以精简该部分中的某些子项目的,只是通过几个参数而已,现在,官方给出了新的配置系统,即为:Qt Lite Project

  关于这部分,主要就是 Qt Lite 配置系统 。开始,我还以为Qt Lite是一个精简的Qt版本 或者说一个可以精简Qt的工具。最早官方也没有专门的文档对这部分进行说明,后续才出了些文档。看了好多文档后,终于有了个大体的了解。针对这一部分,Qt Configure中通常可以使用以下命令进行裁剪:

configure -feature-<feature> 或 -no-feature-<feature>


  在最新的Qt5.9.1源码中,
qtbase/src/corelib/global/qfeatures.txt
一不存在了,至少我没找到。但是现在,可以通过以下命令,直接查看列表:
configure --list-features
。如下图为部分列表



  在Qt5.10.0源码中,执行命令
configure --list-features
会报错!


关于Qt Lite,有位网友给出了很详细的说明,传送门在此。

各种Features存在依赖关系,过度去除会导致编译错误,提示找不到依赖模块

目前有多达190项Features

Qt Parts

  在Qt的配置选项中,有几个参数是针对于Qt Parts List的。但是,具体这个Parts列表中都有啥我也没有搞清楚。

Third-Party Libraries

  Qt源码中包含了一些第三方库,如果想使用Qt自带的第三方库,可用通过-qt配置;不需要使用对应模块时,使用-no+模块名;如果想使用系统中的第三方库,可用通过-system配置。下表中列出一些第三方库及其配置选项:

Library NameBundled in QtInstalled in System
zlib-qt-zlib-system-zlib
libjpeg-qt-libjpeg-system-libjpeg
libpng-qt-libpng-system-libpng
xcb-qt-xcb-system-xcb
xkbcommon-qt-xkbcommon-system-xkbcommon
freetype-qt-freetype-system-freetype
PCRE-qt-pcre-system-pcre
HarfBuzz-NG-qt-harfbuzz-system-harfbuzz
  针对这部分,可以使用一下命令:
./configure -no-zlib -qt-libjpeg -qt-libpng -system-xcb


Qt5.9.1静态编译选项

  以下命令参数进行了大量裁剪,可根据自己的需要修改。例如以下裁剪后,导致编译出来的程序不能正确处理Qss中,
color:white;
这样的属性,必须使用
color:grb(255,255,255)
这样。因为裁剪掉了
colornames
这个特性。

MSVC2017编译:

configure.bat -static -release -platform win32-msvc -no-opengl -prefix "F:\Qt5.9.1_MSVC2017_x86_Static" -opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests -skip qt3d -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtcharts -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip qtquickcontrols2 -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -no-feature-texthtmlparser -no-feature-textodfwriter -no-feature-concurrent -no-feature-effects -no-feature-sharedmemory -no-feature-systemsemaphore -no-feature-im -no-feature-dom -no-feature-filesystemwatcher -no-feature-graphicsview -no-feature-graphicseffect -no-feature-sizegrip -no-feature-printpreviewwidget -no-feature-keysequenceedit -no-feature-colordialog -no-feature-fontdialog -no-feature-printpreviewdialog -no-feature-progressdialog -no-feature-inputdialog -no-feature-errormessage -no-feature-wizard -no-feature-datawidgetmapper -no-feature-colornames -no-feature-cups -no-feature-paint_debug -no-feature-codecs -no-feature-big_codecs -no-feature-iconv -no-feature-ftp -no-feature-networkproxy -no-feature-socks5 -no-feature-networkdiskcache -no-feature-bearermanagement -no-feature-completer -no-feature-fscompleter -no-feature-mimetype -no-feature-undocommand -no-feature-undostack -no-feature-undogroup -no-feature-undoview -no-feature-statemachine -no-feature-gestures -no-feature-dbus


采用MinGW编译:

configure.bat -static -release -platform win32-g++ -no-opengl -prefix "F:\Qt5.9.1_MinGW5.3.0_x86_Static" -opensource -confirm-license -make libs -nomake tools -nomake examples -nomake tests -skip qt3d -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdeclarative -skip qtdoc -skip qtgamepad -skip qtcharts -skip qtgraphicaleffects -skip qtimageformats -skip qtlocation -skip qtmacextras -skip qtmultimedia -skip qtnetworkauth -skip qtpurchasing -skip qtquickcontrols -skip qtquickcontrols2 -skip qtscript -skip qtscxml -skip qtsensors -skip qtserialbus -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtvirtualkeyboard -skip qtwayland -skip qtwebchannel -skip qtwebengine -skip qtwebsockets -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -no-feature-texthtmlparser -no-feature-textodfwriter -no-feature-concurrent -no-feature-effects -no-feature-sharedmemory -no-feature-systemsemaphore -no-feature-im -no-feature-dom -no-feature-filesystemwatcher -no-feature-graphicsview -no-feature-graphicseffect -no-feature-sizegrip -no-feature-printpreviewwidget -no-feature-keysequenceedit -no-feature-colordialog -no-feature-fontdialog -no-feature-printpreviewdialog -no-feature-progressdialog -no-feature-inputdialog -no-feature-errormessage -no-feature-wizard -no-feature-datawidgetmapper -no-feature-colornames -no-feature-cups -no-feature-paint_debug -no-feature-codecs -no-feature-big_codecs -no-feature-iconv -no-feature-ftp -no-feature-networkproxy -no-feature-socks5 -no-feature-networkdiskcache -no-feature-bearermanagement -no-feature-completer -no-feature-fscompleter -no-feature-mimetype -no-feature-undocommand -no-feature-undostack -no-feature-undogroup -no-feature-undoview -no-feature-statemachine -no-feature-gestures -no-feature-dbus


Qt5.10.1静态编译选项

  目前发现,上面的5.9.1的编译选项,在5.10.1中编译会报错!例如:

-no-feature-ftp
会导致
\qtbase\src\network\access\qnetworkaccessfilebackend.cpp(243): error C2027: use of undefined type 'QDateTime'和\qtbase\include\qtcore\../../src/corelib/io/qfileinfo.h(53): note: see declaration of 'QDateTime'


-no-feature-completer
会导致
\qtbase\src\widgets\util\qcompleter.h(50): error C2338: Required feature completer for file e:\code\qt_src\src5.10.1\qtbase\src\widgets\util\qcompleter.h not available.


  裁剪需要慎重!

Qt5兼容XP方法

  主要针对在使用VS编译时。默认情况下,最新的VS编译器没有开启对于xp的支持。

  在编译Qt源码时,修改
qtbase\mkspecs\common\msvc-desktop.conf
中的如下部分:

  
QMAKE_LFLAGS_WINDOWS    = /SUBSYSTEM:WINDOWS,5.01
,即可使编译出来的Qt支持xp。

  如果没有以上编译源码,也可以在写程序时,在pro文件中加一句:
QMAKE_LFLAGS_WINDOWS = /SUBSYSTEM:WINDOWS,5.01


Qt5静态编译后使用错误

  正常编译完成,添加到Qt Creator后,可能会提示如下错误:
Qt version is not properly installed,please run make install


  原因是
qmake.exe
是在Qt编译安装时生成的,里面内嵌了Qt相关的一些路径。移动路径后与原来不同,则Qt库就不能正常使用。也就是说,如果不更改编译时指定的的安装路径是没问题。

  解决方法很简单,可以直接patch一下
qmake.exe
。还有个更简单的方法:在
qmake.exe
同一文件夹下,创建一个
qt.conf
文件。
qt.conf
的内容如下:

[paths]
Prefix = ..


Qt5 VS编译乱码

  使用MSVC编译Qt程序或者用Qt Creator打开VS的源码文件时,中文显示为乱码。这主要是由于文件编码导致的。

关于这部分可以看:

http://zcshou.blog.163.com/blog/static/23843807120176273845167/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: