Why is the Linux binary so much larger?

I was surprised by the big difference in file size amongst different targets. Why is the Linux binary so huge relative to the others?

❯ zig init
info: created build.zig
info: created build.zig.zon
info: created src/main.zig
info: created src/root.zig
info: see `zig build --help` for a menu of options

❯ zig build -Doptimize=ReleaseSafe
❯ l zig-out/bin/foo
-rwxr-xr-x  1 jecolon  staff   228K Mar 19 15:53 zig-out/bin/foo

❯ zig build -Doptimize=ReleaseSafe -Dtarget=x86_64-linux-gnu
❯ l zig-out/bin/foo
-rwxr-xr-x  1 jecolon  staff   1.8M Mar 19 15:53 zig-out/bin/foo

❯ zig build -Doptimize=ReleaseSafe -Dtarget=x86_64-windows-gnu
❯ l zig-out/bin/foo.exe
-rwxr-xr-x  1 jecolon  staff   431K Mar 19 15:55 zig-out/bin/foo.exe
3 Likes

It is debug, dwarf, elf, unwind, flate and other debug related functionality.
Windows and macOS provide most of this code in dynamic libraries.
Linux ReleaseSmall is 9.8k and is the only mode that does not include that code.
Use readelf -a zig-out/bin/foo to diplay information about the executable, most interesting is the symbol table .symtab that shows the size and the type of each symbol.

4 Likes

I see. Although adding the strip option in build.zig can get you pretty close!

❯ zig build -Doptimize=ReleaseSafe -Dtarget=x86_64-linux-gnu
❯ l zig-out/bin/foo
-rwxr-xr-x  1 jecolon  staff    18K Mar 19 18:11 zig-out/bin/foo

❯ zig build -Doptimize=ReleaseFast -Dtarget=x86_64-linux-gnu
❯ l zig-out/bin/foo
-rwxr-xr-x  1 jecolon  staff    11K Mar 19 18:11 zig-out/bin/foo
5 Likes

You are right, the biggest contribution to size is the debugging symbols/information.
After running strip zig-out/bin/foo the size is 197k (from 1.7M).
Also in windows the exe size is 505k plus a 1.2M pdb file.

Interestingly, using the strip option in build.zig gets me (substantially) smaller binaries than using the strip command after.

no strip:        2479264 bytes
strip -s:         264232 bytes
build.zig option: 109664 bytes

All binaries built with ReleaseFast

1 Like