25

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 library and static link of libstdc++.dll.

Using -s linker option helped a bit (reducing 50% size), but I would by satisfied only by <10kB executable. Is there any way how to achieve this using MinGW compiler? Portability is not a big concern for me.

Is it possible to copy libstdc++.dll with the executable using dynamic linking? If so, how to achieve this?


Solved: I was using MinGW 3.4. Now I updated to latest MinGW 4.6 and the size was decreased by 90% to 50kB, with -s option even to 9kB, which is fully sufficient. Anyway - thanks everyone for help. Here you go my results

C++ Hello World program using iostream

MinGW | no options | -s option
------------------------------
3.4   | 500kB      | 286 kB
4.6   | 50kB       |   9 kB
Community
  • 1
  • 1
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
  • 5
    Use `-shared-libstdc++` to compile and dynamically link against libstdc++.dll, and `-Os` to optimize the binary for size. – wkl Nov 01 '11 at 21:42
  • 2
    Although it's un-C++-like, you could try `printf()`. I'm not sure how much of the same dependencies it shares with the iostream library. So it may or may not make it any smaller. – Mysticial Nov 01 '11 at 21:42
  • 2
    @birryree: `g++: error: unrecognized option '-shared-libstdc++'` using MinGW 4.6 – Jan Turoň Nov 02 '11 at 18:33
  • @Jan - Yeah I think in GCC 4.6, your generated binaries fall in line with the normal size expected with a shared libstdc++ - on Linux with GCC 4.6 a simple 'hello world' app is 8 KB for me. MinGW's GCC 3.4 generated sizes are that large since there was no concept of shared libstdc++ yet. – wkl Nov 02 '11 at 18:56
  • Under later versions of MinGW where standard libraries are linked dynamically, the static linking of standard libraries can be forced with `-static-libgcc` (C) or `-static-libstdc++` (C++) options. – Jan Turoň Apr 12 '15 at 13:04

4 Answers4

18

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.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • 1
    I receive this error using 4.6 version (other options works great) - `d:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lstdc++_s` – Jan Turoň Nov 02 '11 at 18:29
  • 2
    @Jan - Looking at mingw's release notes, they switched over to dynamically linking libstdc++ in 4.5.x, so that should be the default in 4.6, and I guess they removed library the flag would use. Not sure if you're still linking statically against libstdc++, but you could run DependencyWalker or `ldd` on your binary to see if that's true. – wkl Nov 02 '11 at 18:38
  • `ldd` is not found and `ld` deletes the executable(?) is it part of MinGW or Msys? – Jan Turoň Nov 02 '11 at 18:46
  • @Jan - `ldd` a Linux/Unix tool that allows you to find out what libraries a binary links to, there is a MinGW windows equivalent provided by the [altbinutils-pe](http://sourceforge.net/project/shownotes.php?group_id=7382&release_id=36702) package. [Dependency Walker / depends.exe](http://www.dependencywalker.com/) is a tool that you can install alone (also comes with Visual Studio) that provides a GUI that allows you to find out what a binary links to. `ld` is a different command and is gcc's linker. – wkl Nov 02 '11 at 18:54
  • Does the `-s` option apply to libraries which needs the symbol names to be called? – nn0p Aug 17 '16 at 15:40
10

Give strip and UPX a try.

genpfault
  • 51,148
  • 11
  • 85
  • 139
4

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

offtehcuff
  • 128
  • 5
0

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.

Jamie Carl
  • 1,206
  • 14
  • 22