Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I make my program 'weight' less
QtWS25 Last Chance

How can I make my program 'weight' less

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt 4.8.5system
20 Posts 3 Posters 5.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jsulm
    4 Dec 2015, 09:13

    You could try to use the strip tool to remove all not needed symbols from your binary:
    strip YOUR_BINARY

    R Offline
    R Offline
    roseicollis
    wrote on 4 Dec 2015, 15:29 last edited by
    #11

    @jsulm can you explain me how to use it? I don't know how does that tools work and didn't find it in google either. Thanks!

    1 Reply Last reply
    0
    • R roseicollis
      4 Dec 2015, 09:02

      @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

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 4 Dec 2015, 15:34 last edited by
      #12

      @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 link Lib1 (filename libLib1.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 dependencies
      • unix:!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:

      1. 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.
      2. 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> and QList<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.

      Read and abide by the Qt Code of Conduct

      R 1 Reply Last reply 9 Dec 2015, 11:05
      1
      • K kshegunov
        4 Dec 2015, 15:34

        @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 link Lib1 (filename libLib1.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 dependencies
        • unix:!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:

        1. 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.
        2. 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> and QList<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.

        R Offline
        R Offline
        roseicollis
        wrote on 9 Dec 2015, 11:05 last edited by
        #13

        @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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 9 Dec 2015, 11:12 last edited by
          #14

          Try QMAKE_CFLAGS += -s in your *.PRO file

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 9 Dec 2015, 12:02 last edited by
            #15

            @roseicollis
            You're welcome. I'd use QMAKE_CXX_FLAGS += -s in the .pro file, that's why I linked the two qmake variables in my previous post.
            Also as @jsulm wrote QMAKE_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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • R Offline
              R Offline
              roseicollis
              wrote on 9 Dec 2015, 13:09 last edited by
              #16

              @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)

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 9 Dec 2015, 13:46 last edited by
                #17

                Did you check whether -s was really passed to gcc (in the compiler output)?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                R 1 Reply Last reply 9 Dec 2015, 15:06
                0
                • J jsulm
                  9 Dec 2015, 13:46

                  Did you check whether -s was really passed to gcc (in the compiler output)?

                  R Offline
                  R Offline
                  roseicollis
                  wrote on 9 Dec 2015, 15:06 last edited by
                  #18

                  @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.

                  J 1 Reply Last reply 10 Dec 2015, 05:20
                  0
                  • K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 9 Dec 2015, 17:27 last edited by
                    #19

                    @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.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    0
                    • R roseicollis
                      9 Dec 2015, 15:06

                      @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.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 10 Dec 2015, 05:20 last edited by
                      #20

                      @roseicollis To avoid "Configuration unchanged" just rebuild your project

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      20/20

                      10 Dec 2015, 05:20

                      • Login

                      • Login or register to search.
                      20 out of 20
                      • First post
                        20/20
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved