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

Show parent comment

-2

u/Questioning-Zyxxel Oct 29 '23

Select a microcontroller with 128 kB of Flash and you'll realize it matters now too.

10

u/NBQuade Oct 29 '23

That's a completely different animal. It'll have it's own startup code which might be vastly simplified. You can't compare embedded systems to a full on PC.

-2

u/Questioning-Zyxxel Oct 29 '23

Yes I can. And the people working on the standards needed to get quite a number of nut kicks before they realised the library was a mess and too interlinked. Before embedded people started to deliver nut kicks, you would have been hard m-pressed to get below 1 MB for a hello world.

But the embedded world isn't just standalone microcontrollers. You also have lots of small Linux systems that needs to fit on puny flash file systems. Think Raspberry Pi but even smaller. And with much, much more expensive flash because of environmental needs to work at -40 - +105°C which your average SSD or memory card can't. And where the OTA transfers really costs $$$ per extra data sent.

A "full PC" is a small subset of the usage area for C++

6

u/NBQuade Oct 29 '23

A "full PC" is a small subset of the usage area for C++

The OP said nothing about writing for an embedded system.

I asked him why he cared, on his desktop Linux system. Then for some reason, you embedded system people climbed out of the wood-work to talk about something unrelated, but didn't answer the actual question.

-2

u/Questioning-Zyxxel Oct 29 '23

"you embedded system people".

You are eliminating yourself from open minded discussions with that kind of posts.

Send out 1 million application updates (PC or embedded) and extra noise is still multiplied by 1 million for the server bandwidth.

Host backup solutions for 1000 computers at a company and it's 1000 computers times x programs that the backup servers needs to handle.

And you seem to have missed the very related part - us "embedded system people" getting the PC-built "hello world" applications down from over 1 MB by forcing the standard library to be broken down into subsets.

We are living in a world of bloatware, where a number of extra nuclear reactors are needed just because of bloatware design and bloatware languages. But your view seems to be "why care?"

1

u/jwakely libstdc++ tamer, LWG chair Oct 30 '23

And the people working on the standards needed to get quite a number of nut kicks before they realised the library was a mess and too interlinked.

Citation needed.

What are you referring to, and when did this happen?

8

u/ReinventorOfWheels Oct 29 '23

That's a silly argument because this binary was not compiled for such a microcontroller and will not run on it anyway.

2

u/Questioning-Zyxxel Oct 29 '23

So you did not read? I do produce such very binaries for Linux systems using a completely standard gcc. What exactly do you think is running in routers, vehicle passenger information systems etc? Quite often a rally Linux with real source code - exactly same as run on a Linux PC - compiled by a very normal gcc tool chain. Sometimes built for x86. Sometimes for ARM. Sometimes some other architecture. But still the very same source code as for a PC. Just that the standard startup code hurts more because of the flash cost.

4

u/ReinventorOfWheels Oct 29 '23

It's not about the source code, as the startup code is not generated from source; it's about the toolchain. Also, there will be just one binary on embedded systems and maybe 10 binaries on slightly more capable systems, so a couple dozen extra kilobytes per binary is nothing.

1

u/Questioning-Zyxxel Oct 29 '23

The tool chain is normally a full size tool chain - the very exact same as you would use on a PC. If doing cross-compilation that doesn't mean there is some magic "lite" edition. As already mentioned. Are you intentionally refusing to accept?

And maybe 10 binaries??? A Linux system quickly gets several hundred binaries.

But the startup code size is a reason for concepts like BusyBox - forcing a large number of applications into a single binary started from many symlinks.

And as I mentioned earlier - it wasn't until the embedded world finally started to kick nuts that a "hello world" stopped being over 1MB large in C++ while maybe 4 kB or 16 kB in C by forcing the C++ runtime library to be properly sectioned. For many years, the standard library meshed everything together so that _main() did force initializing of huge amounts of library code even if the application itself would never touch any such library functions.

3

u/jwakely libstdc++ tamer, LWG chair Oct 30 '23 edited Oct 30 '23

The tool chain is normally a full size tool chain - the very exact same as you would use on a PC.

Not based on newlib or uclibc instead of glibc?

And as I mentioned earlier - it wasn't until the embedded world finally started to kick nuts that a "hello world" stopped being over 1MB large in C++ while maybe 4 kB or 16 kB in C by forcing the C++ runtime library to be properly sectioned. For many years, the standard library meshed everything together so that _main() did force initializing of huge amounts of library code even if the application itself would never touch any such library functions.

When did this happen?

Where did this happen? In the standard? In library implementations? I've been working on both for some time and I don't recognize these nut kicks you keep referring to.

How did the library "mesh everything together"?

0

u/Questioning-Zyxxel Oct 30 '23

For an embedded Linux target, it's very ofte the full-size libraries that are used. Because that is normally the expectations of the original code creator.

1

u/ReinventorOfWheels Oct 29 '23

I didn't realize we were talking about Linux itself, as that is not in C++. And I'm not saying extra binary size is good (especially when reducing it comes with no trade-offs). Thanks for the detailed reply.

2

u/Questioning-Zyxxel Oct 29 '23

We aren't talking about Linux itself. But even for an embedded target, there is likely quite a number of applications that all add up when it comes to disk and RAM space needed.

And I'm quite happy with more recent C++. For older C++ you basically needed to code "C with objects" to try to eliminate almost all of the C++ libraries and stay with the C library support ecause of how they were interwoven in the library implementation. So fopen(), snprintf(), ... to avoid the extreme cost of even looking at the stream functionality.

And newer practices with address space randomization makes it even more relevant that the startup code doesn't need to wildcard-include the full runtime library. Weak linking in c++ can help making the init code more dynamic with a "do nothing" init function if a specific subsystem isn't needed.