膨胀的EXE大小,和不需要的依赖与Qt / MingW [英] Bloated EXE sizes, and unwanted dependencies with Qt/MingW

查看:153
本文介绍了膨胀的EXE大小,和不需要的依赖与Qt / MingW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何缩小在最新的QT SDK(4.8.2)(基于mingw / g ++)下编译的EXE文件的大小。我正在工作的一个香草c ++控制台应用程序,有一个简单的循环,只有#includes iostream,当我注意到exe的它生成的是大约465kb;方式大于他们应该!注释所有的流东西把它降低到预期的5kb范围(虽然剩余的代码将大部分死了)。这看起来似乎不对,特别是因为另一个完整的项目,我正在工作有一个QGLwidget,窗口,十几个数据结构和〜3000个语句,只有在约126Kb的时钟。有没有一些设置或标志,我错过了?这里是.pro,而cpp是微不足道和Qt自由(基本上getline和cout与一半的char交换):

  TEMPLATE = app 
CONFIG + = console
CONFIG - = app_bundle
CONFIG - = qt
SOURCES + = main.cpp
QMAKE_CXXFLAGS_RELEASE + = -O2
QMAKE_CXXFLAGS_RELEASE + = -Os

我已经尝试过其他一些配置,模式(调试是> 3Mb),但我不知道为什么这么so肿。



我也看了PE头,我看到从libgcc_s_dw2-1.dll和mingwm10.dll导入一些函数,如果我可以完全消除这些依赖,那将是很好的,特别是因为任何一个都不需要。我可以通过添加 QMAKE_LFLAGS_RELEASE + = -static 到.pro,但是mingwm10.dll保持不变,可以使libgcc一个离开(以17kb的exe大小为代价)方法,调用单个函数。



基于整体膨胀,以及所有无用的框架的东西,编译器试图偷偷摸摸(至少网络)。我猜这只是一对夫妇的设置问题,特别是与一些默认的编译器标志,如-DQT_LARGEFILE_SUPPORT或-mthreads。下面是编译输出(为强调添加的项目符号):




  • 14:04:00: li>
  • 14:04:00:配置不变,跳过qmake步骤。

  • 14:04:01:开始:C:\QtSDK\mingw \bin \mingw32-make.exe

  • C:/ QtSDK / mingw / bin / mingw32-make -f Makefile.Release

  • mingw32-make [1]:输入目录`C:/ Documents and Settings / Administrator / My Documents / QT / conTest'

  • g ++ -c -O2 -O2 -Os -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -Ic:\QtSDK\Desktop\Qt\4.8.1\mingw\mkspecs\win32-g ++-o release\main.o main.cpp g ++ -Wl,-s -Wl,-subsystem,console -mthreads -o release\conTest.exe release / main.o

  • mingw32-make [1]:离开目录`C :/ Documents and Settings / Administrator / My Documents / QT / conTest'

  • 14:04:10:过程C:\QtSDK\mingw\bin\mingw32-
  • 使用mingw时的一个问题是,使用mingw的问题是: w32版本的binutils不支持死代码剥离(它删除你没有实际使用的库的部分。)为了减少我的可执行文件的大小,我不得不从源代码修补和构建binutils使用补丁:



    http:/ /sourceware.org/bugzilla/show_bug.cgi?id=11539



    它帮助了。但是要使用它,您需要使用以下方法重建所有

     -fdata-sections -ffunction-sections 您自己的应用程序)的编译标志中的p $ p> 

    和:

     -Wl, -  gc-sections 

    这对我来说是值得的,因为以前我的可执行文件的重量约为20MB,现在已经减半了,他们大约10MB。这包括所有库(我静态链接),包括Qt,SDL和各种媒体库(如Vorbis,mpg123,FLAC和其他)。

    虽然我想象,如果你在Windows上构建,这不会很容易做到这一切。我使用Linux编译w32交叉编译版本的一切,这是更容易。


    I'm trying to figure out how to shrink the sizes of EXE files compiled under the newest QT SDK (4.8.2) (mingw/g++ based). I was working on a vanilla c++ console app that has a simple loop and only #includes iostream, when I noticed that the exe's it generated are about 465kb; way bigger than they should be! Commenting out all the stream stuff brings it down to the expected 5kb range (although the remaining code would be mostly dead). This doesn't seem right at all, especially since another, full project I'm working on has a QGLwidget, windowing, a dozen data structures and ~3000 statements and only clocks in at about 126Kb. Is there some setting or flag I'm missing? Here's the .pro, while the cpp is trivial and Qt-free (basically getline and cout with a half dozen char swaps):

    TEMPLATE = app
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG -= qt
    SOURCES += main.cpp
    QMAKE_CXXFLAGS_RELEASE += -O2
    QMAKE_CXXFLAGS_RELEASE += -Os
    

    I've tried a few other configurations, and it's definitely compiling in release mode (debug is >3Mb), but I can't figure out why it's so bloated.

    I've also looked at the PE header, and I see that it's importing some functions from libgcc_s_dw2-1.dll and mingwm10.dll, and it'd be nice if I could eliminate those dependencies altogether as well, especially since neither one should be required anyway. I can make the libgcc one go away(at the expense of 17kb of exe size) by adding QMAKE_LFLAGS_RELEASE += -static to the .pro, but the mingwm10.dll stays either way, calling a single function.

    Based on the overall bloating, and all the useless framework stuff that the compiler is trying to sneak in (networking, at least). I'm guessing it's just a matter of a couple settings that are askew, particularly with some of the default compiler flags like -DQT_LARGEFILE_SUPPORT or -mthreads. Here is the compile output (bullets added for emphasis):

    • 14:04:00: Running steps for project conTest...
    • 14:04:00: Configuration unchanged, skipping qmake step.
    • 14:04:01: Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
    • C:/QtSDK/mingw/bin/mingw32-make -f Makefile.Release
    • mingw32-make[1]: Entering directory `C:/Documents and Settings/Administrator/My Documents/QT/conTest'
    • g++ -c -O2 -O2 -Os -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -I"c:\QtSDK\Desktop\Qt\4.8.1\mingw\mkspecs\win32-g++" -o release\main.o main.cpp g++ -Wl,-s -Wl,-subsystem,console -mthreads -o release\conTest.exe release/main.o
    • mingw32-make[1]: Leaving directory `C:/Documents and Settings/Administrator/My Documents/QT/conTest'
    • 14:04:10: The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited normally.

    解决方案

    One of the problems with using mingw is that the w32 version of binutils doesn't support dead code stripping (it removes the parts of the libraries that you don't actually use.) In order to bring down the sizes of my executables, I had to patch and build binutils from source using the patches here:

    http://sourceware.org/bugzilla/show_bug.cgi?id=11539

    It helped. But for it to work, you will need to rebuild everything using:

    -fdata-sections -ffunction-sections

    in the compile flags of everything (including GCC, Qt, other libraries and your own application) and:

    -Wl,--gc-sections

    in the link flags of your application only. It was worth it for me, as previously my executables would weigh in at about 20MB and now that's been halved and they're about 10MB. This includes all libraries (I link statically), including Qt, SDL, and various media libraries (like Vorbis, mpg123, FLAC and others.)

    Although I imagine that if you build on Windows, it's not gonna be easy to do all that. I used Linux to build w32 cross-compile versions of everything, which is way easier.

    这篇关于膨胀的EXE大小,和不需要的依赖与Qt / MingW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆