[Qt5] 减少dll依赖和大小(特别是webkit的大小和依赖)

Qt5的依赖太多, 而且很dll非常大. 折腾了好久, 摸索了一些精简的方法. 

webkit是个非常蛋疼的东西, 依赖超多, 又很庞大. 所以需不需要webkit是完全不同的. 

如何编译Qt5可以参考本人之前一篇文章 http://www.cnblogs.com/lingdhox/p/3457419.html

一. 

1. 在configure时去掉不需要的模块, 比如openssl, icu等. 

configure -opensource -developer-build -mp -platform win32-msvc2010 -nomake examples -nomake tests -no-icu  -no-openssl -no-iconv -no-qml-debug -no-plugin-manifests

更多参数可以configure -help 查看

-no-plugin-manifests 去掉插件dll的manifests文件

需要注意:  如果要使用webkit, 那么icu是必须的.

2. 在configure时, 指定qconfig文件 

   该方法在嵌入式中使用较多, Qt支持feature裁剪. 

 在qtbasesrccorelibglobal下有一些qconfig-xxx.h的文件, 就是指定了不同的feature. 

   Qt tools下有个工程qttoolssrcqtconfig , 编译这个项目, 运行后, 是一个图形化的裁剪feature工具. 它打开qtbasesrccorelibglobal下的qfeatures.txt,  然后你可以自由选择需要的feature, 保存成qconfig-myconfig.h

   然后在运行configure时. 添加 -qconfig myconfig

   我试过这个方法, 但结果是有些地方引用头文件出错. 而且没有用到feature很少, 就不深入研究了.

3. 去掉webkit的quick qml printsupport sensors Qt5V8依赖, 并裁剪webkit本身的大小

打开qtwebkitToolsqmakemkspecsfeatures下的configure.prf  、features.prf和features.pri

features.prf中注释这四行(加#)

    #haveQtModule(quick): WEBKIT_CONFIG += have_qtquick
    #else: CONFIGURE_WARNINGS += "QtQuick module not found, QML APIs will not be built"

    #haveQtModule(printsupport): WEBKIT_CONFIG += have_qtprintsupport
    #haveQtModule(sensors): WEBKIT_CONFIG += orientation_events device_orientation

  

这样编译出来的webkit.dll不依赖quick qml printsupport sensors

features.prf中这几行config

config_libxml2: WEBKIT_CONFIG += use_libxml2
config_libxslt: WEBKIT_CONFIG += xslt
config_libzlib: WEBKIT_CONFIG += use_zlib
config_libwebp: WEBKIT_CONFIG += use_webp

的定义在qtCompileTest中

qtbasemkspecsfeaturesconfigure.prf下的解释

# Try to build the test project in $$QMAKE_CONFIG_TESTS_DIR/$$1
# ($$_PRO_FILE_PWD_/config.tests/$$1 by default).
#
# If the test passes, config_$$1 will be added to CONFIG.
# The result is automatically cached. Use of cached results
# can be suppressed by passing CONFIG+=recheck to qmake.
#
# Returns: true iff the test passes
defineTest(qtCompileTest) {
positive = config_$$1
done = done_config_$$1

done_config_$$1会输出到.qmake.cache

比如qtimageformatsqtimageformats.pro 中添加libwebp

requires(qtHaveModule(gui))

load(configure)
qtCompileTest(jasper)
qtCompileTest(libmng)
qtCompileTest(libtiff)
qtCompileTest(libwebp)

load(qt_parts)

configure.prf 中把

    WEBKIT_CONFIG += 
        build_webkit1 
        build_webkit2 
        build_tests 
        $$WEBKIT_TOOLS_CONFIG

  改成

    WEBKIT_CONFIG += 
        build_webkit1 
        build_tests 
        $$WEBKIT_TOOLS_CONFIG

  

不编译webkit2 , webkit2依赖Qt5V8,  提升HTML5 , js, qml 的支持. 并提升性能. 

需不需要就看情况了

features.pri 中一堆宏, 用于控制特性. 需要自己检查那些特性不需要. 

注意: 这三个文件的修改要在编译前. 

qtwebkitmakefile是在.makefile中编译module-qtwebkit时动态生成的(不存在才会生成)

所以如果修改三个文件后, 想重新编译, 需要删除webkit下新生成的所有makefile

makefile

sourceMakefile.api
sourceMakefile.api.Debug
sourceMakefile.api.Release
sourceMakefile.widgetsapi
sourceMakefile.widgetsapi.Debug
sourceMakefile.widgetsapi.Release
sourceMakefile.QtWebKit

sourceJavaScriptCoreMakefile.jsc
sourceJavaScriptCoreMakefile.jsc.Debug
sourceJavaScriptCoreMakefile.jsc.Release
sourceJavaScriptCoreMakefile.LLIntOffsetsExtractor
sourceJavaScriptCoreMakefile.LLIntOffsetsExtractor.Debug
sourceJavaScriptCoreMakefile.LLIntOffsetsExtractor.Release
sourceJavaScriptCoreMakefile.JavaScriptCore.DerivedSources
sourceJavaScriptCoreMakefile.JavaScriptCore
Makefile.JavaScriptCore.Target
Makefile.JavaScriptCore.Target.Debug
Makefile.JavaScriptCore.Target.Release

SourceWebCoreMakefile.WebCore.DerivedSources
SourceWebCoreMakefile.WebCore.Target
SourceWebCoreMakefile.WebCore.Target.Debug
SourceWebCoreMakefile.WebCore.Target.Release
SourceWebCoreMakefile.WebCore
SourceWebKitMakefile.WebKit1
SourceWebKitMakefile.WebKit1.Debug
SourceWebKitMakefile.WebKit1.Release
SourceWebKit2Makefile.WebKit2.DerivedSources
SourceWebKit2Makefile.WebProcess
SourceWebKit2Makefile.WebProcess.Debug
SourceWebKit2Makefile.WebProcess.Release
SourceWebKit2Makefile.WebKit2.Target
SourceWebKit2Makefile.WebKit2.Target.Debug
SourceWebKit2Makefile.WebKit2.Target.Release
SourceWebKit2Makefile.WebKit2
sourcewtfMakefile.WTF
sourcewtfMakefile.WTF.Debug
sourcewtfMakefile.WTF.Release
SourceThirdPartyANGLEMakefile.ANGLE.Target
SourceThirdPartyANGLEMakefile.ANGLE.Target.Debug
SourceThirdPartyANGLEMakefile.ANGLE.Target.Release
SourceThirdPartyANGLEMakefile.ANGLE
SourceThirdPartyANGLEMakefile.ANGLE.DerivedSources
SourceWebKitqtdeclarativeMakefile.declarative.public
SourceWebKitqtdeclarativeMakefile.declarative.public.Debug
SourceWebKitqtdeclarativeMakefile.declarative.public.Release
SourceWebKitqtdeclarativeMakefile.declarative
SourceWebKitqtdeclarativeexperimentalMakefile.declarative.experimental
SourceWebKitqtdeclarativeexperimentalMakefile.declarative.experimental.Debug
SourceWebKitqtdeclarativeexperimentalMakefile.declarative.experimental.Release

  

需要删除这些, 所以我觉得从压缩包里重新拷贝一份qtwebkit的源码出来编译, 更方便.  环境也更干净

运行 nmake module-qtwebkit 然后会生成一个.qmake.cache

打开可以看到WEBKIT_CONFIG (即刚才修改的选项)

4. 裁剪icu的大小, icudt*.dll, icuin*.dll, icuuc*.dll

 http://userguide.icu-project.org/icufaq

How can I reduce the size of the ICU data library?

1). 根据官方faq的方法, 定制自己需要的数据

http://apps.icu-project.org/datacustom/

先在get data library下选择版本, 比如 ICU 51 Data 

然后选择自己的需要的语言版本. 这样最后的icudt51l.dat 大概在几M左右. 

2). 下载对应版本的源码包. 

替换icusourcedatain 下的icudt*l.dat

打开icusourceallinone的VS工程

编译.  最终生成的文件icu 下 

5. 最终生成的dll:

Qt5WebKit.dll 10M

icudt51.dll  3M

icuin51.dll   1.3M

icuuc51.dll  1M

原文地址:https://www.cnblogs.com/lingdhox/p/3685360.html