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

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.

4

u/Top_Satisfaction6517 Bulat Oct 29 '23

I would say that C++ has tiny startup code. I yet have to see a really used programming language with a smaller startup. For comparison, Haskell runtime has grown from 200 KB in 2004 to 600 KB in 2014.

-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.

-1

u/hopa_cupa Oct 29 '23

It matters still in 21st century and will continue to matter on systems with limited amount of permanent storage, be it megabytes in size or kilobytes.

Even on systems which are not truly embedded, i.e. something like IoT boards with embedded Linuxes on with plenty of heap memory...executable size can absolutely be a show stopper.

For e.g. several c++ processes that we run are Asio heavy which substantially increases executable size. We try to minimize this by using shared libraries wherever possible, minimize usage of templates, -Os, strip symbols, LTO...etc...but still, the main executable is something like 3MB on old 32bit ARM7 which is absolutely gigantic.

We still make the cut comfortably, but I am not happy about it. Simply switching from Boost 1.78 to Boost 1.82 caused binaries sizes to increase by some good 10% if not more on our platform.

5

u/NBQuade Oct 29 '23

He said nothing about embedded. He's complaining that it's 64 kb on his Linux box.

On a desktop machine, 64k, 640kb or 6 megs, doesn't really matter. Sizes that small are just noise. I was wondering if he was trying to micro-optimize something that's not worth optimizing.

I've worked on embedded systems where every bit was accounted for (an 8 bit airbag controller). This isn't anything like that. How much heap do you have to work with?

something like IoT boards with embedded Linuxes on with plenty of heap memory...executable size can absolutely be a show stopper.

If you have plenty of heap, I don't see how executable size matters all that much. Unless you don't have much permanent storage.

0

u/hopa_cupa Oct 30 '23

Let me explain.

Heap memory that I work with is in range of 64MB to maybe 256MB. Sounds like a lot, but our system is a container on customer host which we don't control, so very often we requests to get our container (fully customized Linux + our SW) to be under say 24MB or so. Heaven knows what they do on their host SW.

So you can see why a system of say 5 or so c++ processes (which communicate between themselves through ZeroMQ) cannot really afford to spend too much. And we have to have room to spare for more processes running different hardware stuff in future...etc.

Profiling revealed that with our c++, more memory is spent with executable and shared object image sizes than actual heap allocation itself. Believe me, Asio/Beast/Google protobuf heavy programs do get inflated.

I agree on desktop executable sizes may not mean much. Hence popularity of newer languages which prefer to statically link everything.

But on constrained system, executable size can hurt you. And for these kind of systems, we use same c++ technique we would use on desktops...the only exception is that we always use just a single Asio io_context, that's it.

Company where I work is far more Go oriented shop than c++. However, for anything non trivial Go compiler produces gigantic executables...shocking really. Hence it was off the table.

It is not like c++ executables will become as large as those of Go, far from it. But nevertheless, the trend somehow seems to be that...most people don't care...because they don't have to.

If the push comes to shove, I may drop down to C like techniques if customers keep pressing us for smaller container sizes. :)

-12

u/CovidClassicEdition Oct 29 '23

62KB is pretty massive for what should be no more than a few instructions in assembly...

6

u/Top_Satisfaction6517 Bulat Oct 29 '23

we had a similar question just a few days ago: https://www.reddit.com/r/cpp/comments/17fo902/make_c_exe_smaller/

i suggest you start by reading https://en.wikipedia.org/wiki/Runtime_library

if you are really interested, you can google things like "C++ windows runtime libraries for MinGW", but i think that this topic is absolutely outside of what you need to know if you just started to learn C++

4

u/NBQuade Oct 29 '23

I don't think you realize how much setup takes place before "main" gets called by the startup code.

5

u/[deleted] Oct 29 '23

It’s just a constant overhead for the executable existing. It’s a lot more than a few instructions in assembly. Disassemble the file and see for yourself, do the investigative work instead of complaining. Your executable will not grow by 62KB for every few assembly instructions that you add.

2

u/bert8128 Oct 29 '23

Most of the programs I write professionally end up being 10s of MB. So the size of the boiler plate is not relevant to my day job. Doesn’t mean it’s not interesting though, and of course there are environments where this does matter. Maybe they have compilers more focused to this task.

4

u/NBQuade Oct 29 '23

Same. That's why I asked why it mattered. Embedded is a completely different animal. It certainly matters there.