1

I have a program written in C that I compiled on both my Windows 7 laptop and a Ubuntu virtual machine. The compiler I'm using is GCC, but on Windows 7 I've downloaded MinGW.

After compiling the programs I checked their file sizes, and noticed that the .exe on Windows is far larger than the one on Ubuntu.

For Windows I get a file size of 69,280 bytes, but on Ubuntu I get a size of only 8,703 bytes.

What's going on here?

Delfino
  • 967
  • 4
  • 21
  • 46

1 Answers1

2

Regardless of which native runtime you're using (MinGW or mingw-w64), you'll encounter this bloat. This is because the Windows executable is linked to static libraries whereas GCC on Ubuntu links to shared libraries (.dll files if you're more of a Windows person, .so files if you're a *nix person, .dylib if you're an OS X person).

I just tested Cygwin64 vs. mingw-w64 with a very small C program (not even any headers since some may replace your function calls with their own macros that call functions), debug info included, and no optimizations, and Cygwin came out as the file-size winner at 67024 bytes vs. 132206 bytes. Optimizing for file size and stripping all symbols, the difference is still in favor of Cygwin: 8.5 KiB vs. 15.5 KiB. It's worth mentioning that both were compiled in 64-bit mode.

This is probably partially due to requiring static libraries (mingw-w64 normally uses static linking) vs. shared libraries/dll files (Cygwin appears to normally use dynamic linking). On Ubuntu, the same thing happens, except you have the choice of either static or shared libraries normally, the default being dynamic libraries to keep the file size small.

Once you get to compiling C++ programs that use std::cout from <iostream>, the file size increases rather dramatically on MinGW/mingw-w64. Compiling it with a static version of libstdc++ using the -static-libstdc++ compiler option ends up with a larger file size on Cygwin, though both end up somewhere above 550 KiB for the following code:

#include <iostream>
int main ()
{
  std::cout << "Hello, world!" << std::endl;
}

Not sure what it is on Ubuntu, but I imagine it's also fairly large with a statically linked C++ library compared with equivalent C code's few KiB. Personally though, I wouldn't worry about it. Storage and memory are fairly cheap these days as the saying goes, so I'd focus more on your execution time and avoiding memory leaks than worrying about an executable's file size.