6 ms·
The problem not in the language per se, but in the linker settings. By tweaking it, you can achieve almost same size as the assembler version has.
by SomeoneFromCA 7y ago
The problem not in the language per se, but in the linker settings. By tweaking it, you can achieve almost same size as the assembler version has.
- BubRoss 7y agoYou say that, but I still haven't seen any compilers link in only parts of libc. Use any of it and it all comes in. Hello world in C becomes anywhere from 120KB to 1.2MB even if just using puts. Even setting the entry point to a function with no arguments doesn't help. A tiny hello world ends up needing to ignore the standard library and use a console output function from the OS.
- SomeoneFromCA 7y agoJust checked on a Ubuntu machine. Hello world takes 6Kb. Uses printf.
- BubRoss 7y agoThat's because it links dynamically to the standard library.
- Dylan16807 7y agoIs that a problem? It's there whether you use it or not, and it's not like the asm is running in a vacuum either. If you go to an embedded point of view where there is nothing already supplied, you can get a hello world, even with a basic printf, down to that size.
- snvzz 7y agoEspecially since you use external libraries to do the bulk of the work, 6KB is a lot. It is an order of magnitude bigger than the assembly version.
- deleted 7y ago[deleted]
- SomeoneFromCA 7y agoAfter some manipulations with the linker, I was able to cut the size down to 750 bytes. You cannot go below this, without hacking the ELF file format.
- snvzz 7y agoI got my asm one down to 268 without even trying. It prints (via write syscall) "hello world.\n" and then calls exit(0). Assembled with fasm, linked with ld -n, then stripped.
- SomeoneFromCA 7y agoOk fine, I've used the tool called sstrip from a suite called ELFKickers. Now my hello_world.cpp (yes, c++) is 380 bytes long. I could not make it any shorter, so yeah, it is 120 bytes (50%) bigger than yours.
- snvzz 7y agoRegarding sstrip, I am familiar with it. Be careful that you know what it actually does.
- SomeoneFromCA 7y agoSo, not order of magnitude then?
- snvzz 7y agoYes, just look into how much effort you had to put vs my five minutes attempt. The difference is between just using asm, or going out of the way to try to coerce a C toolchain to minimize the bloat.
- deleted 7y ago[deleted]