Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 938
Jan Turoň Avatar asked Nov 01 '11 21:11

Jan Turoň


People also ask

How do I reduce the size of an executable application?

You can run some symbol stripper utility on the final exe file. It helps reducing size in many cases. One such utility can be found at neoworx.com. There is a utility named ASPack, this utility also helps in compressing your exe files up to 50%.

What is the size of MinGW compiler?

This file should start downloading in your standard download folder. This file is only 85KB so it should download very quickly.

What determines the size of an executable?

Apparently the most significant factor is the number of bytes the compiler writes out to the resulting file. Basically what you're asking is: "What does my binary executable actually contain?"

Why is MinGW so slow?

Many "unixy" things on MinGW are painfully slow, because Windows has no fork() . Windows only has CreateProcess() , which is quite different. Unix shells and GNU Make do a lot of forking, so running these under MinGW results in "emulated" forks, which are really slow.


3 Answers

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.

like image 134
逆さま Avatar answered Oct 20 '22 04:10

逆さま


Give strip and UPX a try.

like image 27
genpfault Avatar answered Oct 20 '22 03:10

genpfault


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

like image 45
offtehcuff Avatar answered Oct 20 '22 05:10

offtehcuff