4 ms·
Why does a simple "Hello World" create a 578KB binary? Is it packaging a lot of extras that aren't needed for such a simple program?
by aembleton 11y ago
Why does a simple "Hello World" create a 578KB binary?
Is it packaging a lot of extras that aren't needed for such a simple program?
- steveklabnik 11y agoIt statically links the entire standard library by default.
- nly 11y agoHello world in C++ is 1.3 MiB when linked statically. $ cat helloworld.cpp #include <iostream> int main() { std::cout << "Hello world" << std::endl; } $ g++ -std=c++14 -Os -static helloworld.cpp $ size a.out text data bss dec hex filename 1294232 29692 92936 1416860 159e9c a.out The majority of this is bloat from GNU libc though, using diet libc would yield better results $ cat helloworld.c #include <stdio.h> int main() { puts ("Hello world"); } $ gcc -std=c99 -Os -static helloworld.c $ size a.out text data bss dec hex filename 725706 7572 9080 742358 b53d6 a.out
- kevinnk 11y agoAs I've pointed out in a previous thread, virtually all of it is from a) iostreams and b) glibc. If you don't statically link glibc (as is the case the vast majority of the time) it's much smaller. Edit: You made my point for me in your edit :) Leaving my original comment for posterity.
- lambda 11y agoIt is possible, with a little manual hacking of the linker and some trickery with storing strings in ELF headers, to get Rust to produces 151-byte executable. Yes, a simple "Hello World" winds up statically linking a fairly sizeable standard library, in particular anything dealing with strings has to involve a certain amount of UTF-8 manipulation and the formatting and I/O have a lot of features and thus likely fairly large code size. But for the most part that's just fixed overhead, and you can opt out of using the standard library if you want and produce smaller executables.
- GarryManson 11y agoBecause regardless of what the propaganda says, Rust is in the same tradition of most other popular languages: bloated code for features you don't care about. If you want real small code, you use an array language with a small interpreter.
- madez 11y agoRust offers features (see [1]) many do care about. If you don't need what Rust offers, it's okay, move on. [1] http://www.rust-lang.org/ http://www.rust-lang.org/
- SamReidHughes 11y agoThat's really a detail of the implementation, something that you could expect to be improved upon, not the language.
- InclinedPlane 11y agoRust does static linking by default, because it's more bulletproof and hard drives are cheap. If you want tiny binaries using dynamic linking then you can use "rustc -C prefer-dynamic".
- aembleton 11y agoThanks, that brings it down to 9.1KB which is much closer to what I'd expect. I'm happy it does static linking by default because like you said hard drives are cheap and it's safer.
- Jweb_Guru 11y agorustc -C prefer-dyanmic.