[ACCEPTED]-Reduce windows executable size-linker

Accepted answer
Score: 10

You can't mix the CRT/MFC dlls. Going from 18 memory...

As the other answer suggested, you 17 can #define WIN32_LEAN_AND_MEAN and VC_EXTRALEAN. These 16 probably won't help though. They tend to 15 be about minimizing build time - not the 14 final exe size.

Short of rebuilding MFC (Which 13 is an option - you could rebuild it /Os, or 12 if you are feeling really cheeky, /GL - but 11 this will probably lead to more downstream 10 fun - hopefully it's already built /Gy).

OK. Simple 9 things to try. Build your app with /GL 8 /Os /GF /Gy /GA. In the linker you more 7 or less want /OPT:REF and /OPT:ICF and /LTCG.

I 6 have to say - a release build from 30k to 5 megabytes is a bit much. You could also 4 pass /map: to the linker and see what's 3 taking all that space - but that's very 2 very tedius.

It almost sounds like MFC wasn't 1 built with /Gy - but that would be surprising.

Score: 10

For programs using the CRT, you can use 8 the technique in this video by Per Vognsen on achieving 3.5KB executables. Windows\System32\msvcrt.dll ships 7 with every Windows since 95, so by linking 6 to that, you needn't package the Visual 5 C++ Redistributable with your app.

The basic 4 process is:

  1. Run Visual Studio's dumpbin on System32\msvcrt.dll and pipe it to a file
  2. Run a simple filter (awk '{print $4}') to create a msvcrt.def file
  3. Run VS's lib on msvcrt.def to generate msvcrt.lib
  4. Disable default libraries in your project (/NODEFAULTLIB on the link command line)
  5. Disable some Visual C++ checks (/GS- and remove any /RTC<x> flags)

Link against kernel32.lib and msvcrt.lib and voilà, your 3 tiny executable has zero dependencies besides 2 the OS.

(n.b.: To optimize for size (/O1), specify 1 memset as an intrinsic as detailed here.)

Score: 9

I'm not sure how to strip the executable, but 4 another thought is to compress it, for example 3 using upx, which will decompress it and run 2 it in place... Does that reduce the size 1 enough for you?

Score: 1

You can try Dependency Walker to check which DLLs that your 4 static EXE file depends on, then if it depends 3 on a library you are certain it exists at 2 your client's machine, you can compile without 1 it and reduce the size.

More Related questions