Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Why do executables have to be so fat?
9 points by winter_blue on Nov 19, 2011 | hide | past | favorite | 5 comments
Even tiny little programs like "Hello World" take up several KB when written in C and compiled with GCC.

I've also written Hellow World in x86 assembly, and it took up something like 86 bytes.

Why the big difference? cstdlib? Can't the compiler throw out code that isn't used?



Have you seen how big main(){} is? On my system it is 22,427 bytes. A simple printf("hello world\n"); Adds about 200 bytes bringing it up to 22,637. That adds all the functionality of printf, variadic arguments, everything prinf does that your version doesn't. There is a basic C runtime that goes with all C programs. Every programming language besides assembly does something similar. You could always run strip on the executable that knocks the printf version down to 8,192 bytes.


http://blogs.oracle.com/ksplice/entry/hello_from_a_libc_free explains the details of developing a "hello world" program without libc.


The C library does have an impact on the binary size. Compiling Hello World with Glibc results in a 3 KB binary on my system, while compiling with Musl results in a 1.9 KB binary. Compiling statically (no shared library dependencies) with Glibc results in a 498 KB binary, while with Musl it is only 4.6 KB.


Not talking about the poster specifically, but in my expert opinion (irony intended), questions such as this one are a sure sign on a new/inexperienced programmer, obsessed with the wrong things. (Likewise for questions about whether i-- is faster than i++ in a C for loop, etc).

Such people hear, for example, that assembly is lower level than C, and as such can be faster, and have grand visions of rewriting operating systems and even mail clients in assembly.

Or talk about "bloat" regarding the size of executables, not realizing that the 400MB of, say, Pages.app is mostly template images, and that the real executable is like 1/10th the size.

Or, also that parts of code not needed are not even loaded from disk, so it doesn't matter if the raw executable code is 10MB, only how much of it is needed and how well it was written for speed.


"Even tiny little programs like "Hello World" take up several KB when written in C and compiled with GCC."

That would only be a real problem if it kept happening proportionaly as programs grow in lines of code.

A 86 byte raw source program -> 3K can seem like a huge jump.

A 300,000 line, 1MB raw source program -> 1MB + 3K, or similar, would be insignificant.

You can always strip debugging symbols, remove libs you don't use, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: