Free Educational Resource Center for teachers and students. Includes Interviews,
Sourcecode, Free Software, Research Papers, Articles, Tutorials and much more..
     R E S E A R C H A C T I V I T Y . C O M
Our Fellow Research Center for Ph.D Schollars
Home About Submit & Earn Archives: C And C + + Programming » Dev Packages » Interviews » Php Mysql Programming » Windows Programming
search
Windows Programming > reduce executable exe size

Reduce Executable (exe) Size

There are many ways to reduce size of the executable, Am presenting you general size reduction techniques, Compiler specific (which apply to every C/C++ compiler you use), as well as discuss some good things of mingW compiler (gcc for windows).

EXE PACKERS / COMPRESSORS:

The best tool I ever came across is UPX (Ultimate Packer for eXecutables), which not only works for Windows executables but also reduces size of linux/unix & Atari TOS/MiNT binaries.
Better is obtain its GUI version frontend from http://www.winapizone.net/software/upxfrontend/ which results brilliant when used with proper options.
It is a small commandline utility that compresses the file in seconds. And, a packed executable has no difference in speed compaing plain one. UPX may be obtained 100% free of cost for any purpose either commercial, educational, and/or personal, from http://upx.sourceforge.net/.

Few more commercial software are:
PECompact : produces good results
eXPressor : I did not test this done, but it claims to compress executables upto 70%
Search google for urls of these products.

Recomended executable compressor is UPX, which does not leave any flaw in final compressed executable.





MODULAR APPROACH, USE OF DLL FILES (Dyanmic Link Libraries):

This topic is specific to developers. Many developers do not like their software be dependent upong any external file. They prefer the core engine of applciation be built within single executable, and parts of software be executables too. But there stands a very good point of using of Dynamic Link Libraries, the more code goes into a separte file and simply your executable size shrinks down. Once a DLL is loaded into memory, all the functions accessed from the DLL run as faster as from executable itself. This kind of size reduction technique is only recomended for huge projects, which usually result in an executable of Mega Bytes not Kilo Bytes. And obviously, if you do not prefer your applciation be distributed among separate DLLs, then skip to next topic "RESOURCES".

One more good thing about DLLs is, you do not need to recompile entire application, if you have the core functions in the DLL, you keep upddating the DLL and supplying the new version without modifying the core applciation code.

Furthermore, there are two ways of using DLLs, or I may say linking to DLLs; Implicit Linking and Explicit Linking. 1. Implicit Linking means the DLL is directly linked with the executable using linker. Once an applciation is implicityly linked with a DLL, it cannot run WITHOUT dll, even if it is not using the functions or resources of DLL. 2. The other way is called Explicit Linking, which means, load DLL at runtime (when needed), create pointers to functions in the DLL, and free memory when job is done.

Recomendation: I recomend you use Explicit Linking, becuase Implicit Linking after all adds few bucks of bytes to executable while linking the DLL, on other side Explicit Linking has very small code that does not overall increase size of executable. NOTE: It may or may not apply to all kind of applications.




RESOURCES COMPRESSION:

Most of today's applications have bitmaps, languages, dialogs, key shortcuts, string tables in resources. What actualy resources are? Resources are part of executable, a binary representation of many kind of sources that our applciation can use at runtime, without putting them in separate files. UPX (mentioned above) also compresses the resources in executable files which gives very good result. Now, if you have a very lightweight compression / decompression library, it is recomended to compress resources first (before compiling final exe). Then uncompress and use them when need. For this purpose Basic Compression Library is the best choice (especially LZ compression routines), it adds few extra bytes to exe file, but can shrink down the resources to 50%. To folow this technique, you msut first create a simple application to compress each file that is to be added to resources, then compile resources and add only DECOMPRESSION functions to the final executable. Obtain it from http://bcl.comli.eu/home-en.html
NOTE: You may recieve some warnings and/errors while your IDE compiles the resources, by using compressed files. What you should do is simply give them custom type so that they are added as binary only. For example a warning is issued about an image abc.bmp that it is not of recognized format, so you will define it as:
myImage MYTYPE "abc.bmp"





REDUCING SIZE BY OPTIMIZING ICONS AND BITMAPS:

Many of the commercial icons sets come with all size icon images in one icon file, which may even exceed 90 kb, that is not good for an icon. You will be interested in knowing about discarding the unrequired size images from icon file. So, a free (opensource) software GIMP is out there, which can edit windows icon files. It opens an icon file in a way that every image in icon file becomes a layer, you simply delete the unwanted size layers, and save back the icon file. NOTE: make sure not to select the checkbox labeled "PNG Compression" which does not work on all windows operating systems.
GIMP may be downloaded from http://www.gimp.org

Now come ot bitmaps, I must say in simple words, The Images. Your program might have many of bmp files embedded to resources, and you are not in favour of adding decompression routine (talked above) to your software. So how to compress them? Best is use LILI(Liteweight Image Library) image loading library with your applciation, which is very lightweight and most importantly it is free of cost for any purpose, it just requires a little credit anywhere in your applciation (say about dialog). LILI can load BMP, JPEG, and GIF images (of any size and resolution) either from external files, from exe resources and/or from supplied char array containg image data. This library has very small foot print and adds few kbs to your application. Obtain this library from attached zip file and read details at http://www.apitalk.com/lili.

How to get maximum out LILI?

First convert all of your images to JPEG format with 100% quality. I usually convert images to JPEG and save with 85% quality, which in general cannot be judged by a user for quality. I use same software GIMP for this purpose. Then add them to resources but make sure to change resource type of anything else not "BITMAP", e.g. MY_RCTYPE. Something like this:

115 MY_RCTYPE "test.jpg"

Where 115 is resource Id and MY_RCTYPE is the custom type.

Functions in this library LILI:
/* Load image from a char string provided, which must be gif, jpg, bmp file data */
HBITMAP loadImageString(char *imagedata, int size);

/* Load image from an external gif, jpg, bmp file */
HBITMAP loadImage(char *filename);

/* Load image from resources, resource type must not be BITMAP, but any other identifier */
HBITMAP loadResImage(int resId, char *resType);
HBITMAP mybitmap = loadResImage(115, "MY_RCTYPE");



Major purpose of this library is to keep the application small and lightweight, as well as faster as possible to load image of more than 50 MBs in less than a little part of a second. See image loading example in attachments.

KEEPING IMAGE RESOURCES IN DLL: Another way may be putting images, icons, language resources in an external DLL. Which may reduce size of executable a lot.





C or C++ COMPILER:

The very first thing you need to decide is, if your code compiles perfectly with C compiler then there is absolutely no need of using C++ compiler, because when you use C++ compiler, it adds alot to your executable, by linking some libraries. I have been testing this a lot, a simple win32 api code that creates a dialog and a static control on it that is use to display image compiles generally (with c++ compiler) more than 800 kb, then if your compiler is clever enough to strip/discard unused code and/or has option to strip, it can cleanup your executable and make it smaller. So after stripping unused code that was (linked from win32 libraries), exe size magically came to 52 to 54 kb. Remember: it was still C++ compiler not C compiler. AND when I turn option of using C++ compiler OFF and let my code compile as plain C (which obviously was a plain C code), the exesize magically comes down to 7 kb (.^.). Then I compressed it using UPX and it turned out to 5 kb, and at the end I made a zip of this exe to make it downloadable, and zip was 3 and half kb (wow). There is not always same situation, you create a dialog and display image on it, your applciations will definitely get bigger, but yet it is great option in many compilers to reduce binary size by discardng use of C++ compiler when code can be compiled as pure ANSI C code.

I did not do much research on Visual C++ compiler options, frankly they are too much to browse and test. So I decided to conduct tests on mingw (gcc) compiler that comes with dev C++ IDE, which is mostly used for educational purpose.

Create your project in DEV C++, go through menus Project -> Options, in tab 'Files' Select each code file and uncheck the checkbox on rightside entitled 'Compile file as C++', then rebuild application and see size difference.
Now, there is something called stripping, which basically strips the debug information, as well as (am not sure about this, but it does more sice reduction) unused data/code segments in executable. Go to tab called 'Compiler' in same project options, and click 'Linker' options in left tree control, in right options box, there will be an entry 'Strip exeutable', turn it to 'Yes', now rebuild application and check exe size.

Few words on MINGW:
mingw links all the libraries statically, means, it embeds all the required libraries into your executable to make it standalone, comparing other compilers, they do compile final executable in a very small size but, at the end it requires a rutime dll file that becomes anoying for your clients/users. e.g. I made a compression and decompression product for my company based on a DLL which is used explicitly. I thought to provide support for Microsoft Visual C++ 2005 too, the DLL MSVC produced was incredibally small in size, and finally I came to knwo that it is dependant in vc8 runtime that is required when you use standard input and output functions in your code. But when compiled with mingw, DLL size was bit larger but independant (independant, becuase it was just lining to system libraries which are found in win 98 through XP/Vista). Besides I used software called Dependency Walker to check the dependencies of my DLL. Just my thought, that, Borland C++ compiler tools (which are also free) do same as mingw


I will be posting more compilers' specific options once I get some more time to examine.
Till then...

load_image_LILI-devc++.zip:49631:7