Reduce windows executable size

前端 未结 4 506
北荒
北荒 2021-01-05 17:38

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

4条回答
  •  孤街浪徒
    2021-01-05 18:16

    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:

    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 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.)

提交回复
热议问题