Monday, October 12, 2009

Large executables for GCC built on Windows (cygwin/MinGW) - how to reduce the size?

Yes, the large executables for GCC built on Windows (cygwin/MinGW) can be reduced.

First we see why such a large executable?

Generally the size of executables built on Windows (cygwin/MinGW) would be larger than 20 MB (this metric is derived by experimenting with the build of GCC compiler for IA64). This is because of the presence of symbol information for debugging.

If the executable built is not for debugging, then the symbol information shall be stripped-off from the executable.

Strip-off symbol information from executable

The symbol information can be stripped-off from the executable (built on either cygwin or MinGW) by any of the following ways:

  1. Strip utility from binutils
  2. Set the environment variable "CFLAGS" with '-s'

1. Strip utility of binutils

The strip utility of binutils removes symbols from object file. The symbol information from the executable can be stripped-off as follows:
strip cc1.exe

Note: Assume the executable from which the symbol information is to be removed is "cc1.exe".

2. Set the environment variable "CFLAGS" with '-s'

The environment variable "CFLAGS" should be set with '-s' before the build of executable on cygwin/MinGW.

I.e. The environment variable CFLAGS can be set in the shell as follows:
export CFLAGS="-s"

Note: To check whether the enovironment variable CFLAGS is set or not, use the shell command "env". The shell command "env" would list of environment variables.

After stripping off the symbol information, the size of the executable would be reduced to ~7 MB (this metric is derived by experimenting with the build of GCC compiler for IA64).

Executable Compression

The size of the executable can be reduced further by executable compression. Refer executable compression.

The executable compression can be done using the executable packer UPX. UPX is a free and open source executable packer. For details on UPX, refer upx.sourceforge.net.

The executable compression can be done as follows (in windows command prompt):
upx cc1.exe

Note: Assume the executable to be compressed is "cc1.exe".

After compressing the executable, the executable size would be reduced to ~2 MB (this metric is derived by experimenting with the build of GCC compiler for IA64).