3

I'm making a program (let's call it ProgramWP) made with Qt 4.8.5 on a Fedora and based on a QWizard structure with its QWizardPages. The program has more or less 50 classes, 30 of them are QWizardPages.

The thing is that the program executable 'weights' (dunno the english word/expression) 8Mb ( the release version) and I want to know:

  • Why does it weight so much? Which is the cause?
  • How can I reduce it?

I need to reduce it because in the product of the enterprise, there are some applications runing, some of them mine. To sup up the exe's:

  • ProgramMAIN (1.5MiB): the main program of the enterprise.
  • ProgramMAIN2(600KiB): another important program of the enterprise.
  • ProgramWP(8MiB): My main program (made with Qt).
  • ProgramMINI(2.5Mib): My mini version of my main program (made with Qt).
  • Program3(1.3MiB): My other program made with Qt

As you can see my main program weights so so so much than the main one while the main one is soooo much bigger (ProgramWP is just a little program to configure some easy things).

I'm linking statically some of our libs in ProgramWP and ProgramMINI but so does programMAIn and ProgramMAIN2 so... knowing that ProgramMain2 is 600Kibs with the linked libraries, my ProgramWP should not weight more than that.

This is how I do the linking in the .pro file:

unix:!macx: LIBS += -L$PWD/../../ConfigLib/Release/ -lLib1
INCLUDEPATH += $PWD/../../Lib1
DEPENDPATH += $PWD/../../Libs/Release
unix:!macx: PRE_TARGETDEPS += $PWD/../../Libs/Release/Lib1.a

I've searched and asked and found that I could add the QMAKE_CXX_FLAGS+= -s line to the .pro file to delete the unnecesary symbolsand after doing it and runing the qmake, it stills weight the same (It's like if it was ignored). I've see if gcc uses the -s param and rebuilding I get:

g++ -c -pipe -std=c++11 -s -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_SCRIPT_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib/qt4/mkspecs/linux-g++ -I../MyProject-I/usr/include/QtCore -I/usr/include/QtNetwork -I/usr/include/QtGui -I/usr/include/QtXml -I/usr/include/QtScript -I/usr/include -I../../Utils -I../../Lib1-I../../Lib2-I../../Lib3-I../../Lib4-I. -I. -I../MyProject-I. -o wp2.o ../MyProject/wpmine.cpp

so as you can see, seems that gcc uses that param so... Any idea of why is it so heavy and how can I fix it?

Thank you so much.

Note: Their programs are made with eclipse and c++ basically while mine is made with Qt. To run it they have isntalled some Qt libraries in the enterprise's product so another question is... could they run my program without that libraries installed? Just putting there the exe and calling it?

Megasa3
  • 766
  • 10
  • 25
  • 1
    The executable is as large as it is, because Qt is one bloated piece of a library. Want to make it small? Stay clear of Qt. – IInspectable Dec 14 '15 at 11:27
  • Hahaha that's nice :) But my bosses want that program made in Qt, and now they want it to weight less than their programs... I'll add a note to the main post about it because their programs are made in eclipse and c++ – Megasa3 Dec 14 '15 at 11:28
  • The size of the EXE is quite irrelevant. If you want to deploy a Qt application, you'll have to ship the support libraries (DLLs on Windows) alongside your application. For a standard application that does nothing, they weigh in at roughly 30MiB. – IInspectable Dec 14 '15 at 11:31
  • Do you have resources embedded in binary? Have you tried `strip` instead of `-s`? Do you need all the libraries linked? – Ivan Aksamentov - Drop Dec 14 '15 at 11:32
  • 2
    *how can I make my ex weight less* . Closing as off topic because neither relationship nor health related questions are fit for stackoverflow ;) – UmNyobe Dec 14 '15 at 11:32
  • There is also a Qt fork called [CopperSpice](http://www.copperspice.com/) that you might want to check out. – Ivan Aksamentov - Drop Dec 14 '15 at 11:33
  • *But my bosses want that program made in Qt, and now they want it to weight less than their programs.* https://www.youtube.com/watch?v=BKorP55Aqvg – UmNyobe Dec 14 '15 at 11:35
  • @Drop What do you mean with " resources embedded in binary?" I'm kidna new on that and dunno what that is sorry... No, I didnt try with strip, how could I use it? `QMAKE_CXX_FLAGS+= -strip`? And yes, I need all that libraries to be linked – Megasa3 Dec 14 '15 at 11:35
  • https://sourceware.org/binutils/docs-2.24/binutils/strip.html – Ivan Aksamentov - Drop Dec 14 '15 at 11:36
  • Resources like icons, images, strings etc. might be embedded in a binary. See [QtResources](http://doc.qt.io/qt-5/resources.html) – Ivan Aksamentov - Drop Dec 14 '15 at 11:37
  • @Drop oh yes! I dont have any of that on ProgramWP, I just have an image in Program3, and 2 in ProgramMINI. – Megasa3 Dec 14 '15 at 11:39
  • You might try `-Os` "Optimize for size" option instead of `-O2`. Notice, that you have `-O2` twice in your command line – Ivan Aksamentov - Drop Dec 14 '15 at 11:40
  • @Drop I'm really bad with all that unix stuff about the console so I'll check how to use that strip you mentioned and about the double /O2... dunno where to change it but I'll take a look. the strip function u said... is it a gcc option or should I run it apart with the final exe? I don't get it srry and ty for help – Megasa3 Dec 14 '15 at 11:42
  • How much data do you store in data segment of the executable? Long strings in multiple languages? Big arrays? Shaders code? You might want to move them into separate files readable on runtime. – Ivan Aksamentov - Drop Dec 14 '15 at 11:42
  • `strip` is a [separate program](http://stackoverflow.com/questions/1931626/how-to-really-strip-a-binary-in-macos) – Ivan Aksamentov - Drop Dec 14 '15 at 11:43
  • 1
    @Drop wait... WHAAAAAAAT!!! I used the strip like that `strip exename` and it turn from 8Mib to 550KiB O.o wow! I suppose it will still run perfectly but its incredible! Before posting it as an answer and acepting it... I dont have multiple language, yes arraybytes, dunno what shaders code is here... but u mean using xml for example to put some information? – Megasa3 Dec 14 '15 at 11:47
  • @UmNyobe exe not ex ... but nice comment :D Made me laught so much! haha – Megasa3 Dec 14 '15 at 12:01

1 Answers1

5

Here are some tips to reduce executable size:

  • You might want to use strip on your executable (in case something went wrong with compiler's -s flag)

  • Compiling with -Os flag might reduce executable size slightly

  • Reducing size of the data segment of the executable. Note that every constant (including string literals, static arrays initializers, etc) is stored inside executable and increases it's size:

    const char* str = "A very very long string"; // will bloat your executable
    
    BigDataType myData[] = { ... }; // will bloat your executable
    
  • Moving embedded resources into external files (or even to the network). Embedded icons, images, strings, etc.) increase binary size dramatically. See QtResource

  • Reduce usage of the templated code. Massive use of templates (along with their instantiation) is a well known reason of code bloating. This is a trade-off between code size and code beauty.

  • You might want to try CopperSpice, a fork of Qt, if it does any better.

Community
  • 1
  • 1
Ivan Aksamentov - Drop
  • 12,860
  • 3
  • 34
  • 61
  • How can I remove the second -O2 I have in the gcc line? Moving resources to external files you mean like using xml to store data? – Megasa3 Dec 14 '15 at 12:23
  • I don't know where second `-O2` comes from. Check your `.pro` file. Sure, you can store data in XML or whatever format you like, it depends on what kind of data it is and you preferences. The idea is to move data out of executable to somewhere else, thus reducing executable size. – Ivan Aksamentov - Drop Dec 14 '15 at 12:28
  • ok ok, i have nothing in the pro file more than `QT += core gui QT += network QT += script QT += date QT += time QT += xml QMAKE_CXXFLAGS += -std=c++11 QMAKE_CXXFLAGS += -s` so dunno the second O2 that's why I asked. About xml I already use them for large info that goes into some comboboxes and all that. – Megasa3 Dec 14 '15 at 12:31
  • Just before you go away all happy.... have you tried to deploy this binary (run on target)? because you will need the Qt libraries somewhere. I believe you will either need: a. Qt (same version) installed on the target, b. Copy the required Qt dlls alongside your binary on the target, or c. Create a static build (self sufficient) which will be over 30mb even when stripped... – code_fodder Dec 14 '15 at 18:02
  • @code_fodder hi code, thanks for your advice :) but yes, we have installed the qt on the target and also, we have run the program on it and runs ok :) thanks for the advice too. I had a question about if I would need all that stuff on the target but as I can see now, I suppose that yes, and we can't delete them. – Megasa3 Dec 15 '15 at 15:42
  • @Megasa3 No worries, There are many options for deployment, but probably the "smallest" option is to just take the *.dll's that you need (and not install the whole qt). For example, I have 3 qt projects running in the same folder location with just one set of the *.dll's (i.e. sharing the dll's) that I need (around 30mb), and that is about as small as I can make it, so you might want to consider doing this if space is an issue for you : ) – code_fodder Dec 17 '15 at 07:13