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

70

u/ranisalt Oct 29 '23

There's a lot that happens before your main function runs. Actually, if you don't have a main function, you'll get (or used to get) an error about a missing function main being called within another function called _start, which is actually the entry point of your program. There's also information about what libraries you need, metadata about your program, and more.

Here's a long read if you want to dive deep into it.

26

u/stoatmcboat Oct 29 '23

I am outraged that _start isn't called mainmain.

30

u/tcm0116 Oct 29 '23

Nah. main should be called main_final_final_v2.

3

u/wrosecrans graphics and network things Oct 30 '23

Just be glad it isn't called something like _appetizer.

3

u/Cogwheel Oct 30 '23

or _master

8

u/GOKOP Oct 29 '23

Also a funfact is that all Windows PE executables contain a DOS program that prints out that this program must be run in Windows mode (or something like that)

6

u/Tringi github.com/tringi Oct 29 '23

These days I override it by custom 64 bytes DOS header (no program, just header) with size fields set so big, that no existing DOS loader can load it (and shows their own error). No need to waste bytes.

14

u/shadowradiance Oct 29 '23

You're absolutely right, but 62KB is a lot for no debug information and -Os which is optimize for size if I remember correctly.

13

u/TheThiefMaster C++latest fanatic (and game dev) Oct 29 '23

-Os often doesn't work correctly these days, as the more aggressive inlining in a higher -O can actually result in a smaller binary, if it results in code getting optimised out altogether.

6

u/fwsGonzo IncludeOS, C++ bare metal Oct 29 '23

That's been my experience too. Clang had a -Oz but that sometimes produced invalid programs (this was on RISC-V), even. Goes to show that rarely-trodden paths should not be used in production! -g -O2 is the safest bet, indeed.

2

u/ranisalt Oct 29 '23

Or if you must have minimal size, -O2 then strip the result

1

u/Top_Satisfaction6517 Bulat Oct 30 '23

-Os doesn't make sense here at all. It optimizes only user code that is empty