PDA

View Full Version : How to reduce exe file size using Qt Creator?



Awareness
5th July 2011, 00:36
How can I reduce exe file size with Qt Creator?

I wrote a little program consisting of about 20 lines.After I built the exe file with Qt Creator,I saw it was 493.000 bytes.Then I tried Upx,it reduced the exe file size to 123.000 bytes.But I think it is still big for such a small code,isn't it?


ı have read a few threads about reducing exe size but they were not telling how to do it with Qt Creator...

For example,wysota wrote:

First olf all make sure you don't have Qt built in debug mode. Then make sure you pass some options to configure that

disable things you don't need (for instance -no-stl, -no-exceptions, etc.)"

But I am not sure how I can do this with Qt Creator...

ChrisW67
5th July 2011, 01:08
Look under Projects in the left hand tool bar and then check the build settings. You typically have a drop-down with Debug and Release options (depends on how you project originated).

Awareness
5th July 2011, 04:57
Thanks for your answer.

In which place should I write them exactly?I tried a few places under "Projects" section but I couldn't make it work I think because the file is still same size....

ChrisW67
5th July 2011, 06:20
Write what exactly? The Projects page build configuration drop down will let you make sure that the project is being built in release mode and not debug mode. If you are getting a 500K exe out of a twenty line program then it is almost certainly in debug mode. With a simple Qt program that just displays a few labels this makes the difference between 21K and 290K.

If you want to try to minimise the executable size by fiddling with compiler/linker options then you need to adjust your project to pass these. You should look at the qmake variables QMAKE_CFLAGS or QMAKE_CXXFLAGS and friends in conjunction with your compiler manual. If you are using GCC then you might try overriding the optimiser flag that qmake use with "-Os".

I wouldn't be obsessing over a 500K release executable if it is Qt based, because you are probably going to have another 4 or more MB of Qt libraries to go with it anyway.


Edit: I think the "-no-exceptions -no-stl -no-rtti" flags wysota is referring to are configuration options to build a smaller Qt, not a smaller user application.

Awareness
7th July 2011, 02:22
Thanks for your answer.

I generally compile in debug mode firstly,then compile with release mode.And I am talking about the exe file inside "release" directory,not inside "debug" directory...

"If you are using GCC then you might try overriding the optimiser flag that qmake use with "-Os"."

Will I write "-Os" next to QMAKE_CFLAGS or QMAKE_CXXFLAGS inside conf file?

Today I noticed something.I am using Qt Creator(and it uses gcc I think) as a pure c++ builder/compller mostly,because I am learning C++'s fundamentals for now.then I will blend it with Qt codes.

So I generally select "Qt4 Console Application" under "New File or Project" .Then I disable the tick next to QtCore and all the other modules don't have tick next to them.
After I write the code,I choose "build" and even a very small "Hello World" pure c++ exe is about 493.000 bytes,inside "release" directory.

I tried the same "Hello World" program using Qt code,and after I built it the exe file was about 75.000 byes,much smaller than pure c++ code.

So what maybe the problem?Why does little pure c++ code produce a big exe file using Qt Creator?

ChrisW67
7th July 2011, 05:18
What is your little C++ program? What libraries is it dependent on and are they statically linked? Chances are the standard C++ library (and others) is being statically linked.



#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "Hello, world!" << std::endl;
return 0;
}


Static versus dynamic linking:


$ g++ -o test1 -static main.cpp
$ g++ -o test2 main.cpp
$ ls -l test?
-rwxr-xr-x 1 chrisw users 1293108 Jul 7 13:14 test1
-rwxr-xr-x 1 chrisw users 7330 Jul 7 13:14 test2

The static version is much larger but needs no external libraries. The dynamic version is tiny but has external requirements. They both do the same thing. Take a close look at the compile command that Qt Creator is building for you.

Talei
9th July 2011, 18:26
Another way to optimise Qt is custom build and modification of the /qtDir/src/corelib/global/qfeatures.h .
These file allows You to turn on/off some additional features like i.e. QSplitter. So lets say my application don't use QSpliter, but within standard libraries this class is present and occupy some space in library, so using qfeatures.h I can disable it and rebuild Qt libs without i.e. QSpliter functionality thus getting smaller Qt libraries.

Awareness
9th July 2011, 22:49
Thanks for your answers.