How To Reduce The Size Of An Exe

Table of contents:

How To Reduce The Size Of An Exe
How To Reduce The Size Of An Exe

Video: How To Reduce The Size Of An Exe

Video: How To Reduce The Size Of An Exe
Video: How to use upx to reduce pyinstaller exe size in python 2024, May
Anonim

The small amount of RAM and long-term storage devices on personal computers in the past imposed very strict restrictions on the size of programs. This problem does not exist today. However, even now it is sometimes important to minimize the size of the exe-module of the developed application as much as possible.

How to reduce the size of an exe
How to reduce the size of an exe

Necessary

  • - source;
  • - compiler, linker;
  • - compressors of PE modules, such as UPX, Themida.

Instructions

Step 1

Build the release version of the application executable. Select the appropriate configuration in the project settings in the IDE. If there is no such configuration, create it based on the existing one. Modify the list of linker options by removing and adding appropriate directives. So, when using a development package from Microsoft, you should remove the / debug option. You can also add the following directive to the source code: #pragma comment (linker, "/ RELEASE")

Step 2

Configure the project to avoid linking the executable with static libraries as much as possible. Use shared versions of the respective libraries. For example, you can exclude the code of the C and C ++ runtime libraries by replacing the linker option / ML or / MT (static single- and multi-threaded libraries) with / MD (multi-threaded CRT DLL).

Step 3

Consider merging different sections of the exe module into one. This method will not give a noticeable result if the file is large enough, but with the initial module size of 20-30 kilobytes, the gain can be significant. The / merge linker option allows you to merge sections. You can set it through the project parameters: / merge:.text =.data /merge:.reloc=.data /merge:.rdata=.data or using the pragma directives in the source code: #pragma comment (linker, "/ merge:.text =.data ") # pragma comment (linker," /merge:.reloc=.data")#pragma comment (linker, "/merge:.rdata=.data")#pragma comment (linker," / merge:.idata =.data ") It also makes sense to define the attributes of the resulting section: #pragma comment (linker," /section:.data, rwe ")

Step 4

Reduce the size of the exe by setting the minimum value of the size of the blocks along the boundaries of which the sections are aligned. Use the / filealign linker option specified by editing the project properties or the pragma directive: #pragma comment (linker, "/ filealign: 0x200") This method is suitable for small modules.

Step 5

Try to reduce the size of the exe file by building it with optimization options to reduce the amount of machine code. Replace the / O2 or / Od compiler options with / O1.

Step 6

Replace the standard DOS stub in the exe-module with your own, which will have the minimum size. Use the / stub linker option: #pragma comment (linker, "/stub:mystub.exe")Here mystub.exe is the name of the DOS executable file that will be added to the exe module as a stub.

Step 7

Consider specifying your own entry point to the application. This will eliminate the initialization code of the static runtime libraries. Use the / entry linker option, for example: #pragma comment (linker, "/ entry: MyStartup") void MyStartup () {:: MessageBox (NULL, "Hello!", "Message!", MB_OK);}

Step 8

Apply packaging utilities like UPX, ASPack, Themida, PECompact to the finished exe file. The module data will be compressed. They will be unpacked into memory after launching the application. This method gives good results for large exe files containing a large amount of static data with low entropy (for example, DIB rasters in the resource section).

Recommended: