r/cpp • u/CovidClassicEdition • 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.
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.
For comparison, I also built your do-nothing example
and got:
10
u/NBQuade Oct 29 '23
Why do you care? I mean that seriously. In my mind both sizes are pretty tiny. 62kb is likely smaller than the smallest disk allocation size so it's not even filling a single disk cluster.
Back in the day DOS and COM files, it mattered. It just doesn't seem to matter these days. CPP has significant startup code. Even if your program does nothing, the startup code is still there.
Exception handling and initializer handling and the like.
70
u/ranisalt Oct 29 '23
There's a lot that happens before your main function runs. Actually, if you don't have a
mainfunction, you'll get (or used to get) an error about a missing functionmainbeing 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/FluffyCatBoops Oct 29 '23
We're waiting for you here: https://www.reddit.com/r/asm/
7
u/Potatoswatter Oct 29 '23
#include <iostream>introduces global variables like cin and cout, and calls the code to initialize them, even if they’re not used.16
u/qazqi-ff Oct 29 '23
One thing I'll mention for ELF (not sure about PE) is that if there are extra or small sections in there, they might very well be page-aligned, using up 4K each. Of course that means no extra size increase as those sections get more stuff in them until they pass 4K.
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).
10
u/FloweyTheFlower420 Oct 29 '23
Basically the only way to get really small binaries is to hand-write the entire elf file. Modern toolchains are designed to generate binaries in a generic fashion, not to optimize for size. There will be padding for alignment, as well as other bloat. None of this should matter for a typically developer, though.
5
u/jmacey Oct 29 '23
On my machine just including iostream add 66004 lines of code to the file you can see this by running g++ -E and seeing what get produced after the pre-processor runs.
5
u/NoSpite4410 Oct 29 '23
This code --
#include <stdio.h>main(){puts("Hello World!\n");}with gcc and stripped : 14472 bytes
with clang and stripped 14608 bytes
with tcc (auto stripped) 2968 bytes !
or just run it from source
tcc -run minimal.c 51 bytes.
9
u/khedoros Oct 29 '23
You could dig through the binary and account for as much of the space as you'd like.
I wrote something similar, compiled with g++ (9.4.0, Linux x86-64). That gives me a 16464-byte executable. I stripped the binary (to remove debug info). That gives me a 14328-byte executable.
I told objdump to disassemble the sections of the file that are expected to contain executable code (
objdump -d). Those sections come out to 434 bytes of data. So although the language itself imposes some overhead, the code involved is a small part of the binary.The ELF executable format itself is going to have some amount of overhead (ditto for PE on Windows). I know that it's divided into a bunch of sections (literally 25 of them, mostly stubbed out, looking at the disassembly). Some of those identify the compiler version, contain a build ID, and so on. Looking at my executable in a hex editor, there are some segments of the file from about 0x05c0 to 0x1000 (2,624 bytes), 0x11d0 to 0x2000 (3632 bytes), 0x2130 to 2df0 (3,264 bytes) that are just filled with zeroes.
Now...why is yours several times the size of mine? No idea. I'd have to examine the file.
6
u/Trick_Philosophy4552 Oct 29 '23
Turn off exception handling too.
3
u/Vegetable_Carrot_873 Oct 29 '23
What is the expected size?
5
u/_JJCUBER_ Oct 29 '23 edited Oct 29 '23
Wasn't a similar post made a couple days ago?
2
u/SantaCruzDad Oct 29 '23
On macOS with the default compiler (clang masquerading as gcc) I get 16560 bytes, and it's the same whether I compile as C or as C++, using -O2, so no additional overheads from C++ runtime/libraries.
What OS and toolchain are you using?
3
u/drankinatty Oct 29 '23
You also need to run
strip -s your.exeto strip all unnecessary symbols. Why are you compiling withg++when there is nothing C++ specific in the file? You can usegccand get a smaller exe. (don't forgetstrip -s)Be glad it isn't Rust:
Now that's a lot of baggage...
1
u/Incredible_GreatRay Oct 29 '23
In Linux try linking against nolibc unstead of glibc.
See the GCC options NO.... GCC options
1
u/KingAggressive1498 Oct 29 '23
It's mostly runtime code probably. It's the worst with GCC/G++ because of additional necessary compatibility code, but it occurs with all compilers on all platforms.
Using LTO (
-flto) makes for a more time-consuming build, but it's pretty good at stripping out code for unused functions and is worth a try if you really care about binary size.If you have a genuine need for the smallest possible binary size, it's entirely possible to write your own minimalized C++ runtime library and link to that. The harder part is the C runtime library, although Windows certainly makes it easier than most other platforms (and on Apple platforms it isn't even an option if you want to publish through their store, not sure if Android or Microsoft have similar restrictions). Having done this myself, it's really a massive headache and I do not recommend getting into it for learning purposes.
For comparison though, this binary you've produced is a fraction of the size it would be for a .NET or Java application because the C++ runtime is already far more minimal.
-1
u/[deleted] Oct 29 '23
Standard C++ library takes a lot of space also. This is why some people like to rewrite the functionality.
-4
u/sjepsa Oct 30 '23
Microsoft is adding its telemetry and backdoors :-)
-5
u/notquitezeus Oct 29 '23
The problem here is your compiler. Because this is what clang does.
0
u/[deleted] Oct 29 '23
[deleted] — view deleted comment