GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?

前端 未结 14 2242
遇见更好的自我
遇见更好的自我 2020-11-27 04:29

I just recently started learning C++ - I am using nuwen\'s version of MingW on Windows, using NetBeans as an IDE (I have also MSDN AA Version of MSVC 2008, though I don\'t u

相关标签:
14条回答
  • 2020-11-27 05:16

    How is it that other compilers like msvc8 or even an order compiler like borland c++ 5.5.1 are capable of producing very small executables but mingw gcc isn't able to?

    I did a quick compile of a 'hello world' for each of the following toolsets and observed the compiled executable size. Please note that in all these cases the runtime library is statically linked and all debug symbols have been stripped:

    compiler toolchain            exe size                   exe size
                                  (w/iostream header)        (w/cstdio printf)
    -------------------------------------------------------------------------
    Borland C++ 5.5.1             110kbyte                    52kbyte
    MSVC 2008 express             102kbyte                    55kbyte
    MinGW- GCC 3.4.5              277kbyte                    <10kbyte
    MinGW- GCC 4.4.1              468kbyte                    <10kbyte
    

    What's interesting is the later version of gcc 4.4.1 produces an even larger executable than gcc3.4.5, probably due to different version of libstdc++.

    So is there really no way to remove dead code during the linking phase for mingw?

    0 讨论(0)
  • 2020-11-27 05:17

    The major part of the size stems from the use of fairly extensive runtime libraries. So in real life you are actually linking a very large piece of 'dead code' if you have such a simple application.

    As far as I know there are no linker flags to skip the unused parts of a linked library.

    There are two ways I know of to fake a smaller application:

    1. Use dynamic linking. Then your application refers to the dynamically loaded library. You still need the full size (actually more) but you have a much smaller executable.
    2. Use an executable compression system.
    0 讨论(0)
提交回复
热议问题