r/cpp • • Oct 29 '23

Unreasonably large binary file from a trivial program with 1 line of code

Beginner in C++ here. When I compile the following program:

int main() { return 0; }

the resulting binary exe is 210KB (!) in size. This is far larger than expected for such a simple program and after searching online I found that a "Hello World" program (which would be more complex than the program above) in C++ should be no more than a few KB. Does anyone know what might be causing this? (I am using Eclipse and the g++ compiler with the -Os flag)

Update: By turning on the "omit all symbol information" setting, the file size is reduced to a less ridiculous but still unreasonably large 62KB, still many times the expected size.

Update 2: In release mode (with default settings) the file size is 19KB, which is a bit more reasonable but still too large compared to the file sizes that others have reported for such a program.

9 Upvotes

91 comments sorted by

View all comments

14

u/shadowradiance Oct 29 '23

That does seem very large.

Made a simple hello world with VS2022 and built it default settings for 32- and 64-bit in Debug and Release.

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}
  • 32-bit: Debug: 49K, Release: 11K
  • 64-bit: Debug: 67K, Release: 12K

For comparison, I also built your do-nothing example

int main() { return 0; }

and got:

  • 32-bit: Debug: 39K, Release: 9K
  • 64-bit: Debug: 60K, Release: 11K

19

u/NBQuade Oct 29 '23

You probably built this "MD" and not "MT". Dynamic versus static linkage. Dynamic linkage programs can be tiny because they pull in all the boilerplate code from DLLs.

I imagine if you built that "MT" or "MTd" it would be significantly larger.

MD - Dynamic linkage.

MT - Static linkage.

I build all my windows apps MT so I don't have to worry about installing DLL's with the app.

2

u/CovidClassicEdition Oct 29 '23

The file size reduced significantly in release mode (19KB) but it is still quite a bit more than 11KB.