-1

I had a 300 KB application linked dynamically, due to plenty of issues I decided to try static linking build.

I configured Qt with the following:

configure -c++11 -mp -static -debug-and-release -platform win32-msvc2012 -opengl desktop -opensource -confirm-license -make libs -nomake tools -nomake tests -nomake examples -no-openssl -skip webkit

On the dynamically linked build I only had to link to the dependencies:

qtmain.lib
Qt5Gui.lib
Qt5Core.lib
Qt5Widgets.lib
Qt5Network.lib
Qt5WinExtras.lib
qwindows.lib

Now I have to use 15 libraries!

qtmain.lib
qtpcre.lib
Qt5Gui.lib
Qt5Core.lib
Qt5Widgets.lib
Qt5Network.lib
Qt5WinExtras.lib
opengl32.lib
imm32.lib
Ws2_32.lib
Qt5PlatformSupport.lib
qtfreetype.lib
winmm.lib
qtharfbuzzng.lib
qwindows.lib

I mean, why do I have to link against qtpcre, or qtfreetype if I'm not using regular expressions nor free type in my solution? Do I really need all of those?

I have tried to enable full optimalization.

In the results, my application size is 10,731KB and thats really frustrating and depressing. Is there any possibility to decrease it? External applications maybe?

(Yes, I am aware it's a static build and it will not be a 7MB or less file, but maybe I could get below 10MB at least?)

RA.
  • 969
  • 13
  • 36
  • 2
    You depend on a set of dynamic libraries. Those in turn depend on other dynamic libraries, which in turn might depend on other libraries etc. If you link statically, you need to link with all libraries in the dependency tree. And yes that might make your executable file really big, but that's the trade-off of static linking. And you memory requirements won't really change actually, all those dynamic libraries still needs to be loaded, linking statically might actually save some memory since it's only one file that needs to be loaded by the OS. – Some programmer dude Oct 21 '16 at 10:47
  • You may not need to use regular expression, but the qt library functions that you use need regular expression, hence they must be linked with. If you want to use static linking, finding all indirect dependencies is now your responsibility. Even with dynamic linking, you're still pulling in all that code, except that the dynamic linker does all that work for you. – Sam Varshavchik Oct 21 '16 at 10:49
  • Also, is that size you show for the debug or release build? Have you tried stripping debug and other meta-data from from the executable? Pass compiler flags to optimize for size rather than speed? – Some programmer dude Oct 21 '16 at 10:51
  • Qt libraries and dependencies are quite big so this is what happens when you link statically against qt. Also note that you can run into licensing issues if you build statically against Qt. – Hayt Oct 21 '16 at 10:51
  • "Is there any possibility to decrease it?" - yes, go back to linking dynamically. It's probably worth trying to resolve the "due to plenty of issues" which caused you to move to static linking. – UKMonkey Oct 21 '16 at 10:56
  • @Someprogrammerdude It's a release build. Yes, I do optimize for size. – RA. Oct 21 '16 at 10:57
  • @UKMonkey Don't waste my time making me reading rude comments like that. http://stackoverflow.com/questions/40141335/could-not-find-or-load-the-qt-platform-plugin-windows here you go, wishing you best of luck. – RA. Oct 21 '16 at 10:58
  • @DonaldDuck Good luck with that mate, if you think I'm rude by suggesting you revert then clearly I shouldn't be posting to help you. – UKMonkey Oct 21 '16 at 11:07
  • @UKMonkey Thank you. – RA. Oct 21 '16 at 11:40

2 Answers2

1

The extra dependencies are likely system libraries a dynamic build would otherwise load dynamically or indirect dependencies. In a static build those cannot be used as plugins and have to be included in the binary.

Keep in mind that just because you explicitly don't use a feature doesn't mean the features you are suing are not depending on others and others. The dependency is a dependency, regardless whether it is direct or indirect.

10 mb is not too shabby, and you are already saving a considerable amount of space relative to deploying dynamic link libraries. The binary will include all the necessary functionality and you can't really do anything about it if you really need those modules. Also, a static build will have slightly better performance (and deployment convenience of course).

I don't recall the default compiler flags, but it is most likely not optimized for size by default, so for MSVC you may get smaller binaries by using /O1 for minimum size. Note that this may result in slight performance degradation.

Also, even though the number of libraries is higher, most of them are fairly small and won't really be adding that much to the binary size.

Lastly you really don't want to be building debug binaries for Qt - they are huge and it takes a lot of wasted time. Debug using the dynamic link version, only build static binaries for release.

dtech
  • 47,916
  • 17
  • 112
  • 190
1

There is a tool that significantly reduces size of executable called upx

Usage:

upx --best path/to/executable

Consider also adding -q option which would not print output). This way you can even add this tool to your build process to avoid additional manual step.

Shf
  • 3,463
  • 2
  • 26
  • 42