I have a C++/MFC app on windows - dynamically linked it\'s only 60kb static it\'s > 3Mb.
It is a being distributed to customers by email and so needs to be as small as p
For programs using the CRT, you can use the technique in this video by Per Vognsen on achieving 3.5KB executables. Windows\System32\msvcrt.dll
ships with every Windows since 95, so by linking to that, you needn't package the Visual C++ Redistributable with your app.
The basic process is:
dumpbin
on System32\msvcrt.dll
and pipe it to a fileawk '{print $4}'
) to create a msvcrt.def
filelib
on msvcrt.def
to generate msvcrt.lib
/NODEFAULTLIB
on the link
command line)/GS-
and remove any /RTC<x>
flags)Link against kernel32.lib
and msvcrt.lib
and voilà, your tiny executable has zero dependencies besides the OS.
(n.b.: To optimize for size (/O1
), specify memset
as an intrinsic as detailed here.)
I'm not sure how to strip the executable, but another thought is to compress it, for example using upx, which will decompress it and run it in place... Does that reduce the size enough for you?
You can try Dependency Walker to check which DLLs that your static EXE file depends on, then if it depends on a library you are certain it exists at your client's machine, you can compile without it and reduce the size.
You can't mix the CRT/MFC dlls. Going from memory...
As the other answer suggested, you can #define WIN32_LEAN_AND_MEAN and VC_EXTRALEAN. These probably won't help though. They tend to be about minimizing build time - not the final exe size.
Short of rebuilding MFC (Which is an option - you could rebuild it /Os, or if you are feeling really cheeky, /GL - but this will probably lead to more downstream fun - hopefully it's already built /Gy).
OK. Simple things to try. Build your app with /GL /Os /GF /Gy /GA. In the linker you more or less want /OPT:REF and /OPT:ICF and /LTCG.
I have to say - a release build from 30k to megabytes is a bit much. You could also pass /map: to the linker and see what's taking all that space - but that's very very tedius.
It almost sounds like MFC wasn't built with /Gy - but that would be surprising.