How to reduce the size of executable produced by MinGW g++ compiler?

后端 未结 4 775
情书的邮戳
情书的邮戳 2020-11-30 11:07

I have a trivial \"Hello world\" C++ program that is compiled to 500kB executable by MinGW g++ compiler under Win XP. Some say that is caused by iostream li

相关标签:
4条回答
  • 2020-11-30 11:31

    Flags to use:

    • -s like you've been doing to strip symbols
    • -lstdc++_s to specify dynamically linking against the libstdc++.dll
    • -Os to optimize the binary for size.

    By default mingw static links to libstdc++.a on Windows.

    Note that the lstdc++_s flag is only in MinGW with GCC > 4.4, I believe.

    0 讨论(0)
  • 2020-11-30 11:31

    You should be using -O for optimization. Add "-O{level}" to your compiler args and it will optimize for either speed or size. Check the docs for your compiler.

    You could also have debugging symbols enabled. Stripping those will also make it smaller.

    0 讨论(0)
  • 2020-11-30 11:47

    Using the -Os flag might help. That optimizes for size.

    0 讨论(0)
  • 2020-11-30 11:55

    Give strip and UPX a try.

    0 讨论(0)
提交回复
热议问题