5 ms·
I'm disappointed. In C I can get a 1k Hello Word binary simply be writing int main(void) { puts("Hello World!"); return 0; } The Rust
by pantalaimon 6y ago
I'm disappointed.
In C I can get a 1k Hello Word binary simply be writing
int main(void) {
puts("Hello World!");
return 0;
}
The Rust version is a mess [0]
[0] https://github.com/mcountryman/min-sized-rust-windows/blob/master/src/main.rs https://github.com/mcountryman/min-sized-rust-windows/blob/m...
- deleted 6y ago[deleted]
- slezyr 6y agoYou're comparing C+StdLib vs. Rust without StdLib. Disable stdlib for C version and use the following program: #include <windows.h> int _start() { const char str[] = "Hello world!\n"; HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE); DWORD written; WriteFile(stdout, str, sizeof(str), &written, NULL); ExitProcess(12); return 0; } https://stackoverflow.com/a/42536990 https://stackoverflow.com/a/42536990 Which is similar to what the Rust program does. The result should be significantly less than 1k
- zamadatix 6y agoI think the thing being measured by GP is "easiest way to get to the 1kb mentioned in the title" not "most complicated way to get as small as possible".
- ChrisSD 6y agoI tried your program with MSVC using the following command: cl small2.c /MD /link /entry:_start /subsystem:CONSOLE kernel32.lib It's 5kb.
- slezyr 6y agoIf I remember correctly you need a /nodefaultlib flag to disable stdlibs https://docs.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?redirectedfrom=MSDN&view=msvc-160 https://docs.microsoft.com/en-us/cpp/build/reference/nodefau...
- ChrisSD 6y agoOk, I've just tried that but I also needed to add `/force` otherwise it complained about missing symbols. So the full compile line is: cl small2.c /link /force /nodefaultlib /entry:_start /subsystem:CONSOLE kernel32.lib This gets it down to 3kb.
- m1el 6y agoCan you, though? Compiling that source on Windows using visual studio produces a 98K executable.
- userbinator 6y agoYou just need to add some compiler options; /MD should get the size down quite a bit already, then decrease the file alignment and merge sections together: /MD /link /align:4096 /filealign:512 /merge:.rdata=.text /merge:.data=.text That produced a 1.5K binary, which with a custom stub should reach 1K.
- simlevesque 6y agoSo the point is moot, you can't have a simple 1k C example.
- mcountryman 6y agoIn order to get that to 1Kb you would probably have to drop the `.idata` section and resolve syscalls from the PEB like I did.
- ChrisSD 6y agoHave you actually tried this? Merging .data into .text doesn't work. The binary doesn't run and there's this link warning: > LINK : warning LNK4254: section '.data' (C0000040) merged into '.text' (60000020) with different attributes
- deleted 6y ago[deleted]
- mythz 6y agoThe smallest executable gcc (on Ubuntu) compiles that to is 8296 bytes (tried every -O optimization).
- pantalaimon 6y agoHm I only looked at the output of size which gives text data bss dec hex filename 1566 600 8 2174 87e hello (no optimisations)
- nitrogen 6y agoIs that stripped or unstripped?
- ChrisSD 6y agoHave you actually tried this on MSVC? This is my program. I created a file called `small.c` #include <stdio.h> int main(void) { puts("Hello World!"); return 0; } I then compiled it using this command: cl small.c /MD The result? 9kb! That's 9 times the size.
- deleted 6y ago[deleted]
- renewiltord 6y agoActually, the equivalent Rust code to this is just the following: println!("Hello world!"); Exactly that text. Compile it and it comes in at exactly 13.6% of the C program you have that compiles to 1k.