2

I'm currently porting a library (not written by myself, I'm "only" porting it) to use the MinGW compiler. The library is a real heavyweight, with all kinds of C++ "black magic" (multiple inheritance, templates of templates of templates, very macro heavy, etc.).

Now, after some weeks, I get everything compiled fine and it also seems to work well (unit tests work, as do the demos).

What bugs me, though, is the sheer size of the MinGW binaries in contrast to the MSVC binaries. I know that MinGW binaries are generally slightly bigger due to having to include the own non-MS system libraries, but we're talking about 33 MB for MinGW vs 13 MB for MSVC. And that is the "release" (-O3 and -s flags) version!

Those are the flags I compile with in MinGW:

-c -O3 -s -MMD -march=native -frtti

And for the linker it is this:

-shared -s -static-libgcc -static-libstdc++ 

I know that rtti adds some size, but it also has to be in the MSVC binaries. And the static libgcc and libstdc++ libraries can't be that big... or can they?

What am I missing here? Normally, the size difference between MinGW and MSVC isn't that big.

TheSHEEEP
  • 2,961
  • 2
  • 31
  • 57
  • Related post with solutions: [GCC C++ “Hello World” program -> .exe is 500kb big when compiled on Windows. How can I reduce its size?](https://stackoverflow.com/q/1042773/183120) – legends2k Jun 14 '18 at 16:01

2 Answers2

0

First off, who cares? Have you seen this larger size to impact performance?

Second: are you sure you're linking statically with MSVC? Check with Dependency Walker to be sure.

Third: yes MinGW GCC compiled binaries are generally larger. This is because MinGW provides a part of the C library to make up for how ridiculously broken msvcr* is compliance-wise. This has never shown as a large difference for me though.

Fourth: template and inline handling can differ between the two compilers.

Fifth: have you tried to compile the GCC version with -fno-keep-inline-dllexport and/or -Os?

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 4
    First off, our repository cares. ;) Especially when I have to reupload that library several times, should I want to change something in the library itself (I can't upload the sources, just the binaries to our own repo). I will try out the flags you suggested. See if there is a difference. – TheSHEEEP Aug 07 '13 at 10:07
  • 1
    What do you mean with statically linking with MSVC? Statically linking what? I am creating a dynamic library with both. Or do you mean the runtime? If so, I think the c++ runtime of MSVC is not ~20MB big, is it? – TheSHEEEP Aug 07 '13 at 10:19
  • In the GCC case, you're statically linking libgcc and libstdc++, which is a ton of code. If you're not doing that in the MSVC case (I'm not even sure you can at all), comparing the file sizes is meaningless. What happens if you don't statically link the GCC runtime libraries? – rubenvb Aug 07 '13 at 10:28
  • Both flags used and removing linking statically against runtime saved 1.5 MB. Not too impressive, unfortunately :( – TheSHEEEP Aug 07 '13 at 11:11
0

I experienced this issue once and for me it turned out to be just what rubenvb suggested- the C/C++ std library was being statically linked in MinGW, but dynamically linked in MSVC. I used "dumpbin /imports" on each compiled executable/dll to determine this. If you don't see MSVC*.dll as an import, its linked statically. You can link the std libs statically in MSVC by changing the "Runtime Library" setting in Visual Studio, which is a flag to cl.exe that is either /MT (for static) or /MD (for dynamic). I think it is dynamic by default.

Aaron
  • 594
  • 2
  • 12