How can I make my program 'weight' less
-
@kshegunov
Thank you so much for the explanation. I know now all the options and when to use every one. It's a really interesting post :). About the last line that you said I shouldn't use... I don't really know what does the 4 lines I used to include the libs (I just searched on internet and tried until it worked) but I suppose that its something like:- unix:!macx: LIBS += -L$PWD/../../ConfigLib/Release/ -lLib1: this one defines the library
- INCLUDEPATH += $PWD/../../Lib1: this is the path
- DEPENDPATH += $PWD/../../Libs/Release: this should be the path of another lib needed because of dependency
- unix:!macx: PRE_TARGETDEPS += $PWD/../../Libs/Release/Lib1.a: And this one... I don't really know unless it needs the exact path and name+extension of the lib.
That aside, this will not help me with my problem sadly. I've asked my mate and told me that all the applications in our product use static linkage with the libraries (because ofc they don't want to add those libs on the product) so... I'll have to keep the static linkage.
Until now, the stadistics of the applications are:- ProgramMAIN (1.5MiB): the main program.
- ProgramMAIN2(600KiB): another important program.
- ProgramWP(8MiB): My main program (made with Qt).
- ProgramMINI(2.5Mib): My mini program (made with Qt).
- Program3(1.3MiB): My other program made with Qt (and the only one that just includes 1 lib)
All that programs (except the last one as I've already pointed) have the libraries linked statically but only mines are the ones that weight sooo much. I've see how much weights all the libs I link (the *.a file) and they are only 350KiB. So.. ofc, my bosses expect that my ProgramWP weights less than ProgramMAIN2 but nevertheless its 10 times bigger... :S
-
You could try to use the strip tool to remove all not needed symbols from your binary:
strip YOUR_BINARY -
@roseicollis
I'll start top to bottom:unix:!macx: LIBS += -L$PWD/../../ConfigLib/Release/ -lLib1
means to tell qmake to generate a makefile where your libraries are located in$PWD/../../ConfigLib/Release/
and you want to linkLib1
(filenamelibLib1.so
).unix:!macx:
tells qmake to do this for all *nix systems except macx.INCLUDEPATH += $PWD/../../Lib1
tells qmake to add a directory for includes that is$PWD/../../Lib1
DEPENDPATH += $PWD/../../Libs/Release
tells it add a directory for resolving dependenciesunix:!macx: PRE_TARGETDEPS += $PWD/../../Libs/Release/Lib1.a
This tells qmake that you actually want an archive file (.a) linked, not a shared object (dynamic library). Again, this is done for *nix systems that are not macx.
Here is a full reference of qmake's variables.
On the second part there are two main considerations:
- Number of linked libraries (total amount of code to be included) and their size - all that you link statically will be copied in your executable, so this is a factor for size.
- Code generated for your executable - Your own executable might be large, the static libraries aside. For example if you have many template instantiations this will swell your executable's size. Each of the templates is expanded and then compiled as a separate class, meaning
QList<int>
andQList<double>
are two different classes (from the compiler's and/or linker's point of view) and code will be generated for each of them.
Additionally consider @jsulm's suggestion and strip the symbols you won't need. As far as I know, this could also be done by passing
-s
as an additional parameter to gcc (assuming you use that compiler). The relevant qmake variables are QMAKE_CXX_FLAGS for the compiler and QMAKE_LFLAGS for the linker respectively.I hope this helps.
Kind regards. -
@kshegunov
Wow, thank you for that explanation! About adding the -s to the gcc ....How can I add it? I mean.. i use to Ctr+R to compile and run my app (if release, if debug then F5). I've searched in the project properties but didn't found where can I add it. -
Try QMAKE_CFLAGS += -s in your *.PRO file
-
@roseicollis
You're welcome. I'd useQMAKE_CXX_FLAGS += -s
in the .pro file, that's why I linked the two qmake variables in my previous post.
Also as @jsulm wroteQMAKE_CFLAGS
might be used as well, but I'm not a 100% sure it'll be employed by qmake when compiling C++ source, so I suggest to use the C++ specific flags. -
@kshegunov, @jsulm
I've added the QMAKE_CXX_FLAGS+= -s line. Notice that my line is diferent from your s(is th eonly one recognized by my version in Qt). Before adding it, I checked my exe (which was 8.1Mib) and after adding that line and compile it again... its the same... 8.1Mib (and also checked the properties to see if the last change hour was diferent, and was everything correct).I'll copy part of my *.pro as it may be interesting some way maybe:
QT += core gui QT += network QT += script QT += date QT += time QT += xml QMAKE_CXXFLAGS += -std=c++11 QMAKE_CXX_FLAGS += -s greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET =MyProject TEMPLATE = app SOURCES + =... HEDAERS += ... FORMS+= ... // *My statically linked libraries here* unix:!macx: LIBS += -lrt unix:!macx: LIBS += -lexpat
Thanks!
P.d: (how do you mark in red a line here in the forum? I can't find the legend :P) -
Did you check whether -s was really passed to gcc (in the compiler output)?
-
@jsulm
Well... I get this:15:58:52: Running steps for project MyProject... 15:58:52: Configuration unchanged, skipping qmake step. 15:58:52: Starting: "/usr/bin/make" make: Nothing to be done for `first'. 15:58:52: The process "/usr/bin/make" exited normally. 15:58:52: Elapsed time: 00:00.
so looking on the internet to see how can I avoid the "Configuration unchanged, skipping qmake step." found that I had to remove the makefile, which makes:
16:00:14: Running steps for project MyProject... 16:00:14: Starting: "/usr/bin/qmake-qt4" /home/suser/workspace/Path/Projects/MyProject/MyProject.pro -r -spec linux-g++ Project MESSAGE: Warning: unknown QT: date Project MESSAGE: Warning: unknown QT: time 16:00:14: The process "/usr/bin/qmake-qt4" exited normally. 16:00:14: Starting: "/usr/bin/make" make: Nothing to be done for `first'. 16:00:14: The process "/usr/bin/make" exited normally. 16:00:14: Elapsed time: 00:00.
And if i clean and rebuild all the project and copy a part of all the message before it ends (if it ends rebuilding, all the messages are gone), I can see:
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
Where I can see the -s param clearly on the first line, fifth space. So I understand that the compiler efectively is using the -s param.
-
@roseicollis
The red text is inline code, you use it by putting the text between single backticks.This is a very strange build command. You compile in debug but set the code to be optimized (twice no less) ... that aside I don't know what further to suggest, in principle it should be working, but I've not really used it so I don't know for sure.
-
@roseicollis To avoid "Configuration unchanged" just rebuild your project