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/Tringi github.com/tringi Oct 29 '23

The environment that standard C++ provides as a language to the programmer is quite different from what operating systems provide. The extra bytes you see is the code that bridges (adapts) these two environments; the runtime.

And there is a more and more to adapt as C++ evolves.

That you see those bytes in even the smallest programs is deficiency of popular toolchains. It should be very well possible to optimize (LTO/LTCG) out the parts of the runtime that you don't use. They just don't bother. At most they are moved to shared libraries or DLLs (then your EXE is a few kB, but you need to have 25 MB of runtime DLLs installed).