Introduction
So what are those magic tricks? Actually all you have to do is to instruct compiler to leave out unnecessary code from your application. If you create a simple HelloWorld Win32 Application from the app wizard and compile it in release mode, the resulting exe file size is 24K. Too much for a simple "Hello World" application. Now, perform the following steps: Create another similar project workspace, i.e. another "Hello World" project. Select active build configuration to be "Win32 Release". Open project setting and select the "Link" tab. Remove all the library file names from the "Object/library modules:" edit box and type "MSVCRT.LIB kernel32.lib user32.lib". Press OK and compile, the final exe file size is reduced to 16K. You might get a linker warning like "LINK : warning LNK4098: default lib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library". This can be avoided by clicking "Ignore all default libraries" from the Link tab of Project Settings. The further magic is done by using /ALIGN linker option. You can see MSDN for further details about this linker option. Again go to the "Project Settings", "Link" tab. Type /ALIGN:4096 in the Project Options edit box. Press OK and rebuild your project. The size is further reduced to 3K. The compiler generates a linker warning "LINK : warning LNK4108: /ALIGN specified without /DRIVER or /VXD; image may not run". If you have followed my instructions properly, your exe file should run properly. I played around with the /ALIGN linker directive and figured out that if you use /ALIGN:4096 the produced exe file runs properly. That's it, we have reduced size of our Hello World app from 24K to 3K. There are some other options that you can try in this regard: You can run some symbol stripper utility on the final exe file. It helps reducing size in many cases. One such utility can be found at neoworx.com. There is a utility named ASPack, this utility also helps in compressing your exe files up to 50%.
|